PluginProbe
Tiered Pricing Table for WooCommerce / 6.1.0
Tiered Pricing Table for WooCommerce v6.1.0
8.0.2 7.1.7 7.1.5 7.1.4 7.1.1 6.5.0 6.4.0 6.1.0 trunk 1.0 1.1 2.0 2.0.1 2.0.2 2.1 2.1.1 2.1.2 2.2.0 2.2.1 2.2.2 2.2.3 2.3.0 2.3.1 2.3.2 2.3.3 All 91 releases
tier-pricing-table / src / Addons / GlobalTieredPricing / PricingService.php

PricingService.php in Tiered Pricing Table for WooCommerce 6.1.0, at src/Addons/GlobalTieredPricing/PricingService.php

148 lines 6.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace TierPricingTable\Addons\GlobalTieredPricing;
4
5 use TierPricingTable\PricingRule;
6 use TierPricingTable\Settings\Sections\GeneralSection\Subsections\CartOptionsSubsection;
7 class PricingService {
8 public function __construct() {
9 /**
10 * Main function to filter the tiered pricing rules
11 *
12 * @priority 30
13 */
14 add_filter(
15 'tiered_pricing_table/price/pricing_rule',
16 function ( PricingRule $pricingRule, $productId ) {
17 $product = wc_get_product( $productId );
18 if ( !$product ) {
19 return $pricingRule;
20 }
21 $globalPricingRule = GlobalPricingRulesRepository::getInstance()->getMatchedPricingRule( $product );
22 if ( !$globalPricingRule ) {
23 return $pricingRule;
24 }
25 $priority = $globalPricingRule->getSettings()->getPriorityType();
26 // If priority is set to default, use the global settings
27 if ( 'default' === $priority ) {
28 $priority = ( CartOptionsSubsection::globalRulesOverrideProductLevelRules() ? 'override' : 'prefer-product' );
29 }
30 $pricingRule->logPricingModification( '[global rule]: Matched global pricing rule #' . $globalPricingRule->getId() . ' with priority: ' . $priority );
31 $pricingRule = apply_filters(
32 'tiered_pricing_table/global_pricing/before_adjusting_pricing_rule',
33 $pricingRule,
34 $globalPricingRule,
35 $productId,
36 $priority
37 );
38 if ( 'flexible' === $priority ) {
39 $pricingRule = $this->addFlexibleGlobalPricing( $pricingRule, $globalPricingRule );
40 } elseif ( 'prefer-product' === $priority ) {
41 // Do not modify if there are pricing rules set (in product or role-based or category-based)
42 if ( !empty( $pricingRule->getRules() ) || 'role-based' === $pricingRule->provider ) {
43 $pricingRule->logPricingModification( '[global rule]: There is pricing at product-level - do not modify the rule' );
44 return $pricingRule;
45 } else {
46 $pricingRule = $this->addPricing( $pricingRule, $globalPricingRule );
47 $pricingRule->logPricingModification( '[global rule]: Pricing rule was fully overridden by global pricing rule data.' );
48 }
49 } else {
50 $pricingRule->logPricingModification( '[global rule]: Pricing rule was fully overridden by global pricing rule data.' );
51 $pricingRule = $this->addPricing( $pricingRule, $globalPricingRule );
52 }
53 return apply_filters(
54 'tiered_pricing_table/global_pricing/after_adjusting_pricing_rule',
55 $pricingRule,
56 $globalPricingRule,
57 $productId,
58 $priority
59 );
60 },
61 30,
62 2
63 );
64 }
65
66 public function addFlexibleGlobalPricing( PricingRule $pricingRule, GlobalPricingRule $globalPricingRule ) : PricingRule {
67 $updateRegularPricing = false;
68 $updateQuantityLimits = false;
69 $updateTieredPricing = false;
70 /**
71 * Regular prices
72 */
73 if ( $globalPricingRule->getSettings()->getRegularPricingPriority() === 'prefer-role-based-product' ) {
74 if ( $pricingRule->provider !== 'role-based' ) {
75 $updateRegularPricing = true;
76 }
77 } else {
78 $updateRegularPricing = true;
79 }
80 /**
81 * Quantity Limits
82 */
83 if ( $globalPricingRule->getSettings()->getQuantityLimitsPriority() === 'prefer-role-based-product' ) {
84 // Update only if there are no quantity limits set in the product or role-based
85 if ( $pricingRule->provider !== 'role-based' ) {
86 $updateQuantityLimits = true;
87 }
88 } elseif ( $globalPricingRule->getSettings()->getQuantityLimitsPriority() === 'prefer-product' ) {
89 // Update only if there are no quantity limits set in the product
90 if ( empty( $pricingRule->getMinimum() ) ) {
91 $updateQuantityLimits = true;
92 }
93 } else {
94 $updateQuantityLimits = true;
95 }
96 /**
97 * Tiered Pricing
98 */
99 if ( $globalPricingRule->getSettings()->getTieredPricingPriority() === 'prefer-role-based-product' ) {
100 if ( $pricingRule->provider !== 'role-based' ) {
101 $updateTieredPricing = true;
102 }
103 } elseif ( $globalPricingRule->getSettings()->getTieredPricingPriority() === 'prefer-product' ) {
104 if ( empty( $pricingRule->getRules() ) ) {
105 $updateTieredPricing = true;
106 }
107 } else {
108 $updateTieredPricing = true;
109 }
110 if ( $updateRegularPricing ) {
111 }
112 if ( $updateQuantityLimits ) {
113 }
114 if ( $updateTieredPricing ) {
115 $pricingRule->setRules( $globalPricingRule->getTieredPricingRules() );
116 $pricingRule->setType( $globalPricingRule->getTieredPricingType() );
117 $pricingRule->logPricingModification( '[global rule]: Tiered Pricing was updated by global rule' );
118 }
119 $pricingRule->provider = 'global-rules';
120 $pricingRule->providerData['rule_id'] = $globalPricingRule->getId();
121 // If tiered pricing is used from global rule or if mix and match is allowed for product-level tiered pricing
122 if ( $updateTieredPricing || $globalPricingRule->getSettings()->isAllowTieredPricingMixAndMatch() ) {
123 $pricingRule->providerData['applying_type'] = $globalPricingRule->getApplyingType();
124 } else {
125 $pricingRule->providerData['applying_type'] = 'individual';
126 }
127 return $pricingRule;
128 }
129
130 /**
131 * Main function to filter pricing rules with global pricing rule data
132 *
133 * @param PricingRule $pricingRule
134 * @param GlobalPricingRule $globalPricingRule
135 *
136 * @return PricingRule
137 */
138 public function addPricing( PricingRule $pricingRule, GlobalPricingRule $globalPricingRule ) : PricingRule {
139 $pricingRule->setRules( $globalPricingRule->getTieredPricingRules() );
140 $pricingRule->setType( $globalPricingRule->getTieredPricingType() );
141 $pricingRule->provider = 'global-rules';
142 $pricingRule->providerData['rule_id'] = $globalPricingRule->getId();
143 $pricingRule->providerData['applying_type'] = $globalPricingRule->getApplyingType();
144 return $pricingRule;
145 }
146
147 }
148