Black Friday & Cyber Monday SUPER SALE ALL WEEK:
Grab 40% OFF on plugins
Days
Hours
Minutes
Seconds

How to Add BOGO (Buy One Get One) Offers Based on WooCommerce Cart Total Range?

Implementing a BOGO-tiered pricing strategy in WooCommerce is now easier than ever with this PHP snippet. It automatically adds free products to the cart based on the customer’s spending range, enhancing their shopping experience and driving sales. Also, customers feel happy to spend more as they are receiving more free products.

Solution: Add BOGO (Buy One Get One) Offers Based on WooCommerce Cart Total Range

The below code snippet will offer different free products based on the pre-defined cart total ranges that help a store owner to implement a tiered pricing strategy. For each spending range, the code automatically adds different free products to the cart. For example:

  • Spend between $50 and $100: Get Product A for free.
  • Spend between $100 and $200: Get Product A and Product B for free.
function ts_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' => 50, 'max' => 100, 'products' => array(470)),       // For $0 to $100, add Product A
            array('min' => 100, 'max' => 200, 'products' => array(457, 470)), // For $101 to $200, add Product A & Product B
        );

        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', 'ts_add_products_to_cart');

Output

When the customer’s cart total falls with the specified range of $50 to $100, the corresponding free product A is added to the cart.

How to Add BOGO (Buy One Get One) Offers Based on WooCommerce Cart Total Range? - Tyche Softwares

When the cart total falls in the next specified range of $100-$200, both product A and product B is automatically added to the cart.

How to Add BOGO (Buy One Get One) Offers Based on WooCommerce Cart Total Range? - Tyche Softwares

Don’t miss out our another powerful customization that adds free gift based on selected quantity in WooCommerce that will dynamically alter the free products.


Browse more in: Code Snippets, WooCommerce How Tos, WooCommerce Tutorials

Share It:

Subscribe
Notify of
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x