WooCommerce:使用筛选器挂钩禁用结帐字段 [英] WooCommerce: Disabling checkout fields with a filter hook

查看:62
本文介绍了WooCommerce:使用筛选器挂钩禁用结帐字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用 woocommerce_checkout_fields 过滤器挂钩同时禁用多个结帐字段的必需属性,但没有成功。

I have try to disable the "required" property of several checkout fields at the same time using woocommerce_checkout_fields filter hook, with no success.

插件也无法正常工作。这是我的代码:

Plugins are not working properly either. This is my code:

// Hook in
add_filter( 'woocommerce_checkout_fields' , 'custom_override_checkout_fields' );

// Our hooked in function - $fields is passed via the filter!
function custom_override_checkout_fields( $fields ) {
$fields['billing']['billing_address_1']['required'] = false;
$fields['billing']['billing_address_2']['required'] = false;

$fields['billing']['billing_postcode']['required'] = false;
$fields['billing']['billing_city']['required'] = false;
$fields['billing']['billing_phone']['required'] = false;

     return $fields;
}

怎么了?

如何实现?

What is wrong?
How can I achieve this?

推荐答案

您需要使用 unset ()函数,您可以这样做:

You need to use the unset() function for this purpose and you can do-it this way:

add_filter( 'woocommerce_checkout_fields' , 'custom_override_checkout_fields' );

function custom_override_checkout_fields( $fields ) {
    unset($fields['billing']['billing_address_1']);
    unset($fields['billing']['billing_address_2']);
    unset($fields['billing']['billing_postcode']);
    unset($fields['billing']['billing_city']);
    unset($fields['billing']['billing_phone']);
    return $fields;
}

您需要将此代码粘贴到位于的function.php文件中

您已经用更改了问题更新 :这是我的新答案

You have changed your question with an update: Here it is my new answer

使地址字段为可选(关于您的更新):

Making the address fields optional (regarding your update):

您必须为此使用其他过滤器挂钩。有2个不同的过滤器挂钩:

You have to use a different filter hook for that purpose. There are 2 different filter hooks:

1)对于计费字段:

add_filter( 'woocommerce_billing_fields', 'wc_optional_billing_fields', 10, 1 );
function wc_optional_billing_fields( $address_fields ) {
    $address_fields['billing_address_1']['required'] = false;
    $address_fields['billing_address_2']['required'] = false;
    $address_fields['billing_postcode']['required'] = false;
    $address_fields['billing_city']['required'] = false;
    $address_fields['billing_phone']['required'] = false;
    return $address_fields;
}

2)对于运输字段:

add_filter( 'woocommerce_shipping_fields', 'wc_optional_shipping_fields', 10, 1 );
function wc_optional_shipping_fields( $address_fields ) {
    $address_fields['shipping_phone']['required'] = false;
    return $address_fields;
}






要地址字段必填:

您可以使用上面相同的钩子和函数,为每个字段更改 false true

You can use the same hooks and functions above, changing for each field false to true.

您将需要将此代码粘贴到活动子主题或主题中的function.php文件中。

参考:使用挂钩自定义结帐字段

这篇关于WooCommerce:使用筛选器挂钩禁用结帐字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆