Remove postcode from Woocommerce cart shipping calculator - php

In Woocommerce, I have been able to remove postcode checkout field using this code:
add_filter( 'woocommerce_default_address_fields', 'custom_override_default_address_fields' );
function custom_override_default_address_fields( $address_fields ) {
unset($address_fields['postcode']);
return $address_fields;
}
How can I remove the postcode field from the shipping calculator in cart page?

This can be done adding the following:
add_filter( 'woocommerce_shipping_calculator_enable_postcode', '__return_false' );
Code goes in function.php file of your active child theme (or active theme). tested and works.

add_filter( 'woocommerce_default_address_fields', 'custom_override_address_fields', 999, 1 );
function custom_override_address_fields( $address_fields ) {
// set as not required
$address_fields['postcode']['required'] = false;
// remove validation
unset( $address_fields['postcode']['validate'] );
return $address_fields;
}
(this is the real code that devalidates the postcode)

Related

Only one currently added product into Woocommerce cart?

I would like Woocommerce to only allow 1 product in the cart. If a product is already in the cart and another one is added then it should remove the previous one.
I found this code on net:
/**
* When an item is added to the cart, remove other products
*/
function custom_maybe_empty_cart( $valid, $product_id, $quantity ) {
if( ! empty ( WC()->cart->get_cart() ) && $valid ){
WC()->cart->empty_cart();
wc_add_notice( 'Only allowed 1 item in cart, please remove previous
item.', 'error' ); // here instead popup notice need to remove prevous added product
}
return $valid;
}
add_filter( 'woocommerce_add_to_cart_validation', 'custom_maybe_empty_cart',
10, 3 );
But it seems that is not working as I wanted. It need to autoremove previously added product in cart, and add latest added product in cart. Can someone to give me tip what need to update on class to work in latest woo 3.3.X ?
This one is the more compact and actual code as global $woocommerce is not needed anymore:
add_filter( 'woocommerce_add_to_cart_validation', 'auto_empty_cart', 20, 3 );
function auto_empty_cart( $passed, $product_id, $quantity ) {
if( WC()->cart->is_empty() ) return $passed; // Exit
WC()->cart->empty_cart();
return $passed;
}
Code goes in function.php file of your active child theme (or active theme).
Tested and works in all Woocommerce versions since 2.6.x
This working perfecty on Woocommerce 3.3.X
add_filter( 'woocommerce_add_to_cart_validation',
'bbloomer_only_one_in_cart', 99, 2 );
function bbloomer_only_one_in_cart( $passed, $added_product_id ) {
global $woocommerce;
// empty cart: new item will replace previous
$woocommerce->cart->empty_cart();
// display a message if you like
wc_add_notice( 'Product added to cart!', 'notice' );
return $passed;
}

Change a checkout billing field placeholder in Woocommerce

I'm trying to change the fields placeholder of "billing address 2" in the checkout page, using the following code:
add_filter( 'woocommerce_checkout_fields' , 'custom_override_checkout_fields',9999 );
// Our hooked in function - $fields is passed via the filter!
function custom_override_checkout_fields( $fields ) {
$fields['billing']['billing_address_2']['placeholder']="dssfsd";
return $fields;
}
it changes only for a moment than return back to it's defualt value.
see the following video (10s):
https://www.youtube.com/watch?v=-qOZ67gFQ98
The only way to get this is using woocommerce_default_address_fields, but it will change both billing and shipping placeholder for Address 2 checkout fields:
add_filter( 'woocommerce_default_address_fields', 'custom_override_default_checkout_fields', 10, 1 );
function custom_override_default_checkout_fields( $address_fields ) {
$address_fields['address_2']['placeholder'] = __( 'dssfsd', 'woocommerce' );
return $address_fields;
}
Code goes in function.php file of your active child theme (or active theme).
Tested and works
Now if you want to change only the Address 2 billing field placeholder you will:
Unset default Address 2 field placeholder (for both shipping and billing fields)
Customize only billing Address 2 field placeholder
Put back shipping Address 2 field placeholder
Here is the code on two hooked function:
add_filter( 'woocommerce_default_address_fields', 'custom_override_default_checkout_fields', 10, 1 );
function custom_override_default_checkout_fields( $address_fields ) {
// Remove labels for "address 2" shipping fields
unset($address_fields['address_2']['placeholder']);
return $address_fields;
}
add_filter( 'woocommerce_checkout_fields', 'custom_override_checkout_fields', 90, 1 );
function custom_override_checkout_fields( $fields ) {
// Add custom billing "address 2" label
$fields['billing']['billing_address_2']['placeholder'] = __( 'dssfsd', 'woocommerce' );
// Put back shipping "address 2" label
$fields['shipping']['shipping_address_2']['placeholder'] = __( 'Apartment, suite, unit etc. (optional)', 'woocommerce' );
return $fields;
}
Code goes in function.php file of your active child theme (or active theme).
Tested and works

WooCommerce: Pre-select and restrict to one state and city on checkout

I'm setting up a store that only sells to one city. I'm trying to figure out how I can restrict the city option to a pre-filled field.
I've already limited the country and using the following code in my functions.php:
add_filter( 'default_checkout_country', 'change_default_checkout_country' );
add_filter( 'default_checkout_state', 'change_default_checkout_state' );
function change_default_checkout_country() {
return 'PK'; // country code
}
function change_default_checkout_state() {
return 'SD'; // state code
}
And I've pre-selected the state using the following:
add_filter( 'woocommerce_states', 'bbloomer_custom_woocommerce_states' );
function bbloomer_custom_woocommerce_states( $states ) {
$states['PK'] = array(
'SD' => 'Sindh',
);
return $states;
}
But... Now comes the city and I am lost. I've tried using $fields['billing']['billing_city'] as well as $fields['billing']['billing_city']['value'] to no avail.
I want the customer to be limited to Karachi. I am not very familiar with WooCommerce filters, so I need help on achieving this.
Since woocommerce 3, default_checkout_country and default_checkout_state filter hooks are now deprecated and replaced.
Also in your last function, if you want to restrict just to one city, you should not include your city in the array of states. Instead you should return only one possible value.
So this will be your code:
// default checkout country
add_filter( 'default_checkout_billing_country', 'change_default_checkout_country' );
add_filter( 'default_checkout_shipping_country', 'change_default_checkout_country' );
function change_default_checkout_country() {
return 'PK'; // country code
}
// default checkout state
add_filter( 'default_checkout_billing_state', 'change_default_checkout_state' );
add_filter( 'default_checkout_shipping_state', 'change_default_checkout_state' );
function change_default_checkout_state() {
return 'SD'; // state code
}
// Setting one state only
add_filter( 'woocommerce_states', 'custom_woocommerce_state', 10, 1 );
function custom_woocommerce_state( $states ) {
// Returning a unique state
return array('PK' => array('SD' => 'Sindh'));
}
// Only one city at checkout
add_filter( 'woocommerce_checkout_fields', 'custom_checkout_fields', 10, 1 );
function custom_checkout_fields( $fields ) {
$fields['billing']['billing_city']['type'] = 'select';
$fields['billing']['billing_city']['options'] = array('Karachi' => 'Karachi');
$fields['shipping']['shipping_city']['type'] = 'select';
$fields['shipping']['shipping_city']['options'] = array('Karachi' => 'Karachi');
return $fields;
}
The Code goes in function.php file of your active child theme (or theme) or also in any plugin file.
This code is tested and works for wooCommerce versions 3+
Once added the code above and saved, you will need in WooCommerce > Settings > General to set locations this way:
Then you will get something like this on checkout:
Both dropdown have only one value… So you get what you are expecting.

Removing the password field in woocommerce checkout page

I am trying to remove the password field in the checkout page.
Here is what I have tried (code is in my functions.php theme file):
// Hook in
add_filter( 'woocommerce_default_address_fields' , 'custom_override_default_address_fields' );
// Our hooked in function - $address_fields is passed via the filter!
function custom_override_default_address_fields( $address_fields ) {
$checkout_fields['account']['required'] = false;
return $checkout_fields;
}
But it didn't work.
What is the hook for removing the password field in woocommerce checkout page?
Thanks.
The hook woocommerce_default_address_fields is only used for default address fields and not for the account fields.
To make the passwords fields optional you should need using woocommerce_checkout_fields hook, this way:
add_filter( 'woocommerce_checkout_fields' , 'custom_override_checkout_fields' );
function custom_override_checkout_fields( $fields ) {
// There is 2 password fields
fields['account']['account_password']['required'] = false;
fields['account']['account_password-2']['required'] = false;
return $fields;
}
To remove those passwords fields you should need to use unset() PHP function this way:
add_filter( 'woocommerce_checkout_fields' , 'custom_override_checkout_fields' );
function custom_override_checkout_fields( $fields ) {
unset($fields['account']['account_password']);
unset($fields['account']['account_password-2']);
return $fields;
}
But I am not sure that is really possible, as it's something mandatory in WooCommerce checkout process, when you have enabled the option to register in checkout page…
Code goes in function.php file of your active child theme (or theme) or also in any plugin file.
Official Reference: Customizing checkout fields using actions and filters

Woocommerce remove asterisk from required fields on checkout page

Is there a way to remove the required asterisk from a billing field on the checkout page in php? I have the following code which isn't working.
add_filter( 'woocommerce_checkout_fields' , 'customize_fields' );
function customize_fields( $fields ) {
$fields['billing']['billing_address_2']['required'] = false;
return $fields;
}
It would probably be easier to do with CSS, and be cleaner and better for screen-readers:
.woocommerce-checkout abbr.required {
display: none;
}
The .woocommerce-checkout is a body class that is only appended to the checkout page so it won't affect any other woo page that might have the .required class in an abbr element.
To make the field not required with a function:
// Hook in
add_filter( 'woocommerce_default_address_fields' , 'custom_override_default_address_fields' );
// Our hooked in function - $address_fields is passed via the filter!
function custom_override_default_address_fields( $address_fields ) {
$address_fields['address_2']['required'] = false;
return $address_fields;
}
There are two filters to manage checkout fields. You can use woocommerce_checkout_fields filter to make field "not-required", but the red asterisk will not be removed.
When dealing with default address fields with woocommerce_checkout_fields filter, some of your changes will not take effect, because woocommerce_default_address_fields filter and its default values may override your changes.
Only partly functional code:
function custom_override_checkout_fields( $fields ) {
$fields['billing']['billing_address_1']['required'] = false ;
return $fields;
}
add_filter( 'woocommerce_checkout_fields' , 'custom_override_checkout_fields' );
Address field (#1) is not required anymore, but still has red asterisk.
Fully functional code:
function custom_override_default_address_fields( $address_fields ) {
$address_fields['address_1'][ 'required' ] = false;
return $address_fields;
}
add_filter( 'woocommerce_default_address_fields' , 'custom_override_default_address_fields' );
Now this field is not required, and red asterisk is gone.
Documentation says that "In some specific cases you will need to use the woocommerce_default_address_fields filter. This filter is applied to all billing and shipping default fields."
You have to add the following code to your functions.php file. The example below is for postal code:
/* Seteaza campul Cod postal ne-obligatoriu */
add_filter( 'woocommerce_billing_fields', 'wc_npr_filter_postcode', 10, 1 );
function wc_npr_filter_postcode( $address_fields ) {
$address_fields['billing_postcode']['required'] = false;
return $address_fields;
}
/* End - Seteaza campul Cod postal ne-obligatoriu */

Categories