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

How to Offer Store-wide Global Discount in WooCommerce?

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.

How to Offer Store-wide Global Discount in WooCommerce? - Tyche Softwares

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.

How to Offer Store-wide Global Discount in WooCommerce? - Tyche Softwares

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.

Browse more in: Code Snippets, WooCommerce How Tos, WooCommerce Tutorials

Share It:

Subscribe
Notify of
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x