为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

PHP
四季花海 2023-06-18 17:58:16
如果且仅当客户选择该选项时,我正在尝试检查一系列邮政编码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.' );    }}
查看完整描述

1 回答

?
慕少森

TA贡献2019条经验 获得超9个赞

显示错误信息需要满足的条件:(在代码中作为注释添加的解释)

  • 必须选择当地取货。

  • 邮政编码不得存在于预定邮政编码数组中

function zip_code_validator_for_local_home_delivery( $data, $error ) {        

    /* Settings */


    // Zones

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


    // Shipping method

    $shipping_method = 'local_pickup:4';


    /* End settings */


    // Get chosen shipping method ID

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


    // If local pickup is the chosen shipping method && ZIP code NOT in array

    if ( $chosen_shipping_method_id == $shipping_method && !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.' );

    }

}

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



查看完整回答
反对 回复 2023-06-18
  • 1 回答
  • 0 关注
  • 115 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信