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

How to Implement a WooCommerce BOGO: Buy a Fixed Quantity from Both Category X & Y, Get One Free from Category Z?

This customization is designed to implement promotions based on the quantity of products from specific categories. For instance, in an online electronics store with categories like Smartphones (X), Laptops (Y), and Electronics (Z), this code sets up a promotion where, when a combined quantity of products from categories X and Y reaches a specified threshold, a free product from category Z is automatically added to the cart.

Solution: Implement a WooCommerce BOGO: Buy a Fixed Quantity from Both Category X & Y, Get One Free from Category Z

The code works when the following condition is met. i.e. when products from both categories X and Y are bought in a fixed quantity number, a free product from the category Z is offered.

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

function ts_add_product_cat($cart_item_key, $product_id) {
    $category_ids_to_buy = array(50, 65); // Categories X and Y
    $free_category_id = 63; // Category Z
    $required_quantity = 4; // Total combined quantity required to trigger free product

    // Initialize counters for each category
    $category_counts = array_fill_keys($category_ids_to_buy, 0);

    // Update counters based on items in the cart
    foreach (WC()->cart->get_cart() as $cart_item) {
        $cart_product_cats_ids = wc_get_product_term_ids($cart_item['product_id'], 'product_cat');
        foreach ($category_ids_to_buy as $cat_id) {
            if (in_array($cat_id, $cart_product_cats_ids)) {
                $category_counts[$cat_id] += $cart_item['quantity'];
            }
        }
    }

    // Check if the combined count of products from categories X and Y meets or exceeds the required quantity
    $total_quantity = array_sum($category_counts);
    if ($total_quantity >= $required_quantity) {
        // Check if there's a free product in the cart already, if not, add one
        $free_product_in_cart = 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($free_category_id, $cart_product_cats_ids)) {
                $free_product_in_cart = true;
                break;
            }
        }

        // If there's no free product in the cart, add one
        if (!$free_product_in_cart) {
            // 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_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
}

    

Output

ScenarioDiscounted/Free Product
Example
Buy Products from Categories X and Y with Total Minimum QuantityProduct from Category ZBuy 4 units of smartphones (Category X) and laptops (Category Y) to get a free item from the Electronics category (Category Z).

When customers purchase items meeting the total combined quantity of 4 as specified in the code($required_quantity = 4;) from both categories X and Y (Smartphone+Laptop), the free product from Category Z (Electronic Accessories) will be automatically added to the cart.

How to Implement a WooCommerce BOGO: Buy a Fixed Quantity from Both Category X & Y, Get One Free from Category Z? - Tyche Softwares


If the total quantity of products from the specified categories of X and Y is not reached, then the free product will not be added.

Likewise, you can also offer discounts on category Y when purchasing category X within a specified quantity range in WooCommerce.

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