简体   繁体   English

根据Woocommerce中的其他购物车项目自动将特定产品添加到购物车

[英]Auto add a specific product to cart based on other cart items count in Woocommerce

I am trying to add a product called (Delivery Charge) to the cart based on how many products I have in the cart. 我正在尝试根据购物车中的产品数量将一个名为(交货费)的产品添加到购物车。

Cart Example: 购物车示例:
Product A (QTY 5) 产品A(数量5)
Product B (QTY 2) 产品B(数量2)
Product C (QTY 4) 产品C(QTY 4)
Delivery Charge (QTY 3) **This is 3 because that is the total line items it should be counting that was added to cart before the delivery charge product was added. 运费(数量3)**这是3,因为这是在添加运费产品之前添加到购物车中的应计数的总行项目。

Having troubles with my code: 我的代码遇到麻烦:

/* Function to get total Products (line items) not qty of each */
function count_item_in_cart() {
    global $woocommerce; 
    $counter = 0; 

    foreach ($woocommerce->cart->get_cart() as $cart_item_key => $cart_item) {
        $counter++;
    }
    return $counter;
}

/* Add DC (Delivery Charge Product) to Cart based on qty */ 
add_action( 'template_redirect', 'delivery_charge_add_product_to_cart' ); 
function delivery_charge_add_product_to_cart() {
    /* Establish Product Delivery Charge Product ID */
    global $woocommerce;
    $product_id = 4490;  /* Product ID to add to cart */
    $quantity = count_item_in_cart(); 

    if ($quantity > 0) {
        WC()->cart->add_to_cart( $product_id, $quantity); 
    }
}

It always returns a higher number. 它总是返回一个更高的数字。 I think it is counting the QTY for each product and not actual product item. 我认为它是计算每个产品的数量,而不是实际的产品项目。

Any help would be appreciated! 任何帮助,将不胜感激!

The following code will auto add/update your additional product "Delivery charge" to cart each time a product is added to cart and will handle all possible cases: 每次将产品添加到购物车时,以下代码都会自动将您的附加产品“送货费用”添加/更新到购物车,并将处理所有可能的情况:

add_action( 'woocommerce_before_calculate_totals', 'add_delivery_charge_to_cart', 10, 1 );
function add_delivery_charge_to_cart( $cart ) {
    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return;

    if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 )
        return;

    $dcharge_id  = 4490; // Product Id "Delivery charge" to be added to cart
    $items_count = 0;  // Initializing

    // Loop through cart items
    foreach ( $cart->get_cart() as $cart_item_key => $cart_item ) {
        // Check if "Delivery charge" product is already in cart
        if( $cart_item['data']->get_id() == $dcharge_id ) {
            $dcharge_key = $cart_item_key;
            $dcharge_qty = $cart_item['quantity'];
        }
        // Counting other items than "Delivery charge"
        else {
            $items_count++;
        }
    }

    // If product "Delivery charge" is in cart, we check the quantity to update it if needed
    if ( isset($dcharge_key) && $dcharge_qty != $items_count ) {
        $cart->set_quantity( $dcharge_key, $items_count );
    }
    // If product "Delivery charge" is not in cart, we add it
    elseif ( ! isset($dcharge_key) && $items_count > 0 ) {
        $cart->add_to_cart( $dcharge_id, $items_count );
    }
}

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

暂无
暂无

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

相关问题 根据 WooCommerce 购物车小计自动添加可变数量的特定产品 - Auto add a variable quantity of specific product based on WooCommerce cart subtotal 在Woocommerce 3中自动将产品添加到购物车 - Auto add a Product to Cart in Woocommerce 3 为 WooCommerce 中特定产品类别的购物车项目自动添加产品 - Auto add a product for cart item from specific product categories in WooCommerce 根据Woocommerce购物车小计自动为特定产品应用优惠券 - Auto apply a coupon for a specific product based on Woocommerce cart subtotal 根据产品类别拒绝 Woocommerce 中特定购物车项目的结账 - Deny checkout for specific cart items in Woocommerce based on product categories 根据特定产品有条件地删除Woocommerce购物车项目 - Remove conditionally Woocommerce cart items based on a specific product 在WooCommerce购物车项目上为特定产品类别添加正文类 - Add a body class for a specific product category on WooCommerce cart items Ajax上的JS警报已添加到购物车,以获取Woocommerce中特定产品类别的数量 - JS alert on ajax add to cart for specific product category count in Woocommerce 根据Woocommerce中购物车的数量增加产品价格的额外成本 - Add an extra cost to product price based on cart item count in Woocommerce Woocommerce 如果购物车中有特定产品,则将特定产品添加到购物车 - Woocommerce add to cart a specific product if a specific product is in the cart
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM