Are you looking to encourage bulk purchases with the BOGO offer or “Buy Two Get one free” offers in your WooCommerce store? The below snippet adds a “Buy 2 Get 1 Free” offer to attract shoppers to specific products. This applies a free item for every two purchased items of targeted products.
add_action('woocommerce_cart_calculate_fees', 'ts_buy_two_get_one_free', 10, 1 ); function ts_buy_two_get_one_free( $cart ) { if ( is_admin() && ! defined( 'DOING_AJAX' ) ) return; // Set your targeted free product IDs $targeted_product_ids = array(34); $each_n_items = 2; // Number of items required to get a free one $discount = 0; // Initializing foreach ( $cart->get_cart() as $cart_item ) { if ( in_array( $cart_item['product_id'], $targeted_product_ids ) ) { $qty = intval( $cart_item['quantity'] ); // Calculate discount based on the number of items $free_items = floor( $qty / ($each_n_items + 1) ); $discount += $free_items * floatval( $cart_item['data']->get_price() ); } } if ( $discount > 0 ) { // Displaying a custom notice (optional) wc_clear_notices(); wc_add_notice( __("Congratulations! You've earned a free item for every 2 purchased items."), 'success'); // Apply the discount $cart->add_fee( __("Buy 2 Get 1 Free"), -$discount, true ); } }
Output
When a customer adds three of the same items, they will receive one item for free. The discount will be automatically applied to the cart totals section of the WooCommerce cart page as shown below.
Additionally, you can also automatically add a product to your WooCommerce cart page based on various conditions such as limiting the free product to one quantity and setting the free product price to zero.