Are you looking to improve the functionality of your WooCommerce store and provide a BOGO “Buy One Get One” offer for your customers? 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 );
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.
![How to Apply BOGO (Buy One Get One)Offer Based on Product Tags in WooCommerce Cart? 1 How to Apply BOGO (Buy One Get One)Offer Based on Product Tags in WooCommerce Cart? - Tyche Softwares](https://media.tychesoftwares.com/wp-content/uploads/WooCommerce-Creating-tags-on-Backend-Side.png)
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.
![How to Apply BOGO (Buy One Get One)Offer Based on Product Tags in WooCommerce Cart? 2 How to Apply BOGO (Buy One Get One)Offer Based on Product Tags in WooCommerce Cart? - Tyche Softwares](https://media.tychesoftwares.com/wp-content/uploads/WooCommerce-assigning-the-tags-on-backend-1024x425.png)
- 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.
![How to Apply BOGO (Buy One Get One)Offer Based on Product Tags in WooCommerce Cart? 3 How to Apply BOGO (Buy One Get One)Offer Based on Product Tags in WooCommerce Cart? - Tyche Softwares](https://media.tychesoftwares.com/wp-content/uploads/WooCommerce-Selecting-the-Product-tags-from-backend-1024x345.png)
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.
![How to Apply BOGO (Buy One Get One)Offer Based on Product Tags in WooCommerce Cart? 4 How to Apply BOGO (Buy One Get One)Offer Based on Product Tags in WooCommerce Cart? - Tyche Softwares](https://media.tychesoftwares.com/wp-content/uploads/WooCommerce-BOGO-based-on-product-tags-1024x621.png)
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.