nshelper

WPGlobalCart - YITH WooCommerce Product Add-ons & Extra Options Premium

Jun 18th, 2025
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.96 KB | None | 0 0
  1. public function add_order_item_meta( $item_id, $cart_item, $cart_item_key ) {
  2.  
  3. if ( is_object( $cart_item ) && property_exists( $cart_item, 'legacy_values' ) ) {
  4. $cart_item = $cart_item->legacy_values;
  5. }
  6.  
  7. $addon_id_check = '';
  8.  
  9. $quantity = $cart_item['quantity'] ?? 1;
  10.  
  11. if ( isset( $cart_item['yith_wapo_options'] ) && ! isset( $cart_item['yith_wcp_child_component_data'] ) ) {
  12.  
  13. $grouped_in_cart = ! apply_filters( 'yith_wapo_show_options_grouped_in_cart', true );
  14.  
  15. $origin_blog_id = $cart_item['blog_id'];
  16.  
  17.  
  18. foreach ( $cart_item['yith_wapo_options'] as $index => $option ) {
  19. foreach ( $option as $key => $value ) {
  20. if ( $key && '' !== $value ) {
  21.  
  22. if ( is_array( $value ) && isset( $value[0] ) ) {
  23. $value = $value[0];
  24. }
  25.  
  26. if ( apply_filters( 'yith_wapo_option_on_order', false, $option, $value ) ) {
  27. continue;
  28. }
  29.  
  30. switch_to_blog( $origin_blog_id );
  31.  
  32. $values = YITH_WAPO::get_instance()->split_addon_and_option_ids( $key, $value );
  33.  
  34.  
  35. $addon_id = $values['addon_id'];
  36. $option_id = $values['option_id'];
  37.  
  38. if ( $addon_id !== $addon_id_check ) {
  39. $first_free_options_count = 0;
  40. $addon_id_check = $addon_id;
  41. }
  42.  
  43. // Check Product price
  44. $_product = wc_get_product( $cart_item['product_id'] );
  45. // WooCommerce Measurement Price Calculator (compatibility).
  46. if ( isset( $cart_item['pricing_item_meta_data']['_price'] ) ) {
  47. $product_price = $cart_item['pricing_item_meta_data']['_price'];
  48. } else {
  49. $product_price = floatval( $_product->get_price() );
  50. }
  51.  
  52. $addon_name = $this->get_addon_data_name( $addon_id, $option_id, $grouped_in_cart );
  53. $addon_value = $this->get_addon_value_on_cart( $addon_id, $option_id, $key, $value, $cart_item, $grouped_in_cart );
  54. $addon_prices = $this->calculate_addon_prices_on_cart( $addon_id, $option_id, $key, $value, $cart_item, $product_price );
  55.  
  56. $option_price = 0;
  57. $addon_price = abs( floatval( $addon_prices['price'] ) );
  58. $addon_sale_price = abs( floatval( $addon_prices['price_sale'] ) );
  59. $sign = $addon_prices['sign'];
  60.  
  61. $info = yith_wapo_get_option_info( $addon_id, $option_id );
  62. $addon_type = isset( $info['addon_type'] ) ? $info['addon_type'] : '';
  63. $addon_first_options_selected = isset( $info['addon_first_options_selected'] ) ? $info['addon_first_options_selected'] : '';
  64.  
  65. $is_empty_select = 'select' === $addon_type && 'default' === $option_id;
  66.  
  67. if ( $is_empty_select ) {
  68. continue;
  69. }
  70.  
  71. if ( 'product' === $addon_type ) {
  72.  
  73. if ( ! isset( $cart_item['yith_wapo_qty_options'][ $key ] ) ) {
  74. $cart_item['yith_wapo_qty_options'][ $key ] = $quantity;
  75. }
  76.  
  77. $option_product_info = explode( '-', $value );
  78. $option_product_id = $option_product_info[1];
  79. $option_product_qty = isset( $cart_item['yith_wapo_qty_options'][ $key ] ) ? $cart_item['yith_wapo_qty_options'][ $key ] : 1;
  80. $option_product = wc_get_product( $option_product_id );
  81. if ( $option_product && $option_product instanceof WC_Product ) {
  82. // Stock.
  83. if ( $option_product->get_manage_stock() ) {
  84. $stock_qty = $option_product->get_stock_quantity() - $option_product_qty;
  85. wc_update_product_stock( $option_product, $stock_qty, 'set' );
  86. wc_delete_product_transients( $option_product );
  87. }
  88.  
  89. if ( isset( $cart_item['yith_wapo_qty_options'] ) ) {
  90. wc_add_order_item_meta( $item_id, '_ywapo_product_addon_qty', $cart_item['yith_wapo_qty_options'] );
  91. }
  92. }
  93. }
  94.  
  95. // First X free options check.
  96. if ( 'yes' === $addon_first_options_selected && $first_free_options_count < $addon_first_options_selected ) {
  97. $first_free_options_count ++;
  98. } else {
  99. if ( $addon_price !== 0 || $addon_sale_price !== 0 ) {
  100. if ( $addon_sale_price ) {
  101. $option_price = $addon_sale_price;
  102. } else {
  103. $option_price = $addon_price;
  104. }
  105. }
  106. }
  107.  
  108. if ( '' === $addon_name ) {
  109. $addon_name = apply_filters( 'yith_wapo_order_item_meta_name_default', _x( 'Option', '[FRONT] Show it in the cart page if the add-on has not a label set', 'yith-woocommerce-product-add-ons' ), $index, $item_id, $cart_item );
  110. }
  111.  
  112. $addon_value = apply_filters( 'yith_wapo_addon_value_as_order_item', $addon_value, $key );
  113. $option_price = apply_filters( 'yith_wapo_addon_prices_as_order_item', $option_price );
  114.  
  115. $display_value = $this->get_addon_display_on_cart( $addon_id, $option_id, $addon_value, $sign, $option_price, $addon_price, $addon_sale_price );
  116. $display_value = html_entity_decode( stripslashes( $display_value ) );
  117.  
  118. $cart_item['yith_wapo_options'][$index][$key] = (array)$cart_item['yith_wapo_options'][$index][$key]; // Transform string to array.
  119.  
  120. // Additional info to use into the order item meta.
  121. $cart_item['yith_wapo_options'][$index][$key]['addon_id'] = $addon_id;
  122. $cart_item['yith_wapo_options'][$index][$key]['option_id'] = $option_id;
  123. $cart_item['yith_wapo_options'][$index][$key]['addon_value'] = $addon_value;
  124. $cart_item['yith_wapo_options'][$index][$key]['display_label'] = $addon_name;
  125. $cart_item['yith_wapo_options'][$index][$key]['display_value'] = $display_value;
  126.  
  127. $addon_key = apply_filters( 'yith_wapo_cart_item_meta_data_addon_key', 'ywapo-addon-' . $addon_id . '-' . $option_id, $cart_item, $addon_name );
  128. $cart_item = apply_filters( 'yith_wapo_cart_item_meta_data', $cart_item, $index, $key, $value );
  129.  
  130. restore_current_blog();
  131.  
  132. wc_add_order_item_meta( $item_id, $addon_key, $display_value );
  133.  
  134. }
  135. }
  136. }
  137.  
  138. wc_add_order_item_meta( $item_id, '_ywapo_meta_data', $cart_item['yith_wapo_options'] );
  139.  
  140. if ( ! empty( $cart_item['yith_wapo_product_img'] ) ) {
  141. wc_add_order_item_meta( $item_id, '_ywapo_product_img', $cart_item['yith_wapo_product_img'] );
  142. }
  143. }
  144. }
Advertisement
Add Comment
Please, Sign In to add comment