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

How to Apply BOGO (Buy One Get One)Offer Based on Product Tags in WooCommerce?


Are you looking to improve the functionality of your WooCommerce store and provide a BOGO “Buy One Get One” offer for your customers? No worries! Our simple guide helps you create enticing promotions such as setting up BOGO deals based on product tags to drive more traffic and increase your revenue!

Solution: Apply BOGO (Buy One Get One)Offer Based on Product Tags in WooCommerce

Then this code snippet gives you a solution to automatically add a designated free product to the shopping cart based on specific product tags.

function ts_aaptc_add_product_to_cart( $item_key, $product_id ) {
    $required_tags = array( 'electronics' ); // Add more required product tags here

    $product_tags = get_the_terms( $product_id, 'product_tag' );

    if ( ! is_admin() && ! empty( $product_tags ) ) {

        foreach ( $required_tags as $required_tag ) {

            foreach ( $product_tags as $term ) {

                if ( $term->slug === $required_tag ) {

                    $free_product_id = 132; // Product Id of the free product which will get added to cart
                    $found = false;

                    // Check if product is already in cart
                    if ( sizeof( WC()->cart->get_cart() ) > 0 ) {
                        foreach ( WC()->cart->get_cart() as $cart_item_key => $values ) {
                            $_product = $values['data'];
                            if ( $_product->get_id() == $free_product_id ) {
                                $found = true;
                                break;
                            }
                        }

                        // If product not found, add it
                        if ( ! $found ) {
                            WC()->cart->add_to_cart( $free_product_id );
                        }
                    } else {
                        // If no products in cart, add it
                        WC()->cart->add_to_cart( $free_product_id );
                    }

                    break 2; // Break out
                }
            }
        }
    }
}

add_action( 'woocommerce_add_to_cart', 'ts_aaptc_add_product_to_cart', 10, 2 );
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

Here are the steps for creating and assigning tags to products in the WooCommerce backend:

Creating Tags:

  • Go to Products > Select the Tags.
  • Enter the tag name in the “Name” field and click on “Add new tag” to save.

Assigning Tags to Products:

  • Go to Products > All Products, Find the product you want to add tags.
  • Click on the product name to edit it.
  • Scroll down to the “Product Data” section.
  • Click on the “Tags” tab, Start typing the name of the tag you want to add in the “Tags” field. 
  • Click “Update” to save your changes.

When the Customer adds electronic products like a Laptop to the cart page, then the free product (LED Television) gets automatically added to the cart based on the product tags as shown below.

Alternatively, you can also automatically add a product to your WooCommerce cart in various scenarios based on cart total, excluding expensive items from Buy One Get One offers, product categories, and more.

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