当产品 A 被添加到购物车时,然后将产品 B 也添加到特定产品的购物车 5 次

时间:2021-07-01 15:57:10

标签: php wordpress function woocommerce cart

我遇到了一个问题。此功能运行良好。但是现在我想在购物车中添加此产品 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 );
        }
    }
}

1 个答案:

答案 0 :(得分:0)

我认为这应该可行。

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 并添加了它们。