简体   繁体   中英

Automatically add packaging when adding product to cart in WooCommerce

In WooCommerce, I use a code that auto adds packaging when adding any dish to the cart.

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  = 5737; // "LunchBox" 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 "LunchBox" 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 "LunchBox"
        else {
            $items_count++;
        }
    }

    // If product "LunchBox" 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 "LunchBox" is not in cart, we add it
    elseif ( ! isset($dcharge_key) && $items_count > 0 ) {
        $cart->add_to_cart( $dcharge_id, $items_count );
    }
}

I have one dish = one lunchbox, and three dishes (three lunchboxes) = one package.

I want to achieve such functionality ...

  1. When adding a dish, the “Lunchbox” is automatically added. Further, if there are 3 lunchboxes in the cart, then one "Package" is automatically added.

  2. Either the name “Lunchbox” and “Package” are already in the cart, then their quantity is simply automatically added and the total cost of packaging is calculated.

But here two problems:

a. With an increase in the number of servings of one dish that is already in the cart, the number of lunchboxes does not increase.

b.I just can't understand how to automatically add another product "Package" if the quantity of lunchboxes becomes “3”.

I will be happy for your help!

For clarity:

Every product is a dish, except 'lunch box' and 'package' which are also products. The client does not select anything on his own, everything happens automatically.

Then you get:

function add_delivery_charge_to_cart( $cart ) {
    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return;

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

    $lunchbox_id  = 5737; // "LunchBox" to be added to cart
    $pakket_id = 218; // "Pakket" to be added to cart

    // Loop through cart items
    foreach ( $cart->get_cart() as $cart_item_key => $cart_item ) {
        // Check if "LunchBox" product is already in cart
        if( $cart_item['data']->get_id() == $lunchbox_id ) {
            $lunchbox_key = $cart_item_key;
            $lunchbox_qty = $cart_item['quantity'];
        }
        
        // Check if "Pakket" product is already in cart
        if( $cart_item['data']->get_id() == $pakket_id ) {
            $pakket_key = $cart_item_key;
            $pakket_qty = $cart_item['quantity'];
        }       
    }
    
    // Get total items in cart, counts number of products and quantity per product
    $total_items_in_cart = $cart->get_cart_contents_count();
    
    // If product "LunchBox" is in cart, we check the quantity to update it if needed
    if ( isset($lunchbox_key) && $lunchbox_qty != $total_items_in_cart ) {
        // Lunchbox total = total_items_in_cart 
        $lunchbox_total = $total_items_in_cart;
        
        // Isset lunchbox qty, lunchbox total - lunchbox qty
        if ( isset($lunchbox_qty) ) {
            $lunchbox_total = $lunchbox_total - $lunchbox_qty;
        }

        // Isset pakket qty, lunchbox total - pakket qty        
        if ( isset($pakket_qty) ) {
            $lunchbox_total = $lunchbox_total - $pakket_qty;
        } 
        
        // Set quantity, lunchbox
        $cart->set_quantity( $lunchbox_key, $lunchbox_total );
        
    } elseif ( !isset($lunchbox_key) && $total_items_in_cart > 0 ) {
        // Product "LunchBox" is not in cart, we add it
        $cart->add_to_cart( $lunchbox_id, $total_items_in_cart );
    }
    
    // Total items in cart greater than or equal to 3
    if ( $total_items_in_cart >= 3 ) {
        // Pakket total = total_items_in_cart 
        $pakket_total = $total_items_in_cart;
        
        // Isset lunchbox qty, pakket total - lunchbox qty
        if ( isset($lunchbox_qty) ) {
            $pakket_total = $pakket_total - $lunchbox_qty;
        }

        // Isset pakket qty, pakket total - pakket qty      
        if ( isset($pakket_qty) ) {
            $pakket_total = $pakket_total - $pakket_qty;
        }       
        

        // Pakket total = pakket_total / 3 = floor(result)
        // Floor = round fractions down, rounding result down
        $pakket_total = floor( $pakket_total / 3 );
        
        // If product "Pakket" is in cart
        if ( isset($pakket_key) ) {         
            $cart->set_quantity( $pakket_key, $pakket_total );
        } elseif ( !isset($pakket_key) ) {
            // Product "Pakket" is not in cart, we add it
            $cart->add_to_cart( $pakket_id, $pakket_total );
        }
    }
}
add_action( 'woocommerce_before_calculate_totals', 'add_delivery_charge_to_cart', 10, 1 );

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM