必填结帐字段根据送货方式 - Woocommerce
问题描述:
我想使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;
}
但它不工作。
有没有合适的解决方案?
答
这是你更新的代码用钩子:
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;
}
这应该正常工作。请让我们看看钩子。你到哪里去了? –
哦,我认为这是问题,我没有写钩子。我应该怎么做? –
我已经为您提供了答案,请参阅第一行(add_filter)。 –