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.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 / WcMixAndMatch / class-wcml-mix-and-match-products.php
woocommerce-multilingual / compatibility / WcMixAndMatch Last commit date
Factory.php 1 month ago MulticurrencyHooks.php 1 month ago class-wcml-mix-and-match-products.php 1 month ago
class-wcml-mix-and-match-products.php
178 lines
1 <?php
2 class WCML_Mix_And_Match_Products implements \IWPML_Action {
3
4 private $sitepress;
5
6 public function __construct( SitePress $sitepress ) {
7 $this->sitepress = $sitepress;
8 }
9
10 public function add_hooks() {
11 if ( is_callable( [ 'WC_MNM_Compatibility', 'is_db_version_gte' ] ) && WC_MNM_Compatibility::is_db_version_gte( '2.0' ) ) {
12 add_action( 'wcml_after_sync_product_data', [ $this, 'sync_allowed_contents' ], 10, 2 );
13 add_filter( 'wcml_cart_contents', [ $this, 'sync_cart' ], 10, 4 );
14 } else {
15 add_action( 'updated_post_meta', [ $this, 'sync_mnm_data' ], 10, 4 );
16 }
17 }
18
19 public function sync_allowed_contents( $container_id, $translated_container_id ) {
20
21 if ( has_term( 'mix-and-match', 'product_type', $container_id ) ) {
22
23 $lang = $this->sitepress->get_language_for_element( $translated_container_id, 'post_product' );
24
25 $original_product = wc_get_product( $container_id );
26
27 $translated_product = wc_get_product( $translated_container_id );
28
29 if ( $original_product instanceof WC_Product_Mix_and_Match) {
30
31 $original_child_items = $original_product->get_child_items( 'edit' );
32
33 $translated_child_items = [];
34
35 if ( ! empty( $original_child_items ) ) {
36
37 foreach ( $original_child_items as $item_key => $original_child_item ) {
38
39 $translated_child_items[] = [
40 'product_id' => apply_filters( 'wpml_object_id', $original_child_item->get_product_id(), 'product', false, $lang ),
41 'variation_id' => apply_filters( 'wpml_object_id', $original_child_item->get_variation_id(), 'product_variation', false, $lang ),
42 ];
43
44 }
45 }
46
47 if ( ( $translated_product instanceof WC_Product_Mix_and_Match ) && ! empty( $translated_child_items ) ) {
48 $translated_product->set_child_items( $translated_child_items );
49 }
50
51 $original_child_cat_ids = $original_product->get_child_category_ids( 'edit' );
52 $translated_child_cat_ids = [];
53
54 foreach ( $original_child_cat_ids as $original_cat_id ) {
55 $translated_child_cat_ids[] = apply_filters( 'wpml_object_id', $original_cat_id, 'product_cat', true, $lang );
56 }
57
58 if ( ( $translated_product instanceof WC_Product_Mix_and_Match ) && ! empty( $translated_child_cat_ids ) ) {
59 $translated_product->set_child_category_ids( $translated_child_cat_ids );
60 }
61
62 $translated_product->save();
63
64 }
65 }
66
67 }
68
69 public function sync_cart( $new_cart_contents, $cart_contents, $key, $new_key ) {
70 if ( ! function_exists( 'wc_mnm_is_container_cart_item' )
71 || ! function_exists( 'wc_mnm_get_child_cart_items' )
72 || ! function_exists( 'wc_mnm_maybe_is_child_cart_item' )
73 || ! function_exists( 'wc_mnm_get_cart_item_container' )
74 ) {
75 return $new_cart_contents;
76 }
77
78 $current_language = $this->sitepress->get_current_language();
79
80 if ( wc_mnm_is_container_cart_item( $new_cart_contents[ $new_key ] ) ) {
81
82 $new_config = [];
83
84 foreach ( $new_cart_contents[ $new_key ]['mnm_config'] as $id => $data ) {
85
86 $tr_product_id = apply_filters( 'wpml_object_id', $data['product_id'], 'product', false, $current_language );
87 $tr_variation_id = 0;
88
89 if ( isset( $data['variation_id'] ) && $data['variation_id'] ) {
90 $tr_variation_id = apply_filters( 'wpml_object_id', $data['variation_id'], 'product_variation', false, $current_language );
91 }
92
93 $tr_child_id = $tr_variation_id ? intval( $tr_variation_id ) : intval( $tr_product_id );
94
95 $new_config[ $tr_child_id ] = [
96 'mnm_child_id' => $tr_child_id,
97 'product_id' => intval( $tr_product_id ),
98 'variation_id' => intval( $tr_variation_id ),
99 'quantity' => $data['quantity'],
100 'variation' => $data['variation'],
101 ];
102
103 }
104
105 if ( ! empty( $new_config ) ) {
106 $new_cart_contents[ $new_key ]['mnm_config'] = $new_config;
107 }
108
109 foreach ( wc_mnm_get_child_cart_items( $new_cart_contents[ $new_key ] ) as $child_key => $child_item ) {
110 WC()->cart->cart_contents[ $child_key ]['translated_mnm_container'] = $new_key;
111 }
112 }
113
114 if ( wc_mnm_maybe_is_child_cart_item( $new_cart_contents[ $new_key ] ) ) {
115
116 $new_cart_contents[ $new_key ]['mnm_container'] = $cart_contents[ $key ]['translated_mnm_container'];
117 unset( $cart_contents[ $key ]['translated_mnm_container'] );
118
119 $container_key = wc_mnm_get_cart_item_container( $new_cart_contents[ $new_key ], $new_cart_contents, true );
120
121 if ( $container_key ) {
122
123 $remove_key = array_search( $key, $new_cart_contents[ $container_key ]['mnm_contents'] );
124 unset( $new_cart_contents[ $container_key ]['mnm_contents'][ $remove_key ] );
125 $new_cart_contents[ $container_key ]['mnm_contents'][] = $new_key;
126
127 }
128 }
129
130 return $new_cart_contents;
131 }
132
133 public function sync_mnm_data( $meta_id, $post_id, $meta_key, $meta_value ) {
134 if ( '_mnm_data' !== $meta_key ) {
135 return;
136 }
137
138 global $sitepress, $woocommerce_wpml;
139
140 $post = get_post( $post_id );
141
142 if ( 'auto-draft' === $post->post_status || isset( $_POST['autosave'] ) ) {
143 return;
144 }
145
146 if ( 'product' === $post->post_type ) {
147 remove_action( 'updated_post_meta', [ $this, 'sync_mnm_data' ], 10 );
148
149 if ( $woocommerce_wpml->products->is_original_product( $post_id ) ) {
150 $original_product_id = $post_id;
151 } else {
152 $original_product_id = $woocommerce_wpml->products->get_original_product_id( $post_id );
153 }
154
155 $mnm_data = maybe_unserialize( get_post_meta( $original_product_id, '_mnm_data', true ) );
156 $product_trid = $sitepress->get_element_trid( $original_product_id, 'post_product' );
157 $product_translations = $sitepress->get_element_translations( $product_trid, 'post_product' );
158
159 foreach ( $product_translations as $product_translation ) {
160 if ( empty( $product_translation->original ) ) {
161 foreach ( $mnm_data as $key => $mnm_element ) {
162
163 $trnsl_prod = apply_filters( 'wpml_object_id', $key, 'product', true, $product_translation->language_code );
164 $mnm_element['product_id'] = $trnsl_prod;
165 $mnm_data[ $trnsl_prod ] = $mnm_element;
166 unset( $mnm_data[ $key ] );
167 }
168
169 update_post_meta( $product_translation->element_id, '_mnm_data', $mnm_data );
170 }
171 }
172
173 add_action( 'updated_post_meta', [ $this, 'sync_mnm_data' ], 10, 4 );
174 }
175 }
176
177 }
178