BOGO offers often incentivize customers to purchase more items and likewise, this customization will help you to encourage repeat purchases. 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.
Similarly, you can also implement buy 2 get 50 off on second item in WooCommerce applying the discount to specific items based on the quantity.