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 |