Advertisement
helgatheviki

class-wcml-mix-and-match-products.php

Jul 27th, 2021 (edited)
1,130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.89 KB | None | 0 0
  1. <?php
  2.  
  3. class WCML_Mix_and_Match_Products {
  4.  
  5.     public function __construct() {
  6.         add_action( 'updated_post_meta', [ $this, 'sync_mnm_data' ], 10, 4 );
  7.         add_filter( 'wcml_update_cart_contents_lang_switch', [ $this, 'sync_mnm_cart' ], 10, 3 );
  8.     }
  9.  
  10.     public function sync_mnm_data( $meta_id, $post_id, $meta_key, $meta_value ) {
  11.         if ( '_mnm_data' !== $meta_key ) {
  12.             return;
  13.         }
  14.  
  15.         global $sitepress, $woocommerce_wpml;
  16.  
  17.         $post = get_post( $post_id );
  18.  
  19.         // Skip auto-drafts, skip autosave.
  20.         if ( 'auto-draft' === $post->post_status || isset( $_POST['autosave'] ) ) {
  21.             return;
  22.         }
  23.  
  24.         if ( 'product' === $post->post_type ) {
  25.             remove_action( 'updated_post_meta', [ $this, 'sync_mnm_data' ], 10 );
  26.  
  27.             if ( $woocommerce_wpml->products->is_original_product( $post_id ) ) {
  28.                 $original_product_id = $post_id;
  29.             } else {
  30.                 $original_product_id = $woocommerce_wpml->products->get_original_product_id( $post_id );
  31.             }
  32.  
  33.             $mnm_data             = maybe_unserialize( get_post_meta( $original_product_id, '_mnm_data', true ) );
  34.             $product_trid         = $sitepress->get_element_trid( $original_product_id, 'post_product' );
  35.             $product_translations = $sitepress->get_element_translations( $product_trid, 'post_product' );
  36.  
  37.             foreach ( $product_translations as $product_translation ) {
  38.                 if ( empty( $product_translation->original ) ) {
  39.                     foreach ( $mnm_data as $key => $mnm_element ) {
  40.  
  41.                         $trnsl_prod                = apply_filters( 'translate_object_id', $key, 'product', true, $product_translation->language_code );
  42.                         $mnm_element['product_id'] = $trnsl_prod;
  43.                         $mnm_data[ $trnsl_prod ]   = $mnm_element;
  44.                         unset( $mnm_data[ $key ] );
  45.                     }
  46.  
  47.                     update_post_meta( $product_translation->element_id, '_mnm_data', $mnm_data );
  48.                 }
  49.             }
  50.  
  51.             add_action( 'updated_post_meta', [ $this, 'sync_mnm_data' ], 10, 4 );
  52.         }
  53.     }
  54.    
  55.     /**
  56.      * @param $cart_item array
  57.      *
  58.      * @param array $cart_item
  59.      * @param string $new_key
  60.      */
  61.     public function sync_mnm_cart( $cart_contents, $key, $new_key ) {
  62.        
  63.         global $sitepress;
  64.        
  65.         $current_language = $sitepress->get_current_language();
  66.  
  67.         // Translate container.
  68.         if ( wc_mnm_is_container_cart_item( $cart_contents[ $key ] ) ) {
  69.            
  70.             $new_config = array();
  71.            
  72.             // Translate config.
  73.             foreach( $cart_contents[ $key ][ 'mnm_config'] as $id => $data ) {
  74.                
  75.                 $tr_product_id   = apply_filters( 'translate_object_id', $data['product_id'], 'product', false, $current_language );
  76.                 $tr_variation_id = 0;
  77.                
  78.                 if ( isset( $data['variation_id'] ) && $data['variation_id'] ) {
  79.                     $tr_variation_id = apply_filters( 'translate_object_id', $data['variation_id'], 'product_variation', false, $current_language );
  80.                 }
  81.                
  82.                 $tr_child_id = $tr_variation_id ? intval( $tr_variation_id ) : intval( $tr_product_id );
  83.                
  84.                 $new_config[ $tr_child_id ] = array(
  85.                     'mnm_child_id' => $tr_child_id,
  86.                     'product_id'   => intval( $tr_product_id ),
  87.                     'variation_id' => intval( $tr_variation_id ),
  88.                     'quantity'     => $data[ 'quantity' ],
  89.                     'variation'    => $data[ 'variation' ], // @todo: translate attributes
  90.                 ); 
  91.                
  92.             }
  93.            
  94.             if ( ! empty( $new_config ) ) {
  95.                 $cart_contents[ $key ][ 'mnm_config'] = $new_config;
  96.             }
  97.            
  98.             // Find all children and update with new container cart key.
  99.             foreach ( wc_mnm_get_child_cart_items( $cart_contents[ $key ] ) as $child_key => $child_item ) {
  100.                 $cart_contents[ $child_key ][ 'mnm_container' ] = $new_key;
  101.             }              
  102.                
  103.         }
  104.            
  105.         // Translate children.
  106.         if ( wc_mnm_maybe_is_child_cart_item( $cart_contents[ $key ] ) ) {
  107.            
  108.             $container_key = wc_mnm_get_cart_item_container( $cart_contents[ $key ], false, true );
  109.            
  110.             if ( $container_key ) {
  111.                 unset( $cart_contents[ $container_key ][ 'mnm_contents'][ $key ] );
  112.                 $cart_contents[ $container_key ][ 'mnm_contents'][] = $new_key;
  113.             }
  114.            
  115.         }
  116.    
  117.         return $cart_contents;
  118.        
  119.     }
  120.    
  121. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement