分离数量大于1的购物车项目后,从第一个产品中获取itemmeta并将其粘贴到另一个产品上

时间:2019-05-09 14:17:24

标签: php wordpress woocommerce

我有一个非常具体的问题。

首先,当数量大于1时,我将购物车中的物品分开,然后将它们视为单独的物品。

目前,所有功能都可以完美实现分离,只需要将多余的产品选项传递给产品即可。这是我想要的http://prntscr.com/nmfuty

的屏幕截图

以下是分隔项目的代码:

function bbloomer_split_product_individual_cart_items( $cart_item_data, $product_id ){
  $unique_cart_item_key = uniqid();
  $cart_item_data['unique_key'] = $unique_cart_item_key;
  return $cart_item_data;
}

add_filter( 'woocommerce_add_cart_item_data', 'bbloomer_split_product_individual_cart_items', 10, 2 );

add_action( 'woocommerce_after_cart_item_quantity_update', 'limit_cart_item_quantity', 20, 4 );
function limit_cart_item_quantity( $cart_item_key, $quantity, $old_quantity, $cart ){

    // Here the quantity limit
    $limit = 1;
    $orders_added = $quantity - $limit;

    if( $quantity > $limit ){
        //Set existing line item quantity to the limit of 1
        $cart->cart_contents[ $cart_item_key ]['quantity'] = $limit;
        //get product id of item that was updated
        $product_id = $cart->cart_contents[ $cart_item_key ][ 'product_id' ];
        for( $i = 0; $i< $orders_added; $i++ ){
            //iterate over the number of orders you must as with quantity one
            $unique_cart_item_key = uniqid();
            //create unique cart item ID, this is what breaks it out as a separate line item
            $cart_item_data = array();
            //initialize cart_item_data array where the unique_cart_item_key will be stored
            $cart_item_data['unique_key'] = $unique_cart_item_key;
            //set the cart_item_data at unique_key = to the newly created unique_key

            //add that shit! this does not take into account variable products
            $cart->add_to_cart( $product_id, 1, 0, 0, $cart_item_data );
        }

        // Add a custom notice
        wc_add_notice( __('Notice!'), 'notice' );

    }
}

我正在使用TM Extra Product Options插件,如果有帮助,该插件基本上使用meta_key [_tmcartepo_data]将信息保存在DB的woocommerce_order_itemmeta表中。

非常感谢:)

0 个答案:

没有答案