基于运输方法的所需结账字段 - Woocommerce

时间:2017-09-14 10:11:14

标签: php wordpress woocommerce checkout

如果选择本地取件,我不想要billing_last_name

尝试这样的事情:

function xa_remove_billing_checkout_fields($fields) {
    $shipping_method ='local_pickup'; // Set the desired shipping method to hide the checkout field(s).
    global $woocommerce;
    $chosen_methods = WC()->session->get( 'chosen_shipping_methods' );
    $chosen_shipping = $chosen_methods[0];

    if ($chosen_shipping == $shipping_method) {
       $fields['billing']['billing_last_name'][ 'required' ] = false;
    }
    return $fields;
}

但它不起作用。

有适当的解决方案吗?

1 个答案:

答案 0 :(得分:2)

这是您使用钩子更新的代码:

add_filter('woocommerce_checkout_fields', 'xa_remove_billing_checkout_fields');

function xa_remove_billing_checkout_fields($fields) {
    $shipping_method ='local_pickup'; // Set the desired shipping method to hide the checkout field(s).
    global $woocommerce;
    $chosen_methods = WC()->session->get( 'chosen_shipping_methods' );
    $chosen_shipping = $chosen_methods[0];

    if ($chosen_shipping == $shipping_method) {
       $fields['billing']['billing_last_name'][ 'required' ] = false;
    }
    return $fields;
}