Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public function add_order_item_meta( $item_id, $cart_item, $cart_item_key ) {
- if ( is_object( $cart_item ) && property_exists( $cart_item, 'legacy_values' ) ) {
- $cart_item = $cart_item->legacy_values;
- }
- $addon_id_check = '';
- $quantity = $cart_item['quantity'] ?? 1;
- if ( isset( $cart_item['yith_wapo_options'] ) && ! isset( $cart_item['yith_wcp_child_component_data'] ) ) {
- $grouped_in_cart = ! apply_filters( 'yith_wapo_show_options_grouped_in_cart', true );
- $origin_blog_id = $cart_item['blog_id'];
- foreach ( $cart_item['yith_wapo_options'] as $index => $option ) {
- foreach ( $option as $key => $value ) {
- if ( $key && '' !== $value ) {
- if ( is_array( $value ) && isset( $value[0] ) ) {
- $value = $value[0];
- }
- if ( apply_filters( 'yith_wapo_option_on_order', false, $option, $value ) ) {
- continue;
- }
- switch_to_blog( $origin_blog_id );
- $values = YITH_WAPO::get_instance()->split_addon_and_option_ids( $key, $value );
- $addon_id = $values['addon_id'];
- $option_id = $values['option_id'];
- if ( $addon_id !== $addon_id_check ) {
- $first_free_options_count = 0;
- $addon_id_check = $addon_id;
- }
- // Check Product price
- $_product = wc_get_product( $cart_item['product_id'] );
- // WooCommerce Measurement Price Calculator (compatibility).
- if ( isset( $cart_item['pricing_item_meta_data']['_price'] ) ) {
- $product_price = $cart_item['pricing_item_meta_data']['_price'];
- } else {
- $product_price = floatval( $_product->get_price() );
- }
- $addon_name = $this->get_addon_data_name( $addon_id, $option_id, $grouped_in_cart );
- $addon_value = $this->get_addon_value_on_cart( $addon_id, $option_id, $key, $value, $cart_item, $grouped_in_cart );
- $addon_prices = $this->calculate_addon_prices_on_cart( $addon_id, $option_id, $key, $value, $cart_item, $product_price );
- $option_price = 0;
- $addon_price = abs( floatval( $addon_prices['price'] ) );
- $addon_sale_price = abs( floatval( $addon_prices['price_sale'] ) );
- $sign = $addon_prices['sign'];
- $info = yith_wapo_get_option_info( $addon_id, $option_id );
- $addon_type = isset( $info['addon_type'] ) ? $info['addon_type'] : '';
- $addon_first_options_selected = isset( $info['addon_first_options_selected'] ) ? $info['addon_first_options_selected'] : '';
- $is_empty_select = 'select' === $addon_type && 'default' === $option_id;
- if ( $is_empty_select ) {
- continue;
- }
- if ( 'product' === $addon_type ) {
- if ( ! isset( $cart_item['yith_wapo_qty_options'][ $key ] ) ) {
- $cart_item['yith_wapo_qty_options'][ $key ] = $quantity;
- }
- $option_product_info = explode( '-', $value );
- $option_product_id = $option_product_info[1];
- $option_product_qty = isset( $cart_item['yith_wapo_qty_options'][ $key ] ) ? $cart_item['yith_wapo_qty_options'][ $key ] : 1;
- $option_product = wc_get_product( $option_product_id );
- if ( $option_product && $option_product instanceof WC_Product ) {
- // Stock.
- if ( $option_product->get_manage_stock() ) {
- $stock_qty = $option_product->get_stock_quantity() - $option_product_qty;
- wc_update_product_stock( $option_product, $stock_qty, 'set' );
- wc_delete_product_transients( $option_product );
- }
- if ( isset( $cart_item['yith_wapo_qty_options'] ) ) {
- wc_add_order_item_meta( $item_id, '_ywapo_product_addon_qty', $cart_item['yith_wapo_qty_options'] );
- }
- }
- }
- // First X free options check.
- if ( 'yes' === $addon_first_options_selected && $first_free_options_count < $addon_first_options_selected ) {
- $first_free_options_count ++;
- } else {
- if ( $addon_price !== 0 || $addon_sale_price !== 0 ) {
- if ( $addon_sale_price ) {
- $option_price = $addon_sale_price;
- } else {
- $option_price = $addon_price;
- }
- }
- }
- if ( '' === $addon_name ) {
- $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 );
- }
- $addon_value = apply_filters( 'yith_wapo_addon_value_as_order_item', $addon_value, $key );
- $option_price = apply_filters( 'yith_wapo_addon_prices_as_order_item', $option_price );
- $display_value = $this->get_addon_display_on_cart( $addon_id, $option_id, $addon_value, $sign, $option_price, $addon_price, $addon_sale_price );
- $display_value = html_entity_decode( stripslashes( $display_value ) );
- $cart_item['yith_wapo_options'][$index][$key] = (array)$cart_item['yith_wapo_options'][$index][$key]; // Transform string to array.
- // Additional info to use into the order item meta.
- $cart_item['yith_wapo_options'][$index][$key]['addon_id'] = $addon_id;
- $cart_item['yith_wapo_options'][$index][$key]['option_id'] = $option_id;
- $cart_item['yith_wapo_options'][$index][$key]['addon_value'] = $addon_value;
- $cart_item['yith_wapo_options'][$index][$key]['display_label'] = $addon_name;
- $cart_item['yith_wapo_options'][$index][$key]['display_value'] = $display_value;
- $addon_key = apply_filters( 'yith_wapo_cart_item_meta_data_addon_key', 'ywapo-addon-' . $addon_id . '-' . $option_id, $cart_item, $addon_name );
- $cart_item = apply_filters( 'yith_wapo_cart_item_meta_data', $cart_item, $index, $key, $value );
- restore_current_blog();
- wc_add_order_item_meta( $item_id, $addon_key, $display_value );
- }
- }
- }
- wc_add_order_item_meta( $item_id, '_ywapo_meta_data', $cart_item['yith_wapo_options'] );
- if ( ! empty( $cart_item['yith_wapo_product_img'] ) ) {
- wc_add_order_item_meta( $item_id, '_ywapo_product_img', $cart_item['yith_wapo_product_img'] );
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment