制作结帐地址WooCommerce中不需要的字段

时间:2018-04-30 20:00:43

标签: php wordpress woocommerce checkout hook-woocommerce

在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似乎也没问题:

HTML markup screenshot

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

3 个答案:

答案 0 :(得分:5)

您应该使用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;
}

所有代码都包含在您的活动子主题(或活动主题)的function.php文件中。经过测试和工作。

答案 1 :(得分:1)

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/

答案 2 :(得分:0)

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