0

I want to add a certain free gift, depending on cart amount in WooCommerce.

  • Less than 80 no product gift
  • More than 80 and less than 120 add gift 1
  • More than 120 add gift 1 and gift 2

Based on Add free gifted product for a minimal cart amount in WooCommerce answer code, which works if I add 1 element, but if I add gift 2 it stops working.

Additional issues:

  1. When doing a refresh cart the product quantity of gift 1 and gift 2 increases to 2, 3, 4, etc, and not remains 1, and without changing the subtotal.
  2. The displayed subtotal of the gift is not equal to zero

This is my code attempt:

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 = gift1;
    $free_product_id_2 = gift2;

    // Minimum subtotal needed for free products
    $min_subtotal_free_product_1 = 80;
    $min_subtotal_free_product_2 = 120;

    // 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['gift1'] ) {
            $free_key_1 = $cart_item_key;
            $free_qty_1 = $cart_item['1'];
            // Optionally set the price to zero
            $cart_item['6215']->set_price( 0 );
        } elseif ( $free_product_id_2 == $cart_item['gift2'] ) {
            $free_key_2 = $cart_item_key;
            $free_qty_2 = $cart_item['1'];
            // Optionally set the price to zero
            $cart_item['7912']->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 );

Thanks

Vijay Hardaha
  • 2,411
  • 1
  • 7
  • 16
Giam
  • 1

1 Answers1

0

I modify the code in this way but keep doesn't work:

function check_free_gifted_product( $cart ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
    return;
// Free product productIDs
$free_product_id_1 = 6215;
$free_product_id_2 = 7912;

// Minimum subtotal needed for free products
$min_subtotal_free_product_1 = 80;
$min_subtotal_free_product_2 = 120;

$cart_subtotal     = 0; // Initializing

// 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['6215'] ) {
        $free_key_1 = $cart_item_key;
        $free_qty_1 = $cart_item['1'];
        // Optionally set the price to zero
        $cart_item['6215']->set_price(0);
    } elseif ( $free_product_id_2 == $cart_item['7912'] ) {
        $free_key_2 = $cart_item_key;
        $free_qty_2 = $cart_item['1'];
        // Optionally set the price to zero
        $cart_item['7912']->set_price(0);
    } else {
        $cart_subtotal += $cart_item['line_total'] + $cart_item['line_tax'];
    }
}
// If first subtotal match and free product 1 is not already in cart, add it
if ( ! isset($free_key_1) && $cart_subtotal >= $min_subtotal_free_product_1 ) {
    $cart->add_to_cart( $free_product_id_1 );
}
// If second subtotal match and free product 2 is not already in cart, add it
if ( ! isset($free_key_2) && $cart_subtotal >= $min_subtotal_free_product_2 ) {
    $cart->add_to_cart( $free_product_id_2 );
}

// If subtotal doesn't match and free product 1 is already in cart, remove it
elseif ( isset($free_key_1) && $cart_subtotal < $min_subtotal_free_product_1 ) {
    $cart->remove_cart_item( $free_key_1 );
}
// If subtotal doesn't match and free product 2 is already in cart, remove it
elseif ( isset($free_key_2) && $cart_subtotal < $min_subtotal_free_product_2 ) {
    $cart->remove_cart_item( $free_key_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 );
}
}

// Display free gifted product price to zero on minicart
add_filter( 'woocommerce_cart_item_price', 
'change_minicart_free_gifted_item_price', 10, 3 );
function change_minicart_free_gifted_item_price( $price_html, $cart_item, 
$cart_item_key ) {
$free_product_id_1 = 6215;
$free_product_id_2 = 7912;

if( $cart_item['6215'] == $free_product_id_1 ) {
    return wc_price( 0 );
}
return $price_html;
if ( $cart_item['7912'] == $free_product_id_2 ) {
    return wc_price( 0 );
}
return $price_html;
}
Giam
  • 1