I am looking for a way, to limit coupon usage and to display an error message if a customer have used related coupons in previous orders before in WooCommerce.
By related coupons, I mean: couponcodes that appear in a predefined array along with the currently inserted coupon on the cart/checkout page. On that basis, compare.
My code attempt:
add_filter( 'woocommerce_coupon_is_valid', 'specific_coupons_valid', 10,
3 );
function specific_coupons_valid( $is_valid, $coupon, $discount ){
$coupon_codes = array( 'free10', 'free20', 'free30' );
$args = array(
'posts_per_page' => -1,
'orderby' => 'title',
'order' => 'asc',
'post_type' => 'shop_coupon',
'post_status' => 'publish',
);
$coupons = get_posts( $args );
if( in_array( $coupons, $coupon_codes ) ) {
$is_valid = false;
}
return $is_valid;
}
Any help is appreciated