Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class WCML_Mix_and_Match_Products {
- public function __construct() {
- add_action( 'updated_post_meta', [ $this, 'sync_mnm_data' ], 10, 4 );
- add_filter( 'wcml_update_cart_contents_lang_switch', [ $this, 'sync_mnm_cart' ], 10, 3 );
- }
- public function sync_mnm_data( $meta_id, $post_id, $meta_key, $meta_value ) {
- if ( '_mnm_data' !== $meta_key ) {
- return;
- }
- global $sitepress, $woocommerce_wpml;
- $post = get_post( $post_id );
- // Skip auto-drafts, skip autosave.
- if ( 'auto-draft' === $post->post_status || isset( $_POST['autosave'] ) ) {
- return;
- }
- if ( 'product' === $post->post_type ) {
- remove_action( 'updated_post_meta', [ $this, 'sync_mnm_data' ], 10 );
- if ( $woocommerce_wpml->products->is_original_product( $post_id ) ) {
- $original_product_id = $post_id;
- } else {
- $original_product_id = $woocommerce_wpml->products->get_original_product_id( $post_id );
- }
- $mnm_data = maybe_unserialize( get_post_meta( $original_product_id, '_mnm_data', true ) );
- $product_trid = $sitepress->get_element_trid( $original_product_id, 'post_product' );
- $product_translations = $sitepress->get_element_translations( $product_trid, 'post_product' );
- foreach ( $product_translations as $product_translation ) {
- if ( empty( $product_translation->original ) ) {
- foreach ( $mnm_data as $key => $mnm_element ) {
- $trnsl_prod = apply_filters( 'translate_object_id', $key, 'product', true, $product_translation->language_code );
- $mnm_element['product_id'] = $trnsl_prod;
- $mnm_data[ $trnsl_prod ] = $mnm_element;
- unset( $mnm_data[ $key ] );
- }
- update_post_meta( $product_translation->element_id, '_mnm_data', $mnm_data );
- }
- }
- add_action( 'updated_post_meta', [ $this, 'sync_mnm_data' ], 10, 4 );
- }
- }
- /**
- * @param $cart_item array
- *
- * @param array $cart_item
- * @param string $new_key
- */
- public function sync_mnm_cart( $cart_contents, $key, $new_key ) {
- global $sitepress;
- $current_language = $sitepress->get_current_language();
- // Translate container.
- if ( wc_mnm_is_container_cart_item( $cart_contents[ $key ] ) ) {
- $new_config = array();
- // Translate config.
- foreach( $cart_contents[ $key ][ 'mnm_config'] as $id => $data ) {
- $tr_product_id = apply_filters( 'translate_object_id', $data['product_id'], 'product', false, $current_language );
- $tr_variation_id = 0;
- if ( isset( $data['variation_id'] ) && $data['variation_id'] ) {
- $tr_variation_id = apply_filters( 'translate_object_id', $data['variation_id'], 'product_variation', false, $current_language );
- }
- $tr_child_id = $tr_variation_id ? intval( $tr_variation_id ) : intval( $tr_product_id );
- $new_config[ $tr_child_id ] = array(
- 'mnm_child_id' => $tr_child_id,
- 'product_id' => intval( $tr_product_id ),
- 'variation_id' => intval( $tr_variation_id ),
- 'quantity' => $data[ 'quantity' ],
- 'variation' => $data[ 'variation' ], // @todo: translate attributes
- );
- }
- if ( ! empty( $new_config ) ) {
- $cart_contents[ $key ][ 'mnm_config'] = $new_config;
- }
- // Find all children and update with new container cart key.
- foreach ( wc_mnm_get_child_cart_items( $cart_contents[ $key ] ) as $child_key => $child_item ) {
- $cart_contents[ $child_key ][ 'mnm_container' ] = $new_key;
- }
- }
- // Translate children.
- if ( wc_mnm_maybe_is_child_cart_item( $cart_contents[ $key ] ) ) {
- $container_key = wc_mnm_get_cart_item_container( $cart_contents[ $key ], false, true );
- if ( $container_key ) {
- unset( $cart_contents[ $container_key ][ 'mnm_contents'][ $key ] );
- $cart_contents[ $container_key ][ 'mnm_contents'][] = $new_key;
- }
- }
- return $cart_contents;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement