Offering different gifts based on different purchase amount in WooCommerce
According to your needs, you decide what gift from which category should be given to the customer at each level of purchase
For example:
- Buy between 200,000 and 300,000: Get product A for free.
- Buy between 300,000 and 400,000: Get product A and product B for free.
- Buy between 400,000 and infinity: get product A and product B and C free.
To do this, first create your free products on the site and hide them from the users' view. Then, enter the ID of each product in the code below (instead of 23662,1232,1452
), whenever the gift product runs out, just replace the name and image of the product.
To activate this technique of increasing online store sales, just add the following code to the end of the function file of your site.
function Rezvan_add_products_to_cart() {
global $woocommerce;
$cart_total = $woocommerce->cart->total;
if (!is_admin()) {
// Define your cart total ranges and corresponding products
$ranges_and_products = array(
array('min' => 200000, 'max' => 300000, 'products' => array(23659)), // For $200000 to $300000, add Product A
array('min' => 300000, 'max' => 400000, 'products' => array(23660,1232)), // For $300000 to $400000, add Product A & Product B
array('min' => 400000, 'max' => 500000000, 'products' => array(23662,1232,1452)), // For $400000 to $500000000, add Product A & Product B & Product C
);
foreach ($ranges_and_products as $range) {
$min = $range['min'];
$max = $range['max'];
if ($cart_total >= $min && $cart_total <= $max) {
$products_to_add = $range['products'];
// Check if the products are already in the cart
$found_products = array();
foreach ($products_to_add as $product_id) {
foreach (WC()->cart->get_cart() as $cart_item_key => $values) {
$_product = $values['data'];
if ($_product->get_id() == $product_id) {
$found_products[] = $product_id;
}
}
}
// If any product is not found, add it to the cart
foreach ($products_to_add as $product_id) {
if (!in_array($product_id, $found_products)) {
WC()->cart->add_to_cart($product_id);
}
}
// Stop further processing to prevent adding multiple sets of products
return;
}
}
}
}
add_action('template_redirect', 'Rezvan_add_products_to_cart');