Woocommerce 在结账时禁用运输选项方法
Woocommerce disable the shipping option methods in checkout
在结帐页面上,我试图根据我所在地区的邮政编码禁用送货方式选项。 You can find image here。我正在使用 Wordpress 和 PHP 作为后端。
如果邮政编码与地区不匹配,送货方式将被禁用,文本“”将显示在这里。我正在使用此代码:
$packages = WC()->cart->get_shipping_packages();
foreach ($packages as $key => $value) {
$shipping_session = "shipping_for_package_$key";
unset(WC()->session->$shipping_session);
}
但此代码不起作用,它不会禁用送货方式选项。谁能帮帮我?
您可以通过 woocommerce_package_rates
挂钩禁用送货方式。
您可以通过钩子的第二个参数获取邮编:$package
.
在下面的示例中,如果 邮政编码 与 $postcodes
数组中的其中一个不匹配,则禁用送货方式 (您也可以通过排除反向应用您的逻辑。如果需要,您还可以检查州和国家/地区。).
你可以通过$package
得到的所有字段是:
$package['destination']['country']
$package['destination']['state']
$package['destination']['postcode']
$package['destination']['city']
$package['destination']['address']
$package['destination']['address_1']
$package['destination']['address_2']
然后:
// disable shipping methods based on postcode
add_filter( 'woocommerce_package_rates', 'disable_shipping_method_based_on_postcode', 10, 2 );
function disable_shipping_method_based_on_postcode( $rates, $package ) {
// initialize postcodes to match
$postcodes = array( 12050, 20052, 15600, 45063 );
// if the customer's postcode is not present in the array, disable the shipping methods
if ( ! in_array( $package['destination']['postcode'], $postcodes ) ) {
foreach ( $rates as $rate_id => $rate ) {
unset( $rates[$rate_id] );
}
}
return $rates;
}
代码已经过测试并且可以工作。将它添加到您的活动主题 functions.php.
相关答案
- Disable shipping for specific products based on country in Woocommerce
在结帐页面上,我试图根据我所在地区的邮政编码禁用送货方式选项。 You can find image here。我正在使用 Wordpress 和 PHP 作为后端。
如果邮政编码与地区不匹配,送货方式将被禁用,文本“”将显示在这里。我正在使用此代码:
$packages = WC()->cart->get_shipping_packages();
foreach ($packages as $key => $value) {
$shipping_session = "shipping_for_package_$key";
unset(WC()->session->$shipping_session);
}
但此代码不起作用,它不会禁用送货方式选项。谁能帮帮我?
您可以通过 woocommerce_package_rates
挂钩禁用送货方式。
您可以通过钩子的第二个参数获取邮编:$package
.
在下面的示例中,如果 邮政编码 与 $postcodes
数组中的其中一个不匹配,则禁用送货方式 (您也可以通过排除反向应用您的逻辑。如果需要,您还可以检查州和国家/地区。).
你可以通过$package
得到的所有字段是:
$package['destination']['country']
$package['destination']['state']
$package['destination']['postcode']
$package['destination']['city']
$package['destination']['address']
$package['destination']['address_1']
$package['destination']['address_2']
然后:
// disable shipping methods based on postcode
add_filter( 'woocommerce_package_rates', 'disable_shipping_method_based_on_postcode', 10, 2 );
function disable_shipping_method_based_on_postcode( $rates, $package ) {
// initialize postcodes to match
$postcodes = array( 12050, 20052, 15600, 45063 );
// if the customer's postcode is not present in the array, disable the shipping methods
if ( ! in_array( $package['destination']['postcode'], $postcodes ) ) {
foreach ( $rates as $rate_id => $rate ) {
unset( $rates[$rate_id] );
}
}
return $rates;
}
代码已经过测试并且可以工作。将它添加到您的活动主题 functions.php.
相关答案
- Disable shipping for specific products based on country in Woocommerce