根据所选支付网关的邮政编码拒绝 WooCommerce Checkout

如果且仅当客户选择该选项时,我正在尝试检查一系列邮政编码local_pickup。


我有一份本地取件选项 (local_pickup4) 的副本Home Delivery,我有邮政编码数组。


如果选择了本地取货选项,邮政编码不匹配,预期结果是收到错误通知。


add_action('woocommerce_after_checkout_validation', 'zip_code_validator_for_local_home_delivery', 10, 2 );

function zip_code_validator_for_local_home_delivery( $data, $error ) {        


    $del_zones_array = array( 30030, 30032, 30033 );


    $chosen_shipping = WC()->session->get( 'chosen_shipping_methods' )[0];


    $chosen_shipping = explode(':', $chosen_shipping);


    if ( $chosen_shipping[0] == 'local_pickup:4' && !in_array( $data['shipping_postcode'], $del_zones_array ) ) {

        $error->add( 'validation', 'We\'re sorry, but this address is outside of our online order delivery area.<br>Please contact us for assistance or choose the local pickup option.' );

    }

}


四季花海
浏览 116回答 1
1回答

慕少森

显示错误信息需要满足的条件:(在代码中作为注释添加的解释)必须选择当地取货。邮政编码不得存在于预定邮政编码数组中function zip_code_validator_for_local_home_delivery( $data, $error ) {&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; /* Settings */&nbsp; &nbsp; // Zones&nbsp; &nbsp; $del_zones_array = array( 30030, 30032, 30033 );&nbsp; &nbsp; // Shipping method&nbsp; &nbsp; $shipping_method = 'local_pickup:4';&nbsp; &nbsp; /* End settings */&nbsp; &nbsp; // Get chosen shipping method ID&nbsp; &nbsp; $chosen_shipping_method_id = WC()->session->get( 'chosen_shipping_methods' )[0];&nbsp; &nbsp; // If local pickup is the chosen shipping method && ZIP code NOT in array&nbsp; &nbsp; if ( $chosen_shipping_method_id == $shipping_method && !in_array( $data['shipping_postcode'], $del_zones_array ) ) {&nbsp; &nbsp; &nbsp; &nbsp; $error->add( 'validation', 'We\'re sorry, but this address is outside of our online order delivery area.<br>Please contact us for assistance or choose the local pickup option.' );&nbsp; &nbsp; }}add_action('woocommerce_after_checkout_validation', 'zip_code_validator_for_local_home_delivery', 10, 2 );
打开App,查看更多内容
随时随地看视频慕课网APP