I need to give the customer a 10% discount after 10 orders are made, and next 10 and next 10.
I've found some code and modified it. So based on Apply a coupon programmatically in Woocommerce answer code, this is my code attempt:
///* 10% Discount for10 subsequent order and repeating for every 10th purchase
add_action('woocommerce_before_calculate_totals', 'discount_based_on_10 orders');
function discount_based_on_10_orders( $cart ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 )
return;
// Your settings
$coupon_code = '10 Or More Discount'; // Coupon code
$total_orders = 10; // 10th order discount
// Initializing variables
$applied_coupons = $cart->get_applied_coupons();
$coupon_code = sanitize_text_field( $coupon_code );
// Applying coupon
if( ! in_array($coupon_code, $applied_coupons) && $total_orders >= $10 ){
$cart->add_discount( $coupon_code );
wc_clear_notices();
wc_add_notice( sprintf(
__("Your have a surprise %s A discount has been Applied! $ You have reached your 10th order discount %s.", "woocommerce"),
wc_format_order( $order_number ), '10%', '<strong>' . wc_format_order({
$total_orders ) . '</strong>'
), "notice");
}
// Removing coupon
elseif( in_array($coupon_code, $applied_coupons) && $total_orders < $order_number ){
$cart->remove_coupon( $coupon_code );
}
}
However, this does not give the desired result. I have a coupon code 10 Or More Discount. I'm not sure how to test it and it's not my site so I'm reaching out for a quick learning curve.