CanDuplicate.php
1 year ago
CanFilter.php
2 weeks ago
CanFinalize.php
3 years ago
CanResendNotifications.php
1 year ago
HasAffiliation.php
2 years ago
HasAttributes.php
1 year ago
HasAutoFeeRules.php
5 months ago
HasBillingAddress.php
7 months ago
HasCharge.php
3 years ago
HasCheckout.php
3 years ago
HasCommissionStructure.php
2 years ago
HasCoupon.php
3 years ago
HasCustomer.php
3 years ago
HasDates.php
1 year ago
HasDiscount.php
1 year ago
HasDisputes.php
9 months ago
HasFees.php
11 months ago
HasImageSizes.php
2 years ago
HasInvoice.php
1 year ago
HasLicense.php
2 years ago
HasLineItem.php
2 years ago
HasOrder.php
3 years ago
HasPaymentFailures.php
1 year ago
HasPaymentInstrument.php
1 year ago
HasPaymentIntent.php
3 years ago
HasPaymentMethod.php
3 years ago
HasPayout.php
2 years ago
HasPayouts.php
2 years ago
HasPlatformFee.php
1 year ago
HasPrice.php
3 years ago
HasProcessorType.php
3 years ago
HasProduct.php
3 years ago
HasPromotion.php
1 year ago
HasPurchase.php
3 years ago
HasPurchases.php
3 years ago
HasReferral.php
2 years ago
HasReferralItems.php
2 years ago
HasReferrals.php
2 years ago
HasRefund.php
3 years ago
HasRefundItems.php
1 year ago
HasServiceFee.php
1 year ago
HasShippingAddress.php
7 months ago
HasShippingChoices.php
1 year ago
HasShippingMethod.php
1 year ago
HasSubscription.php
3 years ago
HasSubscriptions.php
3 years ago
HasSwap.php
1 year ago
SyncsCustomer.php
3 years ago
HasAutoFeeRules.php
103 lines
| 1 | <?php |
| 2 | |
| 3 | namespace SureCart\Models\Traits; |
| 4 | |
| 5 | use SureCart\Models\AutoFee; |
| 6 | |
| 7 | /** |
| 8 | * If the model has auto fee rules. |
| 9 | */ |
| 10 | trait HasAutoFeeRules { |
| 11 | /** |
| 12 | * Set the rules attribute. |
| 13 | * |
| 14 | * @param string $value Product properties. |
| 15 | * @return void |
| 16 | */ |
| 17 | public function setRulesAttribute( $value ) { |
| 18 | $this->attributes['rules'] = $this->handleCustomAttributes( $value, 'set' ); |
| 19 | } |
| 20 | |
| 21 | /** |
| 22 | * Get the rules attribute. |
| 23 | * |
| 24 | * @param string $value Product properties. |
| 25 | * @return array |
| 26 | */ |
| 27 | public function getRulesAttribute( $value ) { |
| 28 | return $this->handleCustomAttributes( $value, 'get' ); |
| 29 | } |
| 30 | |
| 31 | |
| 32 | /** |
| 33 | * Handle Custom Attributes. |
| 34 | * |
| 35 | * @param array $rule_json Rule JSON. |
| 36 | * @param string $type get or set. |
| 37 | * @return array $rule_json |
| 38 | */ |
| 39 | public function handleCustomAttributes( $rule_json, $type ) { |
| 40 | if ( empty( $rule_json ) || empty( $type ) ) { |
| 41 | return []; |
| 42 | } |
| 43 | |
| 44 | $rule_array = $this->convertObjectToArray( $rule_json ); |
| 45 | |
| 46 | if ( empty( $rule_array ) || ! is_array( $rule_array ) ) { |
| 47 | return []; |
| 48 | } |
| 49 | |
| 50 | if ( isset( $rule_array['conditions'] ) && empty( $rule_array['conditions'] ) ) { |
| 51 | return is_object( $rule_json ) ? (object) [] : []; |
| 52 | } |
| 53 | |
| 54 | $fee_target = $this->attributes['fee_target'] ?? 'line_item'; |
| 55 | $attribute_name = 'line_item' === $fee_target ? 'checkout.metadata' : 'metadata'; |
| 56 | |
| 57 | foreach ( $rule_array as $key => &$value ) { |
| 58 | if ( is_array( $value ) ) { |
| 59 | $value = $this->handleCustomAttributes( $value, $type ); |
| 60 | |
| 61 | if ( empty( $value['attribute_name'] ) ) { |
| 62 | continue; |
| 63 | } |
| 64 | |
| 65 | if ( 'wp_user_role' === $value['attribute_name'] && 'set' === $type ) { |
| 66 | $value['attribute_name'] = $attribute_name; |
| 67 | $value['metadata_key'] = 'wp_user_role'; |
| 68 | continue; |
| 69 | } |
| 70 | |
| 71 | if ( empty( $value['metadata_key'] ) ) { |
| 72 | continue; |
| 73 | } |
| 74 | |
| 75 | if ( $attribute_name === $value['attribute_name'] && 'wp_user_role' === $value['metadata_key'] && 'get' === $type ) { |
| 76 | $value['attribute_name'] = 'wp_user_role'; |
| 77 | } |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | return is_object( $rule_json ) ? (object) $rule_array : $rule_array; |
| 82 | } |
| 83 | |
| 84 | /** |
| 85 | * Convert Nested Objects to Array |
| 86 | * |
| 87 | * @param array $data Object. |
| 88 | * |
| 89 | * @return array $data |
| 90 | */ |
| 91 | private function convertObjectToArray( $data ) { |
| 92 | if ( is_object( $data ) ) { |
| 93 | $data = get_object_vars( $data ); |
| 94 | } |
| 95 | |
| 96 | if ( is_array( $data ) ) { |
| 97 | return array_map( [ $this, 'convertObjectToArray' ], $data ); |
| 98 | } |
| 99 | |
| 100 | return $data; |
| 101 | } |
| 102 | } |
| 103 |