我正在尝试禁用特定城市和州的货到付款方式,但存在一些问题,当我输入此代码时,添加到购物车等待圈不断循环。
function payment_gateway_disable_country( $available_gateways ) {
global $woocommerce;
$targeted_states = array('PB','IS','GB', 'KP');
//$targeted_states = array('PB','IS');
$user_state = $woocommerce->customer->get_billing_state();
if ( is_admin() ) return;
if (in_array($user_state, $targeted_states)) {
return $available_gateways;
}
if ( ($woocommerce->customer->get_billing_city() == "LAHORE") ||
($woocommerce->customer->get_billing_city() == "lahore") ||
($woocommerce->customer->get_billing_city() == "Lahore") ||
($woocommerce->customer->get_billing_city() == "lhr") ||
($woocommerce->customer->get_billing_city() == "islamabad") ||
($woocommerce->customer->get_billing_city() == "Islamabad") ||
($woocommerce->customer->get_billing_city() == "isl") ||
($woocommerce->customer->get_billing_city() == "ISLAMABAD") ||
($woocommerce->customer->get_billing_city() == "rawalpindi") ||
($woocommerce->customer->get_billing_city() == "rwp") ||
($woocommerce->customer->get_billing_city() == "Rawalpindi") ||
($woocommerce->customer->get_billing_city() == "RAWALPINDI")) {
return $available_gateways;
}
else {
unset( $available_gateways['cod']);
}
return $available_gateways;
}
add_filter( 'woocommerce_available_payment_gateways', 'payment_gateway_disable_country' );
答案 0 :(得分:1)
您的函数中存在一些错误。您确定要检查状态而不是 country ("PB", "IS", "GB", "KP")?
你可以像这样优化你的函数:
add_filter( 'woocommerce_available_payment_gateways', 'payment_gateway_disable_country' );
function payment_gateway_disable_country( $available_gateways ) {
if ( is_admin() ) {
return $available_gateways;
}
if ( ! isset( $available_gateways['cod'] ) ) {
return $available_gateways;
}
global $woocommerce;
$targeted_countries = array( 'PB','IS','GB', 'KP' );
$targeted_cities = array( 'LAHORE', 'lahore', 'Lahore', 'lhr', 'islamabad', 'Islamabad', 'isl', 'ISLAMABAD', 'rawalpindi', 'rwp', 'Rawalpindi', 'RAWALPINDI' );
if ( in_array( $woocommerce->customer->get_billing_country(), $targeted_countries ) ) {
unset( $available_gateways['cod'] );
}
if ( in_array( $woocommerce->customer->get_billing_city(), $targeted_cities ) ) {
unset( $available_gateways['cod'] );
}
return $available_gateways;
}
代码已经过测试并且可以工作。将它添加到您的活动主题的functions.php 中。