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

How to Apply 50% Discount for Every Nth Order in WooCommerce?

If you run an online store, you know how crucial it is to make customers come back to your store. Though it is not so easy you can try such effective BOGO strategy such as offering special discounts for repeat purchases.

Imagine a loyal customer who has made 10 purchases at your store—rewarding their loyalty with a free gift encourages further purchases. With this WooCommerce customization, you can easily provide a 50% discount on the cart total for customers on every 10th order.

Solution: Apply 50% Discount for Every Nth Order in WooCommerce

This code provides a 50% discount on the cart total for users who have made at least 10 orders and this discount gets repeated for every 10th order placed by that customer.

function ts_action_woocommerce_cart_calculate_fees($cart) {
    if (is_admin() && !defined('DOING_AJAX')) return;

    // Gets the current user's ID.
    $user_id = get_current_user_id();

    // User ID exists
    if ($user_id >= 1) {
        // Get the total orders by a customer.
        $order_count = wc_get_customer_order_count($user_id);

        // Starting from the 10th order
        if ($order_count >= 10 && $order_count % 10 == 0) {
            // Set the discount to 50%
            $discount = $cart->cart_contents_total * 0.5; // 50% discount

            // Only on cart page
            if (is_cart()) {
                // Message
                $message = sprintf(__('Congratulations! You have a 50%% discount applied. This is your %sth order', 'woocommerce'), $order_count + 1);

                // Check if a notice has already been added
                if (!wc_has_notice($message)) {
                    wc_clear_notices();
                    wc_add_notice($message, 'notice');
                }
            }

            // Set the discount (For discounts (negative fee) the taxes are always included)
            $cart->add_fee(__('Discount', 'woocommerce') . " (50%)", -$discount);
        }
    }
}
add_action('woocommerce_cart_calculate_fees', 'ts_action_woocommerce_cart_calculate_fees', 10, 1);

Output

Upon placing the 11th order, if a customer adds products to the cart, a 50% discount is applied based on the total amount in the cart. along with a notice message. The discount is applied for every set of 10 orders placed by the customer.

How to Apply 50% Discount for Every Nth Order in WooCommerce? - Tyche Softwares

Start implementing different BOGO strategies such as to implement buy 2 get 50 off on second item in WooCommerce which will be applying the discount to specific items based on the quantity.

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