2017-09-14 71 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; 
} 
相關問題