unset Address 2 Field Return Undefined in Cart Page
-
I’m trying to remove address 2 everywhere (Checkout, My Account > Addresses) and reorder some others following the documentation from https://woocommerce.com/document/tutorial-customising-checkout-fields-using-actions- and-filters/ and other forums on the internet.
The field is removed, but I noticed an error. When I add a product and go to the cart page to simulate shipping, the address is shown correctly, but when I go to the checkout page and return to the cart page, the undefined value is added to the address. This only happens because of the removal of address 2.
I’ve already tried using the woocommerce_default_address_fields, woocommerce_billing_fields, woocommerce_shipping_fields and woocommerce_checkout_fields filters. I’ve also disabled plugins that may be related to WooCommerce.
This is the most recent code from my attempts:
add_filter( 'woocommerce_billing_fields' , 'custom_billing_fields' ); function custom_billing_fields( $fields ) { unset($fields ['billing_company']); $fields['billing_neighborhood']['label'] = 'Bairro'; $fields['billing_number']['class'] = array( 'form-row-first', 'address-field' ); $fields['billing_phone']['class'] = array( 'form-row-first', 'address-field' ); $fields['billing_cpf']['class'] = array( 'form-row-last', 'person-type-field', 'validate-required' ); $fields['billing_neighborhood']['class'] = array( 'form-row-first', 'address-field' ); $fields['billing_email']['priority'] = 21; $fields['billing_phone']['priority'] = 22; $fields['billing_number']['priority'] = 60; $fields['billing_neighborhood']['priority'] = 75; $fields['billing_neighborhood']['required'] = true; return $fields; } add_filter( 'woocommerce_shipping_fields' , 'custom_shipping_fields' ); function custom_shipping_fields( $fields ) { unset($fields ['shipping_company']); $fields['shipping_neighborhood']['label'] = 'Bairro'; $fields['shipping_number']['class'] = array( 'form-row-first', 'address-field' ); $fields['shipping_neighborhood']['class'] = array( 'form-row-first', 'address-field' ); $fields['shipping_number']['priority'] = 60; $fields['shipping_neighborhood']['priority'] = 75; $fields['shipping_neighborhood']['required'] = true; return $fields; } add_filter( 'woocommerce_default_address_fields', 'custom_default_address_fields' ); function custom_default_address_fields( $fields ) { unset($fields['address_2']); if(is_account_page() || is_checkout()) { $fields['postcode']['class'] = array( 'form-row-first', 'address-field' ); $fields['address_1']['class'] = array( 'form-row-last', 'address-field' ); $fields['city']['class'] = array( 'form-row-last', 'address-field' ); $fields['state']['class'] = array( 'form-row-last', 'address-field' ); } return $fields; }
I’ve tried using $unset($fields[‘billing_address_2’]); in the ‘woocommerce_billing_fields’ filter, $unset($fields[‘shipping_address_2’]); in the filter ‘woocommerce_shipping_fields’ and $unset($fields[‘billing’][‘billing_address_2’]); $unset($fields[‘shipping’][‘shipping_address_2’]); in the ‘woocommerce_checkout_fields’ filter.
None of the attempts worked. Every time I recalculate in the cart, the address appears correctly (but I noticed that it only asks/shows the city). When I go to the checkout page and return to the cart, the street address is also added, but with the undefined that comes from address 2.
- The topic ‘unset Address 2 Field Return Undefined in Cart Page’ is closed to new replies.