简体   繁体   English

Woocommerce中数量大于1的单独购物车项目

[英]Separated cart items in Woocommerce when quantity is more than 1

I am using " WooCommerce - Treat cart items separate if quantity is more than 1 " answer code and it works great if you are using cart. 我正在使用WooCommerce-如果数量大于1,请单独处理购物车项目 ”的回答代码,如果您使用购物车,效果很好。

But in my case I redirect people directly to the checkout, so it does not work the same way for the cart resume at the bottom at the checkout. 但是在我的情况下,我将人们直接重定向到结帐处,因此对于在结帐处底部的购物车恢复来说,它的工作方式不同。 Also when product is added to cart multiple times, it doesn't give separated items. 同样,当产品多次添加到购物车时,它不会给出单独的项目。

The following code will also handle ajax add to cart and same items added multiple times (+ jumping cart redirection to checkout) : 以下代码还将处理ajax添加到购物车和相同项添加多次(+将购物车重定向到结帐)

// Split items by their quantity units
add_action( 'woocommerce_add_to_cart', 'split_cart_items_by_quantity', 10, 6 );
function split_cart_items_by_quantity( $cart_item_key, $product_id, $quantity, $variation_id, $variation, $cart_item_data ) {
    if ( $quantity == 1 ) return;

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

    // Loop through each unit of item quantity
    for ( $i = 1; $i <= $quantity -1; $i++ ) {
        // Make each quantity item unique and separated
        $cart_item_data['unique_key'] = md5( microtime() . rand() . "Hi Mom!" );

        // Add each item quantity as a separated cart item
        WC()->cart->add_to_cart( $product_id, 1, $variation_id, $variation, $cart_item_data );
    }
}

// Make added cart item as a unique and separated (works with ajax add to cart too)
add_filter( 'woocommerce_add_cart_item_data', 'filter_add_cart_item_data', 10, 4 );
function filter_add_cart_item_data( $cart_item_data, $product_id, $variation_id, $quantity ) {
    if ( ! isset($cart_item_data['unique_key']) ) {
        // Make this item unique
        $cart_item_data['unique_key'] = md5( microtime().rand() . "Hi Dad!"  );
    }
    return $cart_item_data;
}

// Redirect to checkout jumping cart
add_action( 'template_redirect', 'cart_redirect_to_checkout' );
function cart_redirect_to_checkout(){
    if( is_cart() ){
        wp_safe_redirect( wc_get_checkout_url() );
        exit();
    }
}

Code goes in function.php file of your active child theme (or active theme). 代码进入您的活动子主题(或活动主题)的function.php文件中。 Tested and work. 经过测试和工作。

This code add support for: 此代码添加了对以下内容的支持:

  • Ajax added to cart items Ajax已添加到购物车中
  • Same products added to cart multiple times 相同产品多次添加到购物车

Based on: WooCommerce - Treat cart items separate if quantity is more than 1 基于: WooCommerce-如果数量大于1,请单独处理购物车项目

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 WooCommerce - 如果数量超过 1,则将购物车物品分开处理 - WooCommerce - Treat cart items separate if quantity is more than 1 Woocommerce - 将数量为 0 的商品添加到购物车? - Woocommerce - Items with a quantity of 0 are added to the cart? 为特定WooCommerce产品类别的购物车项目设置最小数量 - Set a minimum quantity for cart items from specific WooCommerce product category WooCommerce 检查购物车物品数量为 6 的倍数,从 12 开始 - WooCommerce check cart items quantity for multiples of 6 starting at 12 Woocommerce在结帐页面上获取购物车中特定商品的数量 - Woocommerce get quantity of specific items in cart on checkout page 在Woocommerce中根据购物车数量添加自定义结帐字段 - Add a custom checkout field based on cart items quantity in Woocommerce woocommerce购物车无法在3种以上的版本上正常工作。 - woocommerce cart is not functioning on more than 3 variations.? WooCommerce - 将商品添加到购物车时刷新商品数量 - WooCommerce - Refresh item quantity when item is added to cart WooCommerce-尝试更新数量时购物车项目数据为空 - WooCommerce - cart item data null when attempting to update quantity 在Woocommerce中单击更新购物车时自动重新计算数量 - Automatically recalculate quantity when clicking on update cart in Woocommerce
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM