I would like to only show e.g. Stripe if coupon code 123 is used. So, I have come up with the code which is working, but I would also like to show a message at the top of the checkout, where the woo notices show. My message isn’t showing correctly.
// restrict coupon codes payment method
add_filter('woocommerce_available_payment_gateways', 'unset_gateway_by_applied_coupons');
function unset_gateway_by_applied_coupons($available_gateways) {
// prevents coupon error in admin
if( is_admin() )
return $available_gateways;
global $woocommerce;
$unset = false; // the norm
// names of coupons
$coupon_codes = array('test123', 'check123');
foreach($coupon_codes as $coupon_code) {
// if code is used in cart
if(in_array($coupon_code, WC()->cart->get_applied_coupons() )){
//echo 'found =' .$coupon_code;
unset($available_gateways['bacs']);
unset($available_gateways['invoice']);
// $message = sprintf( __('<br><p class="coupon-code">The coupon code "%s" can only use the Credit Card payment method.</p>'), $coupon_code); // <<<<< not working
}
}
return $available_gateways;
}