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 |