php 在WooCommerce中不需要结帐地址字段

0md85ypi  于 2023-06-04  发布在  PHP
关注(0)|答案(3)|浏览(167)

在Woocommerce我试图使结帐地址字段不需要下面的代码...但我有this error“请输入一个地址继续”,当提交结帐表格。
我的代码设置地址结帐字段不需要:

add_filter( 'woocommerce_billing_fields', 'wc_npr_filter_phone', 10, 1 
);
function wc_npr_filter_phone( $address_fields ) {
    $address_fields['billing_phone']['required'] = true;
    $address_fields['billing_country']['required'] = false;
    $address_fields['billing_last_name']['required'] = false;
    $address_fields['billing_city']['required'] = false;
    $address_fields['billing_postcode']['required'] = false;
    $address_fields['billing_email']['required'] = false;
    $address_fields['billing_state']['required'] = false;
    $address_fields['billing_address_1']['required'] = false;
    $address_fields['billing_address_2']['required'] = false;
    return $address_fields;

}

//make shipping fields not required in checkout
add_filter( 'woocommerce_shipping_fields', 
'wc_npr_filter_shipping_fields', 10, 1 );
function wc_npr_filter_shipping_fields( $address_fields ) {
    $address_fields['shipping_first_name']['required'] = false;
    $address_fields['shipping_last_name']['required'] = false;
    $address_fields['shipping_address_1']['required'] = false;
    $address_fields['shipping_address_2']['required'] = false;
    $address_fields['shipping_city']['required'] = false;
    $address_fields['shipping_country']['required'] = false;
    $address_fields['shipping_postcode']['required'] = false;
    $address_fields['shipping_state']['required'] = false;
    return $address_fields;
}

HTML似乎也不错:

如何在WooCommerce中不需要结帐地址字段?

zsohkypk

zsohkypk1#

您应该需要使用woocommerce_default_address_fields过滤器钩子,而不是解释here
替换代码:

// Billing and shipping addresses fields
add_filter( 'woocommerce_default_address_fields' , 'filter_default_address_fields', 20, 1 );
function filter_default_address_fields( $address_fields ) {
    // Only on checkout page
    if( ! is_checkout() ) return $address_fields;

    // All field keys in this array
    $key_fields = array('country','first_name','last_name','company','address_1','address_2','city','state','postcode');

    // Loop through each address fields (billing and shipping)
    foreach( $key_fields as $key_field )
        $address_fields[$key_field]['required'] = false;

    return $address_fields;
}

由于计费邮箱和电话已经默认为必填,如果您希望不需要,则需要增加此代码:

// For billing email and phone - Make them not required
add_filter( 'woocommerce_billing_fields', 'filter_billing_fields', 20, 1 );
function filter_billing_fields( $billing_fields ) {
    // Only on checkout page
    if( ! is_checkout() ) return $billing_fields;

    $billing_fields['billing_phone']['required'] = false;
    $billing_fields['billing_email']['required'] = false;
    return $billing_fields;
}

所有的代码都在你的活动子主题(或活动主题)的functions.php文件中。测试和作品。

dz6r00yl

dz6r00yl2#

唯一对我有帮助的是为国家添加了一个字段,并使其不可见(因为我不需要它)

uqcuzwp8

uqcuzwp83#

add_filter( 'woocommerce_checkout_fields', 'your_require_wc_phone_field');
function your_require_wc_phone_field( $fields ) {
    $fields['billing']['billing_phone']['required'] = false;
    return $fields;
}

这种方法使用'woocommerce_checkout_fields',当我在自己的网站上测试时,这是我可以将billing_phone字段更改为必填字段的唯一方法。
https://docs.woocommerce.com/document/tutorial-customising-checkout-fields-using-actions-and-filters/

相关问题