disable postcode validation in WooCommerce checkout page - woocommerce

Does anybody know how to disable the Postcode validation on the checkout page in WooCommerce?
I want to input 'text content' in "billing_postcode" field, but the shop says it's an invalid postcode.
Does anyone know how I can disable Postcode validation or allow text characters?

Just do this:
add_filter( 'woocommerce_checkout_fields' , 'custom_override_checkout_fields', 99 );
function custom_override_checkout_fields( $fields ) {
unset($fields['billing']['billing_postcode']['validate']);
unset($fields['shipping']['shipping_postcode']['validate']);
return $fields;
}
You can put this pretty much anywhere, but preferably in a custom plugin or in your theme's functions.php

You could try this:
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;
}

Related

Editing the woocommerce checkout page "order notes"

I want to change the woocommerce checkout page's 'order notes' text field to 'special notes'. But I couldn't find the exact location of this file. Where I can find this file in my localhost folder?
Here is the screenshot of the page:
In this situation direct don't try to edit in plugin files. instead of that try to search for hook.
Here is the code you can add it in function file. it will change text and place holder also
function md_custom_woocommerce_checkout_fields( $fields )
{
$fields['order']['order_comments']['placeholder'] = 'Special notes';
$fields['order']['order_comments']['label'] = 'Add your special note';
return $fields;
}
add_filter( 'woocommerce_checkout_fields', 'md_custom_woocommerce_checkout_fields' );
To achieve that you need to use the woocommerce_checkout_fields filter and set $fields['order']['order_comments']['label'] to the text you want.
Here is the code for it. You should add it to your theme's function.php file or plugin.
add_filter( 'woocommerce_checkout_fields', 'change_order_note_label' );
/**
* Change Order Notes Label - WooCommerce
*
*/
function change_order_note_label( $fields ) {
$fields['order']['order_comments']['label'] = 'Special notes';
return $fields;
}
It worked for me
add_filter( 'woocommerce_checkout_fields' , 'theme_override_checkout_notes_fields' );
// Our hooked in function - $fields is passed via the filter!
function theme_override_checkout_notes_fields( $fields ) {
$fields['order']['order_comments']['placeholder'] = 'Add some order notes or a gift message here.';
$fields['order']['order_comments']['label'] = 'Order notes or gift message';
return $fields;
}

Clear checkout fields in woocommerce

I'm trying to remove the autoloaded user info in the various checkout fields but cannot seem to find any way to access the fields value. I've tried the following which clears formatting, removes, the field, etc. but nothing I can find shows how to remove just the value. Does anyone know how to access this?
add_filter( 'woocommerce_checkout_fields' , 'custom_override_checkout_fields' );
function custom_override_checkout_fields( $fields ) {
$fields['billing']['billing_first_name']['placeholder'] = '';
$fields['billing']['billing_last_name']['value'] = '';
unset($fields['billing']['billing_company']);
return $fields;
}
Register filter:
add_filter( 'woocommerce_checkout_get_value' , 'clear_checkout_fields' );
Add this function. All fields will be empty.
function clear_checkout_fields($input)
{
return '';
}

Adding a hook to "Place order" button in woocommerce

When the user gets to the checkout, there is a button , the "Place order" button at the bottom of the form. I have been trying to add a hook to this button in woocommerce, but I don't seem to find the correct one, I have tried woocommerce_checkout_place_order... but it doesnt do anything.
function my_function() {
//write function
}
add_action( "woocommerce_order_status_pending", "my_function");
Thanks in advance!
You need this hook woocommerce_review_order_after_submit. It will execute any function you hook to it just after the submit area. with this hook you can add some html on the checkout page after the submit button.
But if you need to call a function after the user have pressed the "Place order" button - use woocommerce_checkout_order_processed. This one will hook you just after the order was created so you can use the freshly generated order details:
add_action( 'woocommerce_checkout_order_processed', 'is_express_delivery', 1, 1 );
function is_express_delivery( $order_id ){
$order = new WC_Order( $order_id );
//something else
}
You may check this site for some more hooks you might use on the checkout page.
## I USED THIS CODE FOR ADDING DELIVERY CHARGES DEPENDING UPON THE CART SUBTOTAL AND SOME POST FIELDS ##
function action_woocommerce_checkout_process($wccs_custom_checkout_field_pro_process )
{
global $woocommerce;
//Add Fuel Surcharge & CAF
function woo_add_cart_fee() {
global $woocommerce;
if ( WC()->cart->cart_contents_total < 1500 &&
$_POST['delivery_type']=='Pick Up') {
$fuel_surchargeandCAF = get_option( 'fuel_surchargeandCAF',
70 );
WC()->cart->add_fee( __('Delivery Charges', 'woocommerce'),
$fuel_surchargeandCAF, TRUE, '');
}
}
add_action( 'woocommerce_cart_calculate_fees', 'woo_add_cart_fee' );
};
add_action( 'woocommerce_checkout_process', 'action_woocommerce_checkout_process', 10,
1 );

how to disable cart functionality from woocommerce?

How to disable cart functionality from woocommerce store. I want user can only see products available.customer can not purchase from store.
the easiest way is make the products not purchasable..
add_filter( 'woocommerce_is_purchasable','__return_false',10,2);
To fully disable woocommerce purchase functionality:
add_filter( 'woocommerce_is_purchasable', '__return_false'); // DISABLING PURCHASE FUNCTIONALITY AND REMOVING ADD TO CART BUTTON FROM NORMAL PRODUCTS
remove_action('woocommerce_single_variation', 'woocommerce_single_variation', 10); // REMOVING PRICE FROM VARIATIONS
remove_action('woocommerce_single_variation', 'woocommerce_single_variation_add_to_cart_button', 20); // REMOVING ADD TO CART BUTTON FROM VARIATIONS
More option you can find here: https://react2wp.com/remove-hide-add-to-cart-button-in-woocommerce-while-disabling-keeping-purchase-functionality/
If you need conditions, you can use the following code:
function m3wc_woocommerce_is_purchasable( $is_purchasable, $product ) {
// Conditions here.
return $is_purchasable;
}
add_filter( 'woocommerce_is_purchasable', 'm3wc_woocommerce_is_purchasable', 10, 2 );
For example, if you need to check users:
// Disable purchase for non-logged-in users.
function m3wc_woocommerce_is_purchasable( $is_purchasable, $product ) {
if ( ! is_user_logged_in() ) {
return false;
}
return $is_purchasable;
}
add_filter( 'woocommerce_is_purchasable', 'm3wc_woocommerce_is_purchasable', 10, 2 );

WooCommerce - disable postcode validation

Does anybody know how to disable the Postcode validation on the checkout page in WooCommerce?
My country is set up as Switzerland, but I want also people from Austria and Germany allow to order.
So when I enter a German Postcode with 5 digits (in Switzerland there are only 4), the shop says it's an invalid postcode. (but in the settings I allowed every country).
Any idea how to fix that?
Adding this code to the functions.php file should work:
function custom_override_default_address_fields( $address_fields )
{
unset( $address_fields['postcode'] );
return $address_fields;
}
EDIT:
// Hook into the checkout fields (shipping & billing)
add_filter( 'woocommerce_checkout_fields' , 'custom_override_checkout_fields' );
// Hook into the default fields
add_filter( 'woocommerce_default_address_fields' , 'custom_override_default_address_fields' );
function custom_override_checkout_fields( $fields )
{
unset( $fields['billing']['billing_postcode'] );
unset( $fields['shipping']['shipping_postcode'] );
return $fields;
}
function custom_override_default_address_fields( $address_fields )
{
unset( $address_fields['postcode'] );
return $address_fields;
}
ANOTHER EDIT:
add_filter( 'woocommerce_default_address_fields' , 'custom_override_default_address_fields' );
function custom_override_default_address_fields( $address_fields )
{
$address_fields['postcode']['required'] = false;
return $address_fields;
}
So I didn't actually find a simple code solution for this one but I noticed that if I set
WooCommerce > Preferences > General > Geolocate address
it will work (if settings are set to "Sell to all countries", in my case)
This code only removes validation of address fields in my-account page, what you need:
add_filter( 'woocommerce_default_address_fields',
'custom_override_default_address_fields' );
function custom_override_default_address_fields($address_fields)
{
$address_fields['postcode']['validate'] = false;
return $address_fields;
}
for billing and shipping:
add_filter( 'woocommerce_checkout_fields' , 'remove_postcode_validation', 99 );
function remove_postcode_validation( $fields ) {
unset($fields['billing']['billing_postcode']['validate']);
unset($fields['shipping']['shipping_postcode']['validate']);
return $fields;
}
Also i think with removing "validate-required" class in wc-template-function.php, this feature will be deactivated (no test).
Sorry for bad English and hope this solutions solve your problem.
The previous answers don't seem to address the question! The postcode is still a required field, it's matter of whether other postcodes can be allowed that WooCommerce is saying is wrong.
For a checkout I'm building I've used this filter to allow any postcode to be considered valid regardless of country/postcode given.
add_filter( 'woocommerce_validate_postcode' , 'mycode_override_postcode_check', 99, 3 );
function mycode_override_postcode_check( $valid, $postcode, $country ) {
return true;
}
You can change that return true; for more complex code logic that reviews the postcode and country, this can help if your version of WooCommerce doesn't cover new postcode/zip code rules and you need them to be accepted.
Source: https://github.com/EloxZ/wordpresscrm/blob/main/wp-content/plugins/woocommerce/includes/class-wc-validation.php#L123

Resources