PricingRulesDispatcher.php
3.43 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
<?php namespace MeowCrew\RoleAndCustomerBasedPricing;
use MeowCrew\RoleAndCustomerBasedPricing\Admin\ProductPage\PricingRulesManager;
use \MeowCrew\RoleAndCustomerBasedPricing\Entity\PricingRule;
use MeowCrew\RoleAndCustomerBasedPricing\GlobalRoleSpecificPricing\CPT\RoleSpecificPricingCPT;
use WP_User;
class PricingRulesDispatcher {
/**
* Dispatched rules. Used for the cache
*
* @var PricingRule[]
*/
protected static $dispatchedRules = array();
/**
* Wrapper for the main dispatch function to provide the hook for 3rd-party devs
*
* @param int $productId
* @param null $parentId
* @param null $user
* @param bool $validatePricing
*
* @return false|PricingRule
*/
public static function dispatchRule( $productId, $parentId = null, $user = null, $validatePricing = true ) {
$dispatchedRule = self::_dispatchRule( $productId, $parentId, $user, $validatePricing );
return apply_filters( 'role_customer_specific_pricing/pricing_rules_dispatcher/dispatched_rule', $dispatchedRule, $productId, $parentId, $user, $validatePricing, self::$dispatchedRules );
}
/**
* The main method to get applied rule for a product
*
* @param int $productId
* @param null $parentId
* @param null $user
* @param bool $validatePricing
*
* @return false|PricingRule
*/
protected static function _dispatchRule( $productId, $parentId = null, $user = null, $validatePricing = true ) {
$cacheKey = $productId;
// Cache
if ( array_key_exists( $productId, self::$dispatchedRules ) ) {
return self::$dispatchedRules[ $cacheKey ];
}
$product = wc_get_product( $productId );
if ( ! $product || ! $product->is_type( array(
'variation',
'simple',
'subscription',
'subscription-variation'
) ) ) {
return false;
}
$parentId = $parentId ? $parentId : $product->get_parent_id();
$user = $user instanceof WP_User ? $user : wp_get_current_user();
if ( ! $user ) {
$user = new WP_User( 0 );
}
$customerSpecificRules = PricingRulesManager::getProductCustomerSpecificPricingRules( $productId, $validatePricing );
if ( empty( $customerSpecificRules ) && $product->get_type() === 'variation' ) {
$customerSpecificRules = PricingRulesManager::getProductCustomerSpecificPricingRules( $parentId, $validatePricing );
}
foreach ( $customerSpecificRules as $userId => $rule ) {
if ( intval( $userId ) === $user->ID ) {
self::$dispatchedRules[ $cacheKey ] = $rule;
return $rule;
}
}
$roleSpecificRules = PricingRulesManager::getProductRoleSpecificPricingRules( $productId, $validatePricing );
if ( empty( $roleSpecificRules ) && $product->get_type() === 'variation' ) {
$roleSpecificRules = PricingRulesManager::getProductRoleSpecificPricingRules( $parentId, $validatePricing );
}
foreach ( $roleSpecificRules as $role => $rule ) {
if ( in_array( $role, $user->roles ) ) {
self::$dispatchedRules[ $cacheKey ] = $rule;
return $rule;
}
}
// role-and-customer-based-pricing-for-woocommerce: make it as a generator to save performance
$globalRules = RoleSpecificPricingCPT::getGlobalRules( $validatePricing );
foreach ( $globalRules as $rule ) {
if ( $rule->matchRequirements( $user, $product ) ) {
$rule->setAppliedProductId( $productId );
$rule->setOriginalProductPrice( floatval( $product->get_price( 'edit' ) ) );
self::$dispatchedRules[ $cacheKey ] = $rule;
return $rule;
}
}
self::$dispatchedRules[ $cacheKey ] = false;
return false;
}
}