Blocks
2 months ago
Concerns
6 months ago
Traits
2 weeks ago
Wordpress
1 year ago
AbandonedCheckout.php
1 year ago
AbandonedCheckoutProtocol.php
3 years ago
Account.php
10 months ago
AccountPortalSession.php
3 years ago
Activation.php
1 year ago
Affiliation.php
1 year ago
AffiliationProduct.php
1 year ago
AffiliationProtocol.php
2 years ago
AffiliationRequest.php
1 year ago
ApiToken.php
6 months ago
AutoFee.php
5 months ago
AutoFeeProtocol.php
5 months ago
BalanceTransaction.php
1 year ago
Batch.php
2 days ago
Brand.php
4 months ago
BulkAction.php
2 years ago
Bump.php
1 year ago
BuyLink.php
2 years ago
CancellationAct.php
1 year ago
CancellationReason.php
3 years ago
Charge.php
9 months ago
Checkout.php
2 months ago
Click.php
1 year ago
Collection.php
3 months ago
CommissionStructure.php
2 years ago
Component.php
4 months ago
Coupon.php
2 months ago
Customer.php
2 weeks ago
CustomerLink.php
3 years ago
CustomerNotificationProtocol.php
3 years ago
CustomerPortalProtocol.php
1 year ago
DatabaseModel.php
2 months ago
Discount.php
1 year ago
DisplayCurrency.php
1 year ago
Dispute.php
9 months ago
Download.php
3 years ago
Event.php
3 years ago
Export.php
2 years ago
ExternalApiModel.php
3 months ago
Fee.php
1 year ago
Form.php
3 years ago
Fulfillment.php
1 year ago
FulfillmentItem.php
1 year ago
GalleryItem.php
9 months ago
GalleryItemAttachment.php
5 months ago
GalleryItemImageAttachment.php
3 months ago
GalleryItemMedia.php
1 year ago
GalleryItemProductMedia.php
1 month ago
GalleryItemVideoAttachment.php
3 months ago
Import.php
6 months ago
ImportRow.php
2 months ago
IncomingWebhook.php
1 year ago
Integration.php
2 years ago
IntegrationCatalog.php
2 days ago
Invoice.php
1 year ago
License.php
1 year ago
LineItem.php
9 months ago
ManualPaymentMethod.php
1 year ago
Media.php
1 month ago
Model.php
2 days ago
ModelInterface.php
3 years ago
Order.php
2 weeks ago
OrderProtocol.php
3 years ago
ParcelTemplate.php
3 months ago
PaymentFailure.php
1 year ago
PaymentInstrument.php
2 months ago
PaymentIntent.php
1 year ago
PaymentMethod.php
1 year ago
Payout.php
1 year ago
PayoutGroup.php
2 years ago
Period.php
1 year ago
PlatformFee.php
1 year ago
PortalSession.php
3 years ago
Price.php
6 months ago
Processor.php
1 year ago
Product.php
2 days ago
ProductCollection.php
1 year ago
ProductCollectionImport.php
6 months ago
ProductGroup.php
1 year ago
ProductImport.php
6 months ago
ProductMedia.php
4 months ago
Promotion.php
1 year ago
ProvisionalAccount.php
2 months ago
Purchase.php
2 years ago
Referral.php
1 year ago
ReferralItem.php
2 years ago
Refund.php
1 year ago
RefundItem.php
1 year ago
RegisteredWebhook.php
2 months ago
ReturnItem.php
2 years ago
ReturnReason.php
2 years ago
ReturnRequest.php
1 year ago
Review.php
4 months ago
ReviewProtocol.php
4 months ago
RuleSchema.php
5 months ago
ServiceFee.php
1 year ago
ShippingChoice.php
1 year ago
ShippingMethod.php
3 years ago
ShippingProfile.php
3 years ago
ShippingProtocol.php
3 years ago
ShippingRate.php
3 years ago
ShippingZone.php
3 years ago
Statistic.php
3 years ago
Subscription.php
2 weeks ago
SubscriptionProtocol.php
3 years ago
Swap.php
1 year ago
TaxOverride.php
2 years ago
TaxProtocol.php
3 years ago
TaxRegistration.php
1 year ago
TaxZone.php
3 years ago
Upload.php
3 years ago
Upsell.php
1 year ago
UpsellFunnel.php
1 year ago
User.php
1 month ago
Variant.php
2 days ago
VariantOption.php
2 years ago
VariantOptionValue.php
1 year ago
VariantValue.php
2 years ago
VerificationCode.php
3 years ago
Webhook.php
1 year ago
WebhookRegistration.php
2 years ago
Affiliation.php
191 lines
| 1 | <?php |
| 2 | |
| 3 | namespace SureCart\Models; |
| 4 | |
| 5 | use SureCart\Models\Traits\HasCommissionStructure; |
| 6 | use SureCart\Models\Traits\HasDates; |
| 7 | use SureCart\Models\Traits\HasPayouts; |
| 8 | use SureCart\Models\Traits\HasReferrals; |
| 9 | use SureCart\Support\Currency; |
| 10 | use SureCart\Support\TimeDate; |
| 11 | |
| 12 | /** |
| 13 | * Holds the data of the current Affiliation. |
| 14 | */ |
| 15 | class Affiliation extends Model { |
| 16 | use HasReferrals; |
| 17 | use HasPayouts; |
| 18 | use HasCommissionStructure; |
| 19 | use HasDates; |
| 20 | |
| 21 | /** |
| 22 | * Rest API endpoint |
| 23 | * |
| 24 | * @var string |
| 25 | */ |
| 26 | protected $endpoint = 'affiliations'; |
| 27 | |
| 28 | /** |
| 29 | * Object name |
| 30 | * |
| 31 | * @var string |
| 32 | */ |
| 33 | protected $object_name = 'affiliation'; |
| 34 | |
| 35 | /** |
| 36 | * Activate an affiliation |
| 37 | * |
| 38 | * @param string $id Affiliation ID. |
| 39 | * |
| 40 | * @return $this|\WP_Error |
| 41 | */ |
| 42 | protected function activate( $id = null ) { |
| 43 | if ( $id ) { |
| 44 | $this->setAttribute( 'id', $id ); |
| 45 | } |
| 46 | |
| 47 | if ( $this->fireModelEvent( 'activating' ) === false ) { |
| 48 | return $this; |
| 49 | } |
| 50 | |
| 51 | if ( empty( $this->attributes['id'] ) ) { |
| 52 | return new \WP_Error( 'not_saved', 'Please create the affiliation.' ); |
| 53 | } |
| 54 | |
| 55 | $activated = \SureCart::request( |
| 56 | $this->endpoint . '/' . $this->attributes['id'] . '/activate', |
| 57 | [ |
| 58 | 'method' => 'PATCH', |
| 59 | 'query' => $this->query, |
| 60 | ] |
| 61 | ); |
| 62 | |
| 63 | if ( is_wp_error( $activated ) ) { |
| 64 | return $activated; |
| 65 | } |
| 66 | |
| 67 | $this->resetAttributes(); |
| 68 | $this->fill( $activated ); |
| 69 | $this->fireModelEvent( 'activated' ); |
| 70 | |
| 71 | return $this; |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * Deactivate an affiliation |
| 76 | * |
| 77 | * @param string $id Affiliation ID. |
| 78 | * |
| 79 | * @return $this|\WP_Error |
| 80 | */ |
| 81 | protected function deactivate( $id = null ) { |
| 82 | if ( $id ) { |
| 83 | $this->setAttribute( 'id', $id ); |
| 84 | } |
| 85 | |
| 86 | if ( $this->fireModelEvent( 'deactivating' ) === false ) { |
| 87 | return $this; |
| 88 | } |
| 89 | |
| 90 | if ( empty( $this->attributes['id'] ) ) { |
| 91 | return new \WP_Error( 'not_saved', 'Please create the affiliation.' ); |
| 92 | } |
| 93 | |
| 94 | $deactivated = \SureCart::request( |
| 95 | $this->endpoint . '/' . $this->attributes['id'] . '/deactivate', |
| 96 | [ |
| 97 | 'method' => 'PATCH', |
| 98 | 'query' => $this->query, |
| 99 | ] |
| 100 | ); |
| 101 | |
| 102 | if ( is_wp_error( $deactivated ) ) { |
| 103 | return $deactivated; |
| 104 | } |
| 105 | |
| 106 | $this->resetAttributes(); |
| 107 | $this->fill( $deactivated ); |
| 108 | $this->fireModelEvent( 'deactivated' ); |
| 109 | |
| 110 | return $this; |
| 111 | } |
| 112 | |
| 113 | /** |
| 114 | * Set the clicks attribute |
| 115 | * |
| 116 | * @param object $value Array of click objects. |
| 117 | * |
| 118 | * @return void |
| 119 | */ |
| 120 | public function setClicksAttribute( $value ) { |
| 121 | $this->setCollection( 'clicks', $value, Click::class ); |
| 122 | } |
| 123 | |
| 124 | /** |
| 125 | * Get the display name |
| 126 | * |
| 127 | * @return string |
| 128 | */ |
| 129 | protected function getDisplayNameAttribute() { |
| 130 | if ( ! empty( $this->first_name . $this->last_name ) ) { |
| 131 | return $this->first_name . ' ' . $this->last_name; |
| 132 | } |
| 133 | return $this->email; |
| 134 | } |
| 135 | |
| 136 | /** |
| 137 | * Get the display status attribute. |
| 138 | * |
| 139 | * @return string |
| 140 | */ |
| 141 | public function getStatusDisplayTextAttribute() { |
| 142 | return $this->active ? __( 'Active', 'surecart' ) : __( 'Inactive', 'surecart' ); |
| 143 | } |
| 144 | |
| 145 | /** |
| 146 | * Get the display status attribute. |
| 147 | * |
| 148 | * @return string |
| 149 | */ |
| 150 | public function getStatusTypeAttribute() { |
| 151 | return $this->active ? 'success' : 'default'; |
| 152 | } |
| 153 | |
| 154 | |
| 155 | /** |
| 156 | * Get the expires at date. |
| 157 | * |
| 158 | * @return string |
| 159 | */ |
| 160 | public function getExpiresAtDateAttribute() { |
| 161 | return ! empty( $this->expires_at ) ? TimeDate::formatDate( $this->expires_at ) : ''; |
| 162 | } |
| 163 | |
| 164 | /** |
| 165 | * Get the expires at date and time. |
| 166 | * |
| 167 | * @return string |
| 168 | */ |
| 169 | public function getExpiresAtDateTimeAttribute() { |
| 170 | return ! empty( $this->expires_at ) ? TimeDate::formatDateAndTime( $this->expires_at ) : ''; |
| 171 | } |
| 172 | |
| 173 | /** |
| 174 | * Get the total commission amount display. |
| 175 | * |
| 176 | * @return string |
| 177 | */ |
| 178 | public function getTotalCommissionDisplayAmountAttribute() { |
| 179 | return Currency::format( $this->total_commission_amount ); |
| 180 | } |
| 181 | |
| 182 | /** |
| 183 | * Get the total commission amount display. |
| 184 | * |
| 185 | * @return string |
| 186 | */ |
| 187 | public function getTotalNotPaidCommissionDisplayAmountAttribute() { |
| 188 | return Currency::format( $this->total_not_paid_commission_amount ); |
| 189 | } |
| 190 | } |
| 191 |