0

I am new here. I found some code on here for Adding a Free product based on Subtotal in Woocommerce, with the ability to add different products at 2 different levels. The only issue I am facing with this is when you involve Coupons, removing them, and Changing Product Qty from the Cart page. When making changes from the Cart page to Qty or removing the coupon, the free product does not behave properly. For example if I have 2 qty of something that adds the 2nd free product, and if i change qty to 1, then instead of adding the 1st free product, it removes it completely. And a similar thing happens if i add and remove a coupon. Now if i proceed and go to checkout then it shows properly, but this kind of behavior will confuse people...Is there anyway to deal with that?

EDIT: It seems like this code is not actually looking at the Subtotal. The way it behaves when adding a coupon is like it is actually looking at the Final Cart amount. That is why I think when I add the coupon the Free products don't add properly, because I have certain minimums set with the Subtotal in mind, but the coupon is dropping the cart amount below, so the free products are not adding properly.

Any help would be greatly appreciated, thanks!

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

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

    // Free product productIDs
    $free_product_id_1 = 819;
    $free_product_id_2 = 821;
    
    // Minimum subtotal needed for free products
    $min_subtotal_free_product_1 = 1500;
    $min_subtotal_free_product_2 = 2000;

    // Initializing
    $cart_subtotal = 0;

    // Loop through cart items
    foreach ( $cart->get_cart() as $cart_item_key => $cart_item ) {
        // When free product is is cart
        if ( $free_product_id_1 == $cart_item['product_id'] ) {
            $free_key_1 = $cart_item_key;
            $free_qty_1 = $cart_item['quantity'];
            // Optionally set the price to zero
            $cart_item['data']->set_price(0);
        } elseif ( $free_product_id_2 == $cart_item['product_id'] ) {
            $free_key_2 = $cart_item_key;
            $free_qty_2 = $cart_item['quantity'];
            // Optionally set the price to zero
            $cart_item['data']->set_price(0);
        } else {
            // NOT empty
            if ( ! empty ( $cart_item['line_total'] ) ) {
                $cart_subtotal += $cart_item['line_total'];
            }

            // NOT empty
            if ( ! empty ( $cart_item['line_tax'] ) ) {
                $cart_subtotal += $cart_item['line_tax'];
            }
        }
    }
    
    // If subtotal is less than first subtotal
    if ( $cart_subtotal < $min_subtotal_free_product_1 ) {
        // Free product 1 is already in cart, remove it
        if ( isset( $free_key_1 ) ) {
            $cart->remove_cart_item( $free_key_1 );
        }
        
        // Free product 2 is already in cart, remove it
        if ( isset( $free_key_2 ) ) {
            $cart->remove_cart_item( $free_key_2 );
        }
    }
    // If subtotal is between first and second subtotal
    elseif ( $cart_subtotal >= $min_subtotal_free_product_1 && $cart_subtotal < $min_subtotal_free_product_2 ) {
        // Free product 1 is not already in cart, add it
        if ( ! isset( $free_key_1 ) ) {
            $cart->add_to_cart( $free_product_id_1 );
        }
        
        // Free product 2 is in cart, remove it
        if ( isset( $free_key_2 ) ) {
            $cart->remove_cart_item( $free_key_2 );
        }
    }
    // If subtotal greater than or equal to second subtotal
    elseif ( $cart_subtotal > $min_subtotal_free_product_2 ) {
        // Free product 1 is already in cart, remove it
        if ( isset( $free_key_1 ) ) {
            $cart->remove_cart_item( $free_key_1 );
        }
        
        // Free product 2 is not already in cart, add it
        if ( ! isset( $free_key_2 ) ) {
            $cart->add_to_cart( $free_product_id_2 );
        }
    }   

    // Keep free product 1 quantity to 1.
    if ( isset( $free_qty_1 ) && $free_qty_1 > 1 ) {
        $cart->set_quantity( $free_key_1, 1 );
    }
    
    // Keep free product 2 quantity to 1.
    if ( isset( $free_qty_2 ) && $free_qty_2 > 1 ) {
        $cart->set_quantity( $free_key_2, 1 );
    }
}
add_action( 'woocommerce_before_calculate_totals', 'action_woocommerce_before_calculate_totals', 10, 1 );
Satyen
  • 1
  • 1

0 Answers0