Search code examples
phpwordpresswoocommerceproductcart

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)
Product B (QTY 2)
Product 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.

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!


Solution

  • 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). Tested and works.