【问题标题】:When Product A is added to cart, then also add Product B to cart in 5 times for specific products将产品 A 加入购物车后,针对特定产品也将产品 B 加入购物车 5 次
【发布时间】:2021-09-13 17:06:24
【问题描述】:

我遇到了一个问题。此功能运行良好。但是现在我想在购物车中添加此产品454545 时,还需要在购物车中添加此263654 产品五次。如果我在购物车中添加常规/其他产品,则需要像以前一样将这个产品 263654 一次性添加到购物车中。

add_action('woocommerce_add_to_cart', 'add_product_to_cart');
function add_product_to_cart() {
    if ( !is_admin()  && !is_cart() && !is_checkout()) {
        $product_id = 263654; //replace with your own product id
        $specific_products = 454545; 

        $excloud_product = 380978; 
        $excloud_product1 = 446740; 

        $found = false;
        $items_count = 0;
        //check if product already in cart
        if ( sizeof( WC()->cart->get_cart() ) > 0 ) {
            foreach ( WC()->cart->get_cart() as $cart_item_key => $values ) {
                $_product = $values['data'];
                if ( $_product->get_id() == $product_id  || $_product->get_id() == $excloud_product ){
                    $product_key = $cart_item_key;
                    $product_qty = $cart_item['quantity'];
                    $found = true;
                }else {
                    $items_count++;
                }
            }
            // if product not found, add it
            if ( ! $found ){
                WC()->cart->add_to_cart( $product_id, $items_count );
            }else{
                WC()->cart->set_quantity( $product_key, $items_count );
            }
        
        }else{
            // if no products in cart, add it
            WC()->cart->add_to_cart( $product_id );
        }
    }
}

【问题讨论】:

    标签: php wordpress function woocommerce cart


    【解决方案1】:

    我认为这应该可行。

    add_action('woocommerce_add_to_cart', 'bks_add_product_to_cart');
    
    function bks_add_product_to_cart()
    {
        if (!is_admin()  && !is_cart() && !is_checkout()) {
            $product_id = 263654; //replace with your own product id
    
            $excloud_product = 380978;
    
            $other_five_products = array(12321, 23123, 213123, 21323, 123213);
    
            $found = false;
            $items_count = 0;
            //check if product already in cart
            if (sizeof(WC()->cart->get_cart()) > 0) {
                foreach (WC()->cart->get_cart() as $cart_item_key => $values) {
                    $_product = $values['data'];
                    if ($_product->get_id() == $product_id  || $_product->get_id() == $excloud_product) {
                        $product_key = $cart_item_key;
                        $found = true;
                    } else {
                        $items_count++;
                    }
                }
                // if product not found, add it
                if (!$found) {
                    WC()->cart->add_to_cart($product_id, $items_count);
                    ////////// ADD IT WHEN NOT FOUND //////////////////////
                    foreach ($other_five_products as $prod) {
                        WC()->cart->add_to_cart($prod, 1);
                    }
                } else {
                    WC()->cart->set_quantity($product_key, $items_count);
                }
            } else {
                // if no products in cart, add it
                WC()->cart->add_to_cart($product_id);
                ////////// ADD IT IF NO PRODUCT IS ADDED //////////////////////
                foreach ($other_five_products as $prod) {
                    WC()->cart->add_to_cart($prod, 1);
                }
            }
        }
    }
    

    我已在我进行更改的地方添加了评论。这两个是计算后将产品添加到购物车的地方。我刚刚添加了带有您要添加的产品数组的 for 循环 $other_five_products 并添加了它们。

    【讨论】:

    • 谢谢@bhanu,但是我想在购物车中添加此产品454545 时,还需要在购物车中添加此263654 产品五次。如果我在购物车中添加常规/其他产品,则需要像以前一样将这个产品 263654 一次性添加到购物车中。
    猜你喜欢
    • 2012-06-09
    • 1970-01-01
    • 2011-12-19
    • 2014-11-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-06
    相关资源
    最近更新 更多