Search code examples
phpwordpressfunctionwoocommercecart

When Product A is added to cart, then also add Product B to cart in 5 times for specific products


I have faced a problem. This functions is working fine. But now I want when add this product 454545 in the cart, then need to add also this 263654 product in the cart in five times. If I add regular/other products in the cart, then need add this product 263654 in the cart in one time as usual works like before.

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 );
        }
    }
}

Solution

  • I think this should work.

    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);
                }
            }
        }
    }
    

    I have added the comment where I have made the changes. Those two are the places which are adding the product to cart after calculation. I have just added for loop with the array of product you want to add $other_five_products and added them.