ProductPricingService.php
3.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
<?php namespace MeowCrew\RoleAndCustomerBasedPricing\Services;
use MeowCrew\RoleAndCustomerBasedPricing\Core\ServiceContainerTrait;
use MeowCrew\RoleAndCustomerBasedPricing\PricingRulesDispatcher;
use WC_Product;
class ProductPricingService {
use ServiceContainerTrait;
protected $priceCache = array(
'price' => array(),
'sale_price' => array(),
'regular_price' => array(),
);
public function __construct() {
add_filter( 'woocommerce_product_get_regular_price', array(
$this,
'adjustRegularPrice'
), 99, 2 );
add_filter( 'woocommerce_product_get_sale_price', array(
$this,
'adjustSalePrice'
), 99, 2 );
add_filter( 'woocommerce_product_get_price', array(
$this,
'adjustPrice'
), 99, 2 );
// Variations
add_filter( 'woocommerce_product_variation_get_regular_price', array(
$this,
'adjustRegularPrice'
), 99, 2 );
add_filter( 'woocommerce_product_variation_get_sale_price', array(
$this,
'adjustSalePrice'
), 99, 2 );
add_filter( 'woocommerce_product_variation_get_price', array(
$this,
'adjustPrice'
), 99, 2 );
// Variable (price range)
add_filter( 'woocommerce_variation_prices_price', array( $this, 'adjustPrice' ), 99, 3 );
// Variation
add_filter( 'woocommerce_variation_prices_regular_price', array(
$this,
'adjustRegularPrice'
), 99, 3 );
add_filter( 'woocommerce_variation_prices_sale_price', array(
$this,
'adjustSalePrice'
), 99, 3 );
// Price caching
add_filter( 'woocommerce_get_variation_prices_hash', function ( $hash, \WC_Product_Variable $product, $forDisplay ) {
$user = wp_get_current_user();
$hash[] = json_encode( $product->get_category_ids() );
if ( $user ) {
$hash[] = md5( json_encode( $user->roles ) );
$hash[] = $user->ID;
}
return $hash;
}, 99, 3 );
}
public function adjustPrice( $price, WC_Product $product ) {
$parentId = null;
if ( array_key_exists( $product->get_id(), $this->priceCache['price'] ) ) {
return $this->priceCache['price'][ $product->get_id() ];
}
$pricingRule = PricingRulesDispatcher::dispatchRule( $product->get_id() );
if ( $pricingRule ) {
$price = $pricingRule->getPrice();
}
$this->priceCache['price'][ $product->get_id() ] = $price;
return $price;
}
public function adjustSalePrice( $price, WC_Product $product ) {
if ( array_key_exists( $product->get_id(), $this->priceCache['sale_price'] ) ) {
return $this->priceCache['sale_price'][ $product->get_id() ];
}
$pricingRule = PricingRulesDispatcher::dispatchRule( $product->get_id() );
if ( $pricingRule ) {
if ( $pricingRule->getPriceType() === 'flat' && $pricingRule->getSalePrice() ) {
$price = $pricingRule->getSalePrice();
} else {
$price = $pricingRule->getPrice();
}
}
$this->priceCache['sale_price'][ $product->get_id() ] = $price;
return $price;
}
public function adjustRegularPrice( $price, WC_Product $product ) {
if ( array_key_exists( $product->get_id(), $this->priceCache['regular_price'] ) ) {
return $this->priceCache['regular_price'][ $product->get_id() ];
}
$pricingRule = PricingRulesDispatcher::dispatchRule( $product->get_id() );
if ( $pricingRule ) {
if ( $pricingRule->getPriceType() === 'flat' && $pricingRule->getRegularPrice() ) {
$price = $pricingRule->getRegularPrice();
} else if ( $pricingRule->getPriceType() !== 'percentage' || $this->getContainer()->getSettings()->getPercentageBasedRulesBehavior() !== 'sale_price' ) {
// Do no modify regular price if "sale_price" chosen
$price = $pricingRule->getPrice();
}
}
$this->priceCache['regular_price'][ $product->get_id() ] = $price;
return $price;
}
}