Advertisement
helgatheviki

class-wcml-mix-and-match.php

Jul 28th, 2021
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.23 KB | None | 0 0
  1. <?php
  2.  
  3. class WCML_Mix_and_Match_Products {
  4.  
  5. public function __construct() {
  6. add_action( 'wcml_after_duplicate_product_post_meta', [ $this, 'sync_mnm_data' ], 10, 2 );
  7. add_filter( 'wcml_cart_contents', [ $this, 'sync_mnm_cart' ], 10, 4 );
  8. }
  9.  
  10. /**
  11. * Sync container data with translated values when the product is duplicated.
  12. *
  13. * @param int $container_id
  14. * @param int $translated_container_id
  15. */
  16. public function sync_mnm_data( $container_id, $translated_container_id ) {
  17. global $sitepress;
  18.  
  19. $original_data = maybe_unserialize( get_post_meta( $container_id, '_mnm_data', true ) );
  20. $translated_data = [];
  21. $lang = $sitepress->get_language_for_element( $translated_container_id, 'post_product' );
  22.  
  23. if ( $original_data ) {
  24. foreach ( $original_data as $original_id => $data ) {
  25.  
  26. if ( is_array( $data ) ) {
  27.  
  28. $translated_child_id = $translated_product_id = apply_filters( 'translate_object_id', $data[ 'product_id' ] , 'product', false, $lang );
  29. $translated_variation_id = 0;
  30.  
  31. if ( ! empty( $data[ 'variation_id' ] ) ) {
  32. $translated_child_id = $translated_variation_id = apply_filters( 'translate_object_id', $data[ 'product_id' ] , 'product_variation', false, $lang );
  33. }
  34.  
  35. $translated_data[ $translated_child_id ] = array(
  36. 'child_id' => $translated_child_id,
  37. 'product_id' => $translated_product_id,
  38. 'variation_id' => $translated_variation_id,
  39. );
  40.  
  41. }
  42.  
  43. }
  44. }
  45.  
  46. update_post_meta( $translated_container_id, '_mnm_data', $translated_data );
  47. }
  48.  
  49. /**
  50. * @param $cart_item array
  51. *
  52. * @param array $cart_item
  53. * @param string $new_key
  54. */
  55. public function sync_mnm_cart( $new_cart_contents, $cart_contents, $key, $new_key ) {
  56.  
  57. global $sitepress;
  58.  
  59. $current_language = $sitepress->get_current_language();
  60.  
  61. // Translate container.
  62. if ( wc_mnm_is_container_cart_item( $new_cart_contents[ $new_key ] ) ) {
  63.  
  64. $new_config = array();
  65.  
  66. // Translate config.
  67. foreach( $new_cart_contents[ $new_key ][ 'mnm_config'] as $id => $data ) {
  68.  
  69. $tr_product_id = apply_filters( 'translate_object_id', $data['product_id'], 'product', false, $current_language );
  70. $tr_variation_id = 0;
  71.  
  72. if ( isset( $data['variation_id'] ) && $data['variation_id'] ) {
  73. $tr_variation_id = apply_filters( 'translate_object_id', $data['variation_id'], 'product_variation', false, $current_language );
  74. }
  75.  
  76. $tr_child_id = $tr_variation_id ? intval( $tr_variation_id ) : intval( $tr_product_id );
  77.  
  78. $new_config[ $tr_child_id ] = array(
  79. 'mnm_child_id' => $tr_child_id,
  80. 'product_id' => intval( $tr_product_id ),
  81. 'variation_id' => intval( $tr_variation_id ),
  82. 'quantity' => $data[ 'quantity' ],
  83. 'variation' => $data[ 'variation' ], // @todo: translate attributes
  84. );
  85.  
  86. }
  87.  
  88. if ( ! empty( $new_config ) ) {
  89. $new_cart_contents[ $new_key ][ 'mnm_config'] = $new_config;
  90. }
  91.  
  92. // Find all children and stash new container cart key. Need to direclty manipulate the wc()->cart as $cart_contents isn't persisted.
  93. foreach ( wc_mnm_get_child_cart_items( $new_cart_contents[ $new_key ] ) as $child_key => $child_item ) {
  94. wc()->cart->cart_contents[ $child_key ][ 'translated_mnm_container' ] = $new_key;
  95. }
  96.  
  97. }
  98.  
  99. // Translate children.
  100. if ( wc_mnm_maybe_is_child_cart_item( $new_cart_contents[ $new_key ] ) ) {
  101.  
  102. // Update the child's container and remove the stashed version.
  103. $new_cart_contents[ $new_key ][ 'mnm_container'] = $cart_contents[ $key ][ 'translated_mnm_container' ];
  104. unset( $cart_contents[ $key ][ 'translated_mnm_container'] );
  105.  
  106. $container_key = wc_mnm_get_cart_item_container( $new_cart_contents[ $new_key ], $new_cart_contents, true );
  107.  
  108. if ( $container_key ) {
  109.  
  110. // Swap keys in container's content array.
  111. $remove_key = array_search( $key, $new_cart_contents[ $container_key ][ 'mnm_contents'] );
  112. unset( $new_cart_contents[ $container_key ][ 'mnm_contents'][ $remove_key ] );
  113. $new_cart_contents[ $container_key ][ 'mnm_contents'][] = $new_key;
  114.  
  115. }
  116.  
  117. }
  118.  
  119. return $new_cart_contents;
  120.  
  121. }
  122.  
  123. }
  124.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement