php - 在 WooCommerce 中结帐期间预定义的运输邮政编码验证
问题描述
我希望达到的目标
我正在尝试在结帐时添加一个控件,确保在选择“运送到另一个地址”时提供正确的邮政编码。
我的代码的问题
我正在使用的代码没有任何反应。没有消息,什么都没有。无论如何都可以下订单。
我的问题
我的代码有什么问题或错误/错误在哪里?
到目前为止我的代码
add_action( 'woocommerce_after_checkout_validation' , 'deny_outside_zone_message', 10, 2,);
function deny_outside_zone_message( $fields, $errors ) {
// the accepted delivery zones
$del_zones_array = array('30030', '30032', '30033');
// check if the postal code (billing or shipping) if within the array
if (! in_array('shipping_postcode', $del_zones_array ) ) {
// if the postal is not within the array, deny checkout
echo "The ZIP you provided is not available for oneline deliveries.";
return;
}
}
解决方案
shipping_postcode
未在您的代码中设置
// Validate
function action_woocommerce_after_checkout_validation( $data, $error ) {
// The accepted delivery zones
$del_zones_array = array( 30030, 30032, 30033 );
// If the postal is not within the array, deny checkout
if( ! in_array( $data['shipping_postcode'], $del_zones_array ) ) {
$error->add( 'validation', 'The ZIP you provided is not available for oneline deliveries.' );
}
}
add_action('woocommerce_after_checkout_validation', 'action_woocommerce_after_checkout_validation', 10, 2 );
推荐阅读
- javascript - 在JavaScript中的两点之间画线?
- numpy - 从 Tensorflow 中的嵌入列获取嵌入向量
- java - 依赖管理和构建管理工具有什么区别?
- pyomo - 如何根据预先确定的数据设置有条件地将变量添加到 Pyomo 约束
- flutter - 我想在我的 Flutter 应用程序中创建一个页面,允许用户使用属性过滤器搜索其他用户配置文件
- javascript - 正则表达式 $1 符号读取一次错误
- windows - “gwmi win32_quickfixengineering”但只需要 InstalledOn 日期
- css - 如何将特定的 css 应用于 Django 登录表单
- python - 将 python requests.post 转换为 CURL 语句
- python - 在 python 中构建和反转列表(或多切片列表)的最简洁方法