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

How to Set up a BOGO Deal: Buy one Get one Free from Different Categories in WooCommerce?

With this customization, you can easily setup WooCommerce BOGO deals across different categories. This approach involves a way to introduce cross-selling products and make customers to explore another category that might be less popular or need attention. For example, If customers buy a pair of shoes from the Sneakers category, they get a Baseball Cap for free from the Accessories category.

Solution: Set up a BOGO Deal: Buy one Get one Free from Different Categories in WooCommerce

The code automatically adds a free product from a specified category (with ID 16) to the cart when a product from the “Sports” category (with ID 60) is added to the cart.

function ts_add_product_cat( $cart_item_key, $product_id ) {
    $sports_category_id = 60; // Sports category ID
    $free_category_id = 16; // Category ID for free products

    $product_cats_ids = wc_get_product_term_ids( $product_id, 'product_cat' );

    if ( ! is_admin() && in_array( $sports_category_id, $product_cats_ids ) ) {
        // 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 );
        }
    }
}

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

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

When a customer adds a pair of sneakers to their cart, the ‘ts_get_free_product_from_category’ function takes a category ID as a parameter and queries the WordPress database to retrieve a single product ID from the specified category. If the product is found, as a bonus, a free product from the accessories category is also added to their cart.

How to Set up a BOGO Deal: Buy one Get one Free from Different Categories in WooCommerce? - Tyche Softwares


Just like the above customization, you can apply different BOGO strategies, such as offering “Buy 2 Get 50% Off” discounts on specific product categories. Whether you’re looking to boost sales in a particular category, or simply offer a compelling deal to attract more customers, our simple code snippets make customizing BOGO deals in WooCommerce easy and versatile.

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