BOGO offers aim to increase sales volume by providing customers with an additional item for free or at a discounted price. The purpose of this code is to provide storewide 25% discounts that are applied to all the products within a store. Discounts are applied on the cart page where the regular prices are modified to the discounted price.
function ts_apply_discount_to_all_products( $cart ) { // Apply a 25% discount all the time foreach ( $cart->get_cart() as $cart_item_key => $cart_item ) { // Get the product object $product = $cart_item['data']; // Calculate the 25% discount $discount_percentage = 0.25; $regular_price = $product->get_regular_price(); $discounted_price = $regular_price - ( $regular_price * $discount_percentage ); // Set the discounted price $product->set_price( $discounted_price ); } } // Hook to apply the discount to all products add_action( 'woocommerce_before_calculate_totals', 'ts_apply_discount_to_all_products' );
Output
Before seeing the output, let’s get a glance at the regular prices of the products that are discounted.
When customers add the products to the cart, the code calculates the discount using the regular price and the discounted price is displayed on the cart page as shown below.
In contrast to discounts, you can also provide bogo buy one get one offers when specific product is selected. Check here on how to create bogo but one get one offers based on product ids in WooCommerce.