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

How to Automatically Add Free Product & Apply Discount Based on Cart Subtotal in WooCommerce?

With this WooCommerce customization, you can easily enhance your store’s promotional strategy by automatically adding free product and apply discounts based on cart total thresholds. This way you can encourage customers to spend more at the same time rewarding them with discounts will lead to customer satisfaction.

Solution: Automatically Add Free Product & Apply Discount Based on Cart Subtotal in WooCommerce


The purpose of the code is to automatically add a free product (Product B) to the cart when the total value of items in the cart exceeds $100. Additionally, if the free product is already in the cart, a 20% discount is applied to that product.

function ts_aapc_add_product_to_cart() {
   

    $cart_total = 500; // Replace 500 with the desired amount for which the free product should be added to the cart.
    $discount_percentage = 50;
    $free_product_id = 470; // Product Id of the free product which will get added to the cart

    // Check if the cart total meets the condition
    if ( WC()->cart->get_cart_contents_total() >= $cart_total ) {
        $found = false;

        // Check if the free product is already in the cart
        foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
            $_product = $cart_item['data'];
            if ( $_product->get_id() == $free_product_id ) {
                $found = true;
                // Apply a 50% discount on the added free product
                $discount_amount = $_product->get_price() * $discount_percentage / 100;
                $_product->set_price( $_product->get_price() - $discount_amount );
                // Break the loop once the free product is found
                break;
            }
        }

        // If the free product is not found, add it
        if ( ! $found ) {
            $free_product = wc_get_product( $free_product_id );
            WC()->cart->add_to_cart( $free_product_id );

            
        }
    }
}

add_action( 'template_redirect', 'ts_aapc_add_product_to_cart', 20 ); // Priority set to 20 to ensure it runs after WooCommerce calculates totals
flexi bogo cta banner image


This to the shop owners who are running or planning to run BOGO offers on their WooCommerce store…

BOGO deals are great for increasing your sales, but have you thought about which offers are bringing you more revenue and which offers are not performing that great?

Don’t just set a BOGO deal, track the revenue generated by your deals in real-time with the Flexi BOGO for WooCommerce plugin.

Output

Let’s look into the actual product price of product B which is applied with the discount price.

How to Automatically Add Free Product & Apply Discount Based on Cart Subtotal in WooCommerce? - Tyche Softwares


When the customer adds the items with values that exceed the defined cart subtotal amount(in this case, $100), then product B is automatically added to the cart with the discount of 20% being applied to it.

How to Automatically Add Free Product & Apply Discount Based on Cart Subtotal in WooCommerce? - Tyche Softwares

Similarly, you can implement more innovative promotional strategies like implementing buy 1 get 2 free action in WooCommerce based on categories or other scenarios. Have a specific promotion in mind? Let us know in the comments!

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

Share It:

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