php - 在 WooCommerce 结帐期间验证预定义的运输邮政编码

标签 php wordpress validation woocommerce checkout

我希望实现什么

我正在尝试在结账时添加一个控件,确保在选择“运送到另一个地址”时提供正确的邮政编码。

我的代码有问题

我正在使用的代码没有任何反应。没有消息,什么也没有。无论如何都可以下订单。

我的问题

我的代码出了什么问题或者错误/错误在哪里?

到目前为止我的代码

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 );

关于php - 在 WooCommerce 结帐期间验证预定义的运输邮政编码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62477000/

相关文章:

php - 在 xslt 转换中保留节点

php - WordPress:从帖子 ID 获取作者信息

javascript - 如何以重力形式创建一个切换按钮,一旦在 WordPress 中从打开更改为关闭,该按钮就会卡住?

c# - 使用 UTF-8 解码文件流

java - struts2对话和验证

ios - swift : Cannot validate password

javascript - CodeIgniter ajax 上传图片(ajax 有效,而不是 CodeIgniter)

php - 在反向代理后面运行的 symfony 应用程序的 URL

PHP 复选框验证,if else 问题

css - 媒体查询不适用于 iPhone 6/6S