I'm trying to disable the bacs gateway if the customers cart total is over £500.
I have this code here which I've placed into my storefront functions.php file
add_filter('woocommerce_available_payment_gateways','filter_gateways',1);
function filter_gateways($gateways){
global $woocommerce;
$min_cart_total = 500;
$cart_total = ($woocommerce->cart->get_cart_total());
$cart_total = ereg_replace("[^0-9]", "", $cart_total)/100;
if ($cart_total > $min_cart_total){
unset($gateways['bacs']);
}
return $gateways;
}
But the options for bacs still shows up at the end even if my cart total is £250 for example.
Is there anything I'm doing wrong here?
I'm not sure about your ereg_replace
code, but I think you should for sure be using a later priority:
add_filter('woocommerce_available_payment_gateways','filter_gateways', 20);
function filter_gateways($gateways){
$min_cart_total = 500;
$cart_total = WC()->cart->cart_contents_total; // the total without wc_price formatting
if ($cart_total > $min_cart_total){
unset($gateways['bacs']);
}
return $gateways;
}
Your code was running first, and the Woo code was probably running later and adding the gateway back to the available gateways.
Doesn't impact anything yet, but the global $woocommerce
is deprecated in favor of WC()
.