【问题标题】:Which method or action hook is fired when changing shipping method on checkout page?在结帐页面上更改运输方式时会触发哪个方法或操作挂钩?
【发布时间】:2016-08-31 16:43:19
【问题描述】:

当用户选择“本地取货”以外的送货方式时,我需要将地址字段更改为“必填”。

我使用过滤器将其设置为默认可选(默认选择本地取货),如下所示:

add_filter( 'woocommerce_checkout_fields', 'disable_required_address_shipping' );

function disable_required_address_shipping($fields ) {
    if ( check_if_local_pickup() ) {

        $fields['billing']['billing_address_1']['required'] = false;
        $fields['billing']['billing_address_2']['required'] = false;
        $fields['billing']['billing_city']['required'] = false;
    }

    return $fields;

}

function check_if_local_pickup() {
    $chosen_methods = WC()->session->get( 'chosen_shipping_methods' );
    $chosen_shipping = $chosen_methods[0]; 
    if ($chosen_shipping == 'local_pickup') { 
        return true;
    }
}

但我需要使用操作动态更改它。

我试图在这里寻找解决方案,但找不到。

更改运输方式时是否会触发任何操作?
有什么想法吗?

【问题讨论】:

    标签: php wordpress woocommerce checkout shipping


    【解决方案1】:

    要在运输方式更改时触发操作,您可以使用do_action( 'woocommerce_shipping_method_chosen', $chosen_method );

    do_action( 'woocommerce_shipping_method_chosen', 'check_if_local_pickup', 10, 1 );
    function check_if_local_pickup( $chosen_method ) {
        $chosen_methods = WC()->session->get( 'chosen_shipping_methods' );
        $chosen_shipping = $chosen_methods[0]; 
        if ($chosen_shipping == 'local_pickup') { 
            return true;
        }
    }
    

    参考:
    woocommerce_shipping_method_chosen (hookr.io)
    Source code: class WC_Shipping (line 311)

    【讨论】:

      猜你喜欢
      • 2021-04-21
      • 2020-02-12
      • 2012-12-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-08-24
      • 1970-01-01
      相关资源
      最近更新 更多