WooCommerce - 如果数量超过 1,则将购物车项目分开处理 [英] WooCommerce - Treat cart items separate if quantity is more than 1

查看:26
本文介绍了WooCommerce - 如果数量超过 1,则将购物车项目分开处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试执行以下操作:

I'm trying to do the following:

客户将数量为1"的产品添加到购物车(单个产品页面上的数量字段已删除,因此只能添加 1 个).

A customer adds a product with quantity "1" to the cart (the quantity field has been removed on the single product pages so only 1 can be added).

如果再次添加具有相同变体的相同产品,则不是将数量增加到2",而是将其作为单独的产品添加.

If the same product with the same variations gets added again, instead of increasing the quantity to "2", it adds it as a separate product.

我设法使用下面的过滤器完成了上述操作,我在 WordPress 论坛上找到了它.

I managed to do the above with the filter below, which I found on the WordPress forums.

add_filter('woocommerce_add_cart_item_data', 'force_separate_item_add', 10, 2);
function force_separate_item_add($cart_item_data, $product_id) {

    $unique_cart_item_key = md5(microtime().rand()."Hi Mom!");
    $cart_item_data['unique_key'] = $unique_cart_item_key;

    return $cart_item_data;
}

这家伙的解释非常好,我明白了:当一个项目被添加到购物车时,购物车中该单个项目的键"是根据添加的项目及其相关联的元数据.如果该项目及其元数据与购物车中的另一个项目相同,那么生成的键也将相同,并且购物车中已有项目的数量将简单地增加添加的数量".

The guy's explanation was quite good, and I get it: "When an item gets added to the cart, a 'key' for that individual item in the cart is created based on the item being added and its associated meta data. If the item and its meta data are identical to another item in the cart, then the generated key will be identical too, and the the quantity of the item already in the cart will simply be incremented by the quantity being added".

上面的代码只是添加了一个新密钥,然后购物车将它们视为单独的条目.

The code above simply adds a new key and the cart then treats them as separate entries.

我现在要做的是在通过购物车更改数量时为此找到合适的过滤器.因此,如果客户将数量更改为2",它将为第二个"项目生成一个新密钥并单独处理.

我尝试了几乎所有能找到的与购物车"相关的过滤器,在 WooCommerce 文档上搜索,在 Sublime 上的 woocommerce 插件文件夹中大量查找更新购物车"、购物车"等,但我恐怕我不确定要使用哪个.我工作最多的一个是这个:

I have tried almost every "cart" related filter I could find, searching on the WooCommerce docs, doing a mass find on Sublime on the woocommerce plugin folder for "update cart", "cart", etc, but I'm afraid I'm not really sure which on to use. The one I have worked on the most is this one:

add_action('woocommerce_before_calculate_totals', 'change_cart_item_price');
function change_cart_item_price($cart_object) {

    global $woocommerce;

    foreach ($woocommerce->cart->cart_contents as $cart_key => $cart_item_array) {        
        if($cart_item_array['quantity'] > 1 ) {
            $cart_item_key = md5(microtime().rand()."Hi Mom!");
            $cart_item_data['unique_key'] = $cart_item_key;

            $woocommerce->cart->set_quantity($cart_item_key, '1');
        }
   }

   return $cart_object;
}

在这里,我正在浏览购物车对象,检查数量是否大于 1,如果是,我分配了一个新键.我真的不知道我是否在做正确的事情.

Here I'm running through the cart object, checking if the quantity is bigger than one and if it is I assign a new key. I really don't know if I'm doing the right thing.

推荐答案

你的 woocommerce_add_cart_item_data 过滤器对我来说是一个很好的开始.我最终保持原样,转而使用 woocommerce_add_to_cart 钩子进行手动处理.

Your woocommerce_add_cart_item_data filter was a great start for me. I ended up leaving that as-is and moving to the woocommerce_add_to_cart hook to manually process.

这是目前对我有用的精简版.

This is a slim version of what is currently working for me.

/**
 * This hook runs at the end of the default add to cart processes, when you try to add an item to cart.
 * If trying to add more than item to the cart, separate them into individual cart items
 *
 * @author  Mike Hemberger <mike@bizbudding.com>
 *
 * @return  void
 */
add_action( 'woocommerce_add_to_cart', 'mai_split_multiple_quantity_products_to_separate_cart_items', 10, 6 );
function mai_split_multiple_quantity_products_to_separate_cart_items( $cart_item_key, $product_id, $quantity, $variation_id, $variation, $cart_item_data ) {

    // If product has more than 1 quantity
    if ( $quantity > 1 ) {

        // Keep the product but set its quantity to 1
        WC()->cart->set_quantity( $cart_item_key, 1 );

        // Run a loop 1 less than the total quantity
        for ( $i = 1; $i <= $quantity -1; $i++ ) {
            /**
             * Set a unique key.
             * This is what actually forces the product into its own cart line item
             */
            $cart_item_data['unique_key'] = md5( microtime() . rand() . "Hi Mom!" );

            // Add the product as a new line item with the same variations that were passed
            WC()->cart->add_to_cart( $product_id, 1, $variation_id, $variation, $cart_item_data );
        }

    }

}

这篇关于WooCommerce - 如果数量超过 1,则将购物车项目分开处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆