smart discounts for the fifth Customer order

Smart discount In WooCommerce Offer a discount for the fifth customer order

Apply 10% discount for every 5th order on WooCommerce – the trick to increase sales with smart discount on WooCommerce

This code offers a 10% discount on the entire cart for users who have made at least 5 orders, and this discount is repeated for every 5 customer orders.

To use this code, just add it to the end of the template function file


 

function Rezvan_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 5th order
        if ($order_count >= 5 && $order_count % 5 == 0) {
            // Set the discount to 10%
            $discount = $cart->cart_contents_total * 0.10; // 10% discount

            // Only on cart page
            if (is_cart()) {
                // Message
                $message = sprintf(__('Congratulations! You have a 10%% 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') . " (10%)", -$discount);
        }
    }
}
add_action('woocommerce_cart_calculate_fees', 'Rezvan_action_woocommerce_cart_calculate_fees', 10, 1);

Leave a Reply

Your email address will not be published. Required fields are marked *

ten − ten =

 
Online-Chat

We are ready to answer your questions