PluginProbe
Tiered Pricing Table for WooCommerce / 6.4.0
Tiered Pricing Table for WooCommerce v6.4.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.4.0, at src/Addons/GlobalTieredPricing/PricingService.php

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