【问题标题】:Predefined shipping ZIP Code validation during checkout in WooCommerce在 WooCommerce 中结帐期间预定义的运输邮政编码验证
【发布时间】:2020-06-19 19:06:35
【问题描述】:

我希望达到的目标

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

我的代码有问题

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

我的问题

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

到目前为止我的代码

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

【问题讨论】:

    标签: php wordpress validation woocommerce checkout


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-05-31
      • 1970-01-01
      • 1970-01-01
      • 2016-10-20
      • 2017-08-02
      • 1970-01-01
      • 2021-11-08
      • 2021-03-19
      相关资源
      最近更新 更多