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

How to Offer a BOGO Deal: Buy Category X and Y, Get Category Z in WooCommerce?

This BOGO (Buy One Get One) promotion is especially useful when you want your customers to explore more categories of products and end up with multiple purchases. The provided code sets up a WooCommerce BOGO offer that automatically grants a free product from Category Z if customers add items from both Category X and Category Y to their cart eventually boosting the store’s average order value.

Solution: Offer a BOGO Deal: Buy Category X and Y and Get Category Z in WooCommerce

For example, you can consider a bundled category promotion such as customers are required to purchase a smartphone (Category X) and a laptop (Category Y) to qualify for a complimentary accessory, such as a pair of electronic accessories (Category Z).

add_action('woocommerce_add_to_cart', 'ts_add_product_cat', 10, 2);
add_action('woocommerce_cart_item_removed', 'ts_check_remove_free_product', 10, 2);

function ts_add_product_cat($cart_item_key, $product_id) {
    $category_ids_to_buy = array(50, 16); // Categories X and Y
    $free_category_id = 63; // Category Z

    // Check if the added product belongs to any of the specified categories to buy
    $product_cats_ids = wc_get_product_term_ids($product_id, 'product_cat');

    if (!is_admin() && array_intersect($category_ids_to_buy, $product_cats_ids)) {
        // Check if both categories X and Y are present in the cart
        $x_category_present = false;
        $y_category_present = false;

        foreach (WC()->cart->get_cart() as $cart_item) {
            $cart_product_cats_ids = wc_get_product_term_ids($cart_item['product_id'], 'product_cat');
            if (in_array(50, $cart_product_cats_ids)) {
                $x_category_present = true;
            }
            if (in_array(16, $cart_product_cats_ids)) {
                $y_category_present = true;
            }
        }

        // If both categories X and Y are present, add the free product to the cart
        if ($x_category_present && $y_category_present) {
            // Get a free product from the specified category
            $free_product_id = ts_get_free_product_from_category($free_category_id);

            // If the free product is available, add it to the cart
            if ($free_product_id) {
                WC()->cart->add_to_cart($free_product_id);
            }
        }
    }
}

function ts_check_remove_free_product($removed_cart_item_key, $cart) {
    $x_category_present = false;
    $y_category_present = false;

    foreach ($cart->get_cart() as $cart_item) {
        $cart_product_cats_ids = wc_get_product_term_ids($cart_item['product_id'], 'product_cat');
        if (in_array(50, $cart_product_cats_ids)) {
            $x_category_present = true;
        }
        if (in_array(16, $cart_product_cats_ids)) {
            $y_category_present = true;
        }
    }

    // If either category X or category Y is not present, remove the free product
    if (!$x_category_present || !$y_category_present) {
        // Find and remove the free product from the cart
        foreach ($cart->get_cart() as $cart_item_key => $values) {
            $_product = $values['data'];
            $free_product_id = ts_get_free_product_from_category(63); // Free product category ID

            if ($free_product_id && $_product->get_id() == $free_product_id) {
                $cart->remove_cart_item($cart_item_key);
                break; // Stop after removing the first occurrence
            }
        }
    }
}

function ts_get_free_product_from_category($category_id) {
    // Implement this function to dynamically get a free product ID from the specified category
    $args = array(
        'post_type'      => 'product',
        'posts_per_page' => 1,
        'tax_query'      => array(
            array(
                'taxonomy' => 'product_cat',
                'field'    => 'id',
                'terms'    => $category_id,
            ),
        ),
    );

    $products_query = new WP_Query($args);

    if ($products_query->have_posts()) {
        $products_query->the_post();
        $free_product_id = get_the_ID();
        wp_reset_postdata();
        return $free_product_id;
    }

    return null; // Return null if no free product is found
}
flexi bogo cta banner image


This to the shop owners who are running or planning to run BOGO offers on their WooCommerce store…

BOGO deals are great for increasing your sales, but have you thought about which offers are bringing you more revenue and which offers are not performing that great?

Don’t just set a BOGO deal, track the revenue generated by your deals in real-time with the Flexi BOGO for WooCommerce plugin.

Output

Scenario
Discounted/Free ProductExample
Adding Products from Both Category X and YProduct from Category Z is addedAdd products from Category X and Category Y to the cart, and a product from Category Z is automatically included in the cart.

When the customer adds products to the cart, the code loops through the cart items and checks if the items belong to both category X and Y. Only when both categories are present, the code will automatically include the complimentary product from Category Z in the cart.

How to Offer a BOGO Deal: Buy Category X and Y, Get Category Z in WooCommerce? - Tyche Softwares

If either a product from category X or Y is not present, or if such products added to cart from category X or Y are removed, the complimentary free product added to the cart is also removed. This ensures that the free product is offered when the cart contains items from both the categories of X & Y.

In similar to the above customization, you can offer free gifts based on different conditions and will let customers to automatically add a product to your WooCommerce cart based on cart total, categories, website visit, etc.



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

Share It:

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