2017-09-14 73 views
0

我想使billing_last_name不需要,如果选择本地取货。必填结帐字段根据送货方式 - Woocommerce

尝试这样的事:

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

但它不工作。

有没有合适的解决方案?

+0

这应该正常工作。请让我们看看钩子。你到哪里去了? –

+0

哦,我认为这是问题,我没有写钩子。我应该怎么做? –

+0

我已经为您提供了答案,请参阅第一行(add_filter)。 –

回答

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; 
} 
相关问题