0

I have a setup where if the order minimum is $100 a free product is added to the cart. If the customer once again goes below the order minimum the free product is dynamically removed via ajax.

I would like to add another threshold of $200, where a different free product is added once the order minimum is hit, but remove the free product that was added at $100. ($100 gets you a small prize, $200 gets you a bigger prize, but you can't have both.)

I seem to struggling with scanning the cart for more than one product. All help appreciated. (If possible, please explain - I have tried so many different approaches. I can add the second, but can't for the life of me remove the $100 free product)

add_action( 'woocommerce_before_calculate_totals', 'add_or_remove_cart_items', 10, 1 );
function add_or_remove_cart_items( $cart ) {
    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return;

    // ONLY for logged users (and avoiding the hook repetition) 
    if ( ! is_user_logged_in() && did_action( 'woocommerce_before_calculate_totals' ) >= 2 )
        return;

    $threshold_amount = 100; // The threshold amount for cart total
    $free_product_id  = 339; // ID of the free product
    $cart_items_total = 0; // Initicializing

    // Loop through cart items
    foreach ( $cart->get_cart() as $cart_item_key => $cart_item ){
        // Check if the free product is in cart
        if ( $cart_item['data']->get_id() == $free_product_id ) {
            $free_item_key = $cart_item_key;
        }
        // Get cart subtotal incl. tax from items (with discounts if any)
        $cart_items_total += $cart_item['line_total'] + $cart_item['line_tax'];
    }

    // If Cart total is up to the defined amount and if the free products is not in cart, we add it.
    if ( $cart_items_total >= $threshold_amount && ! isset($free_item_key) ) {
        $cart->add_to_cart( $free_product_id );
    }
    // If cart total is below the defined amount and free product is in cart, we remove it.
    elseif ( $cart_items_total < $threshold_amount && isset($free_item_key) ) {
        $cart->remove_cart_item( $free_item_key );
    }
}
0

Add or remove a free product based on a cart total threshold amount

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

    // ONLY for logged users (and avoiding the hook repetition) 
    if ( ! is_user_logged_in() && did_action( 'woocommerce_before_calculate_totals' ) >= 2 )
        return;

    // Settings
    $first_threshold_amount = 100; // The first threshold amount for cart total
    $second_threshold_amount = 200; // The secound threshold amount for cart total

    $first_free_product_id  = 339; // ID of the first free product
    $second_free_product_id  = 30; // ID of the secound free product

    $cart_items_total = 0; // Initicializing

    // Loop through cart items
    foreach ( $cart->get_cart() as $cart_item_key => $cart_item ){
        // Check if the first free product is in cart
        if ( $cart_item['data']->get_id() == $first_free_product_id ) {
            $first_free_item_key = $cart_item_key;
        }

        // Check if the secound free product is in cart     
        if ( $cart_item['data']->get_id() == $second_free_product_id ) {
            $second_free_item_key = $cart_item_key;
        }       

        // Get cart subtotal incl. tax from items (with discounts if any)
        $cart_items_total += $cart_item['line_total'] + $cart_item['line_tax'];
    }

    // If Cart total is below 100
    if ( $cart_items_total < $first_threshold_amount ) {

        // First free product is in cart, we remove it.
        if ( isset($first_free_item_key) ) {
            $cart->remove_cart_item( $first_free_item_key );
        }

        // Second free product is in cart, we remove it.
        if ( isset($second_free_item_key) ) {
            $cart->remove_cart_item( $second_free_item_key );
        }

        // If cart total between 100 & 200
    } elseif ( $cart_items_total >= $first_threshold_amount && $cart_items_total < $second_threshold_amount ) {

        // First free product is not in cart, we add it.
        if ( !isset($first_free_item_key) ) {
            $cart->add_to_cart( $first_free_product_id );
        }

        // Secound free product is in cart, we remove it
        if ( isset($second_free_item_key) ) {
            $cart->remove_cart_item( $second_free_item_key );
        }

        // If cart total >= 200
    } elseif( $cart_items_total >= $second_threshold_amount ) {

        // First free product is in cart, we remove it.
        if ( isset($first_free_item_key) ) {
            $cart->remove_cart_item( $first_free_item_key );
        }

        // Second free product is not in cart, we add it.
        if ( !isset($second_free_item_key) ) {
            $cart->add_to_cart( $second_free_product_id );
        }

    }
}
add_action( 'woocommerce_before_calculate_totals', 'add_or_remove_cart_items', 10, 1 );
1
  • Any luck with this? – 7uc1f3r Mar 20 '20 at 9:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.