In WooCommerce, you can showcase featured products in different parts of your online store for various strategic reasons, such as promoting best-sellers, new arrivals, or high-margin items. Additionally, you can also tie up BOGO “Buy One Get One” offers to these products to draw customer attention to such products with the help of the code snippet given below.
add_action('woocommerce_cart_calculate_fees', 'ts_bogo_featured_products'); function ts_bogo_featured_products($cart) { if (is_admin() && !defined('DOING_AJAX')) { return; } // Check if the cart is empty if ($cart->is_empty()) { return; } // Initialize variables for the featured product $free_product_id = 470; // Replace with your free product ID // Check if the cart contains any featured product $featured_product_in_cart = false; foreach ($cart->get_cart() as $cart_item) { $product = $cart_item['data']; if ($product->is_featured()) { $featured_product_in_cart = true; break; } } // If any featured product is in the cart, add the free product if ($featured_product_in_cart) { // Check if the free product is not already in the cart $free_product_in_cart = false; foreach ($cart->get_cart() as $cart_item) { if ($cart_item['product_id'] == $free_product_id && !$cart_item['data']->is_featured()) { $free_product_in_cart = true; break; } } // If the free product is not in the cart, add it if (!$free_product_in_cart) { $cart->add_to_cart($free_product_id, 1); } } }
Let’s look into how a product is set up as a featured product in WooCommerce.
- Go to your WordPress admin dashboard.
- Navigate to the “Products” section.
- Find the product you want to feature.
- Click on the star icon next to the product. When the product is featured, the star icon will be filled in.
In the below image, there are examples of some of the high-value items that are being marked as featured products.
Output
When the customer adds any featured product to the cart, the free product also gets automatically added to the cart as shown below.
Likewise, you can also add buy one get one offer for downloadable products in WooCommerce.