PluginProbe
Tiered Pricing Table for WooCommerce / 7.1.5
Tiered Pricing Table for WooCommerce v7.1.5
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 7.1.5, at src/Addons/GlobalTieredPricing/PricingService.php

152 lines 6.8 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 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 $updateTieredPricing = false;
69 /**
70 * Regular prices
71 */
72 if ( $globalPricingRule->getSettings()->getRegularPricingPriority() === 'prefer-role-based-product' ) {
73 if ( $pricingRule->provider !== 'role-based' ) {
74 $updateRegularPricing = true;
75 }
76 } else {
77 $updateRegularPricing = true;
78 }
79 /**
80 * Quantity Limits
81 */
82 $updateMinimum = false;
83 $updateMixAndMatch = false;
84 if ( $globalPricingRule->getSettings()->getQuantityLimitsPriority() === 'prefer-role-based-product' ) {
85 if ( $pricingRule->provider !== 'role-based' ) {
86 $updateMinimum = true;
87 $updateMixAndMatch = true;
88 }
89 } elseif ( $globalPricingRule->getSettings()->getQuantityLimitsPriority() === 'prefer-product' ) {
90 if ( empty( $pricingRule->getMinimum() ) ) {
91 $updateMinimum = true;
92 }
93 if ( get_post_meta( $pricingRule->getProductId(), '_tiered_price_mix_and_match_minimum', true ) === '' ) {
94 $updateMixAndMatch = true;
95 }
96 } else {
97 $updateMinimum = true;
98 $updateMixAndMatch = true;
99 }
100 /**
101 * Tiered Pricing
102 */
103 if ( $globalPricingRule->getSettings()->getTieredPricingPriority() === 'prefer-role-based-product' ) {
104 if ( $pricingRule->provider !== 'role-based' ) {
105 $updateTieredPricing = true;
106 }
107 } elseif ( $globalPricingRule->getSettings()->getTieredPricingPriority() === 'prefer-product' ) {
108 if ( empty( $pricingRule->getRules() ) ) {
109 $updateTieredPricing = true;
110 }
111 } else {
112 $updateTieredPricing = true;
113 }
114 if ( $updateRegularPricing ) {
115 }
116 if ( $updateMinimum || $updateMixAndMatch ) {
117 }
118 if ( $updateTieredPricing ) {
119 $pricingRule->setRules( $globalPricingRule->getTieredPricingRules() );
120 $pricingRule->setType( $globalPricingRule->getTieredPricingType() );
121 $pricingRule->logPricingModification( '[global rule]: Tiered Pricing was updated by global rule' );
122 }
123 $pricingRule->provider = 'global-rules';
124 $pricingRule->providerData['rule_id'] = $globalPricingRule->getId();
125 // If tiered pricing is used from global rule or if mix and match is allowed for product-level tiered pricing
126 if ( $updateTieredPricing || $globalPricingRule->getSettings()->isAllowTieredPricingMixAndMatch() ) {
127 $pricingRule->providerData['applying_type'] = $globalPricingRule->getApplyingType();
128 } else {
129 $pricingRule->providerData['applying_type'] = 'individual';
130 }
131 return $pricingRule;
132 }
133
134 /**
135 * Main function to filter pricing rules with global pricing rule data
136 *
137 * @param PricingRule $pricingRule
138 * @param GlobalPricingRule $globalPricingRule
139 *
140 * @return PricingRule
141 */
142 public function addPricing( PricingRule $pricingRule, GlobalPricingRule $globalPricingRule ) : PricingRule {
143 $pricingRule->setRules( $globalPricingRule->getTieredPricingRules() );
144 $pricingRule->setType( $globalPricingRule->getTieredPricingType() );
145 $pricingRule->provider = 'global-rules';
146 $pricingRule->providerData['rule_id'] = $globalPricingRule->getId();
147 $pricingRule->providerData['applying_type'] = $globalPricingRule->getApplyingType();
148 return $pricingRule;
149 }
150
151 }
152