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 4.7.6 4.7.7 4.7.8 4.7.9 4.8.0 4.9.0 4.9.1 5.0.1 5.0.2 5.1.1 5.1.2 5.1.3 5.2.0 5.2.1 5.3.2 5.3.3.1 5.3.4 5.3.5 5.3.6 5.3.7
woocommerce-multilingual / compatibility / WcDynamicPricing / class-wcml-dynamic-pricing.php
woocommerce-multilingual / compatibility / WcDynamicPricing Last commit date
Factory.php 4 weeks ago MulticurrencyHooks.php 4 weeks ago class-wcml-dynamic-pricing.php 4 weeks ago
class-wcml-dynamic-pricing.php
154 lines
1 <?php
2
3 use WPML\FP\Obj;
4
5 class WCML_Dynamic_Pricing implements \IWPML_Action {
6
7 private $sitepress;
8
9 public function __construct( SitePress $sitepress ) {
10 $this->sitepress = $sitepress;
11 }
12
13 public function add_hooks() {
14 if ( ! is_admin() ) {
15 add_filter( 'woocommerce_dynamic_pricing_is_object_in_terms', [ $this, 'is_object_in_translated_terms' ], 10, 3 );
16 add_filter( 'wc_dynamic_pricing_load_modules', [ $this, 'translate_collector_args' ] );
17 add_filter( 'woocommerce_dynamic_pricing_is_applied_to', [ $this, 'woocommerce_dynamic_pricing_is_applied_to' ], 10, 5 );
18 } else {
19 $this->hide_language_switcher_for_settings_page();
20 }
21 add_filter( 'woocommerce_product_get__pricing_rules', [ $this, 'translate_variations_in_rules' ] );
22 }
23
24 public function translate_collector_args( $modules ) {
25 foreach ( $modules as $mod_key => $module ) {
26 if ( isset( $module->available_advanced_rulesets ) ) {
27 foreach ( $module->available_advanced_rulesets as $rule_key => $rule ) {
28 $taxonomy = $this->get_taxonomy( $rule );
29 if ( ! empty( $rule['targets'] ) ) {
30 $modules[ $mod_key ]->available_advanced_rulesets[ $rule_key ]['targets'] = $this->adjust_cat_ids( $rule['targets'], $taxonomy );
31 }
32 if ( ! empty( $rule['collector']['args']['cats'] ) ) {
33 $modules[ $mod_key ]->available_advanced_rulesets[ $rule_key ]['collector']['args']['cats'] = $this->adjust_cat_ids( $rule['collector']['args']['cats'], $taxonomy );
34 }
35 }
36 }
37 }
38
39 return $modules;
40 }
41
42 public function is_object_in_translated_terms( $result, $product_id, $categories ) {
43 foreach ( $categories as &$cat_id ) {
44 $cat_id = apply_filters( 'wpml_object_id', $cat_id, 'product_cat', true );
45 }
46
47 $product_id = apply_filters( 'wpml_object_id', $product_id, 'product', true );
48
49 return is_object_in_term( $product_id, 'product_cat', $categories );
50 }
51
52 public function woocommerce_dynamic_pricing_is_applied_to( $process_discounts, WC_Product $_product, $module_id, $dynamic_pricing, $cat_ids ) {
53 if ( ! $cat_ids || ! $this->has_requirements( $dynamic_pricing ) ) {
54 return $process_discounts;
55 }
56 $taxonomy = $this->get_taxonomy( $dynamic_pricing );
57 $product_id = 'product_variation' === Obj::prop( 'post_type', $_product )
58 ? $_product->get_parent_id()
59 : $_product->get_id();
60
61 $product_id = apply_filters( 'wpml_object_id', $product_id, 'product', true );
62
63 return is_object_in_term( $product_id, $taxonomy, $this->adjust_cat_ids( $cat_ids, $taxonomy ) );
64 }
65
66 private function get_taxonomy( $dynamic_pricing ): string {
67 $taxonomy = 'product_cat';
68 if ( $dynamic_pricing instanceof WC_Dynamic_Pricing_Simple_Taxonomy || $dynamic_pricing instanceof WC_Dynamic_Pricing_Advanced_Taxonomy ) {
69 $taxonomy = $dynamic_pricing->taxonomy;
70 }
71
72 return $taxonomy;
73 }
74
75 private function has_requirements( $dynamic_pricing ) {
76 $requirements = [
77 'WC_Dynamic_Pricing_Advanced_Category' => [
78 'adjustment_sets',
79 ],
80 'WC_Dynamic_Pricing_Advanced_Taxonomy' => [
81 'adjustment_sets',
82 ],
83 'WC_Dynamic_Pricing_Advanced_Totals' => [
84 'adjustment_sets',
85 ],
86 'WC_Dynamic_Pricing_Simple_Membership' => [
87 'available_rulesets',
88 ],
89 'WC_Dynamic_Pricing_Simple_Category' => [
90 'available_rulesets',
91 ],
92 'WC_Dynamic_Pricing_Simple_Taxonomy' => [
93 'available_rulesets',
94 ],
95 'WC_Dynamic_Pricing_Simple_Base' => [
96 'available_rulesets',
97 ],
98 ];
99
100 $class_name = wpml_collect( array_keys( $requirements ) )
101 ->first(
102 function ( $class_name ) use ( $dynamic_pricing ) {
103 return get_class( $dynamic_pricing ) === $class_name || is_subclass_of( $dynamic_pricing, $class_name );
104 }
105 );
106
107 if ( $class_name ) {
108 return (bool) wpml_collect( $requirements[ $class_name ] )
109 ->filter(
110 function ( $property ) use ( $dynamic_pricing ) {
111 return isset( $dynamic_pricing->$property ) && $dynamic_pricing->$property;
112 }
113 )
114 ->count();
115 }
116
117 return false;
118 }
119
120 private function adjust_cat_ids( $cat_ids, $taxonomy ) {
121 if ( ! is_array( $cat_ids ) ) {
122 $cat_ids = [ $cat_ids ];
123 }
124
125 return array_map(
126 function ( $cat_id ) use ( $taxonomy ) {
127 return apply_filters( 'wpml_object_id', $cat_id, $taxonomy, true );
128 },
129 $cat_ids
130 );
131 }
132
133 public function translate_variations_in_rules( $rules ) {
134 if ( is_array( $rules ) ) {
135 foreach ( $rules as $r_key => $rule ) {
136 if ( isset( $rule['variation_rules']['args']['variations'] ) ) {
137 foreach ( $rule['variation_rules']['args']['variations'] as $i => $variation_id ) {
138 $rules[ $r_key ]['variation_rules']['args']['variations'][ $i ] = apply_filters( 'wpml_object_id', $variation_id, 'product_variation', true );
139 }
140 }
141 }
142 }
143
144 return $rules;
145 }
146
147 public function hide_language_switcher_for_settings_page() {
148 if ( 'wc_dynamic_pricing' === filter_input( INPUT_GET, 'page' ) ) {
149 remove_action( 'wp_before_admin_bar_render', [ $this->sitepress, 'admin_language_switcher' ] );
150 }
151 }
152
153 }
154