I have been using this custom function below in the previous versions of WooCommerce in order to pre-fill the City and ZIP code fields:
add_filter( 'woocommerce_checkout_fields' , 'custom_override_checkout_fields' );
function custom_override_checkout_fields( $fields ) {
$fields['billing']['billing_city']['default'] = 'Beverly Hills';
$fields['billing']['billing_postcode']['default'] = '90210';
return $fields;
}
It has been working great until the new WC updates.
The city still works, but the default ZIP code field doesn't seem to work anymore. It doesn't automatically pre-polulate the value.
Anything changed? Is there any other workaround for this?
Thanks
Setting a value for 'post-code' fields doesn't work anymore as there is an autocomplete feature. Even when disable "autocomplete", this doesn't work. So the workaround is to use jQuery in this case.
So your code is going to be:
add_filter( 'woocommerce_checkout_fields' , 'custom_override_checkout_fields', 10, 1 );
function custom_override_checkout_fields( $fields ) {
$fields['billing']['billing_city']['default'] = 'Beverly Hills';
$fields['billing']['billing_postcode']['autocomplete'] = "off"; // Removing autocomplete
return $fields;
}
add_action( 'woocommerce_after_checkout_form' , 'my_custom_checkout_field_postcode' );
function my_custom_checkout_field_postcode( ) {
?>
<script>
(function($){
$('#billing_postcode').val('90210');
})(jQuery);
</script>
<?php
}
This will set correctly your the desired value in "post-code" checkout billing field.
The Code goes in function.php file of your active child theme (or theme) or also in any plugin file.
Not necessary have to use java-script when you use the woocommerce callback made for the job: 'woocommerce_checkout_get_value'.
The 'woocommerce_after_checkout_form' call is made for setting the attributes of the check out fields, such as disabling the autocomplete.
The point to note is this function will be repeatedly called for each and every field within the check out form. So you switch on the field and return the value you wish assigned to the check-out form:
Based on your above code. here ya go...
function populating_checkout_fields ($fields, $input)
{
global $woocommerce;
switch($input)
{
case 'billing_city':
$FieldValue = 'Beverly Hills';
return $FieldValue;
break;
}
return $fields; // return the default value
}
add_filter( 'woocommerce_checkout_get_value', 'populating_checkout_fields', 10, 2 );
function ModifyAutoComplete($fields)
{
$fields['billing']['billing_postcode']['autocomplete'] = null;
}
add_filter( 'woocommerce_checkout_fields' , 'ModifyAutoComplete', 10, 1 );
Related
I am able to verify by viewing the "Application" tab in the dev tools that my session variable has carried over to the checkout page; and I have the WC hook setup that allows me to change the "default"/value of the field I want to insert data into.
However, I am unable to get them to read each other.
add_filter( 'woocommerce_checkout_fields' , 'custom_override_checkout_fields' );
function custom_override_checkout_fields( $fields ) {
$fields['order']['order_comments']['label'] = "Custom Neon Details (If included)";
$fields['order']['order_comments']['default'] = $_SESSION["customtext"];
var_dump( $fields );
return $fields;
}
As you can see from the attached image, my session variable is present on that page. How now, do I get it to add that session variable to $fields['order']['order_comments']['default']?
Any help is appreciated! Thanks.
Use session_start(); before accessing the $_SESSION variable:
add_filter( 'woocommerce_checkout_fields' , 'custom_override_checkout_fields' );
function custom_override_checkout_fields( $fields ) {
session_start();
$fields['order']['order_comments']['label'] = "Custom Neon Details (If included)";
$fields['order']['order_comments']['default'] = $_SESSION["customtext"];
var_dump( $fields );
return $fields;
}
Alternatively you can use jQuery with sessionStorage:
// set the value of the order comment field via session storage
add_action( 'wp_footer', 'set_value_order_comments_field_via_session_storage' );
function set_value_order_comments_field_via_session_storage() {
?>
<script type="text/javascript">
jQuery(function($){
var customtext = sessionStorage.getItem("customtext");
if ( customtext.length ) {
$('#order_comments').val(customtext);
}
});
</script>
<?php
}
The code has been tested and works. Add it to your active theme's functions.php.
I am trying to clear the billing_po_no field value from my WooCommerce checkout billing Form, by adding the following code into my functions.php file:
add_filter( 'woocommerce_checkout_fields' , 'custom_override_checkout_fields' );
function custom_override_checkout_fields( $fields ) {
$fields['billing']['billing_po_no'] = '';
return $fields;
}
But it does not appear to be working. Can someone point me in the right direction?
Try the following instead:
add_filter( 'woocommerce_checkout_get_value' , 'clear_specific_checkout_field' , 10, 2 );
function clear_specific_checkout_field( $value, $input ){
if( $input === 'billing_po_no' )
$value = '';
return $value;
}
Code goes in functions.php file of the active child theme (or active theme). It should works.
I'm trying to delete or increase the default maxlength (nowadays set to 4) on a WordPress site I developed using Woocommerce.
Up to now I've tried all these functions on my childtheme's functions.php
#OPTION 1
function wpse215677_checkout_fields ( $fields ) {
$fields['postcode']['maxlength'] = 5;
return $fields;
}
add_filter('woocommerce_default_address_fields', 'wpse215677_checkout_fields');
#OPTION 2 - WITH AND WITHOUT ['custom_attributes']
add_filter( 'woocommerce_checkout_fields' , 'my_override_checkout_fields');
function my_override_checkout_fields( $fields ) {
$fields['billing']['postcode']['custom_attributes']['maxlength'] = 5;
$fields['billing']['billing_postcode']['custom_attributes']['maxlength'] = 5;
return $fields;
}
#OPTION 3 - WITH AND WITHOUT ['custom_attributes']
function my_wc_custom_billing_fields( $fields ) {
$fields['billing_postcode']['custom_attributes']['maxlength'] = 5;
return $fields;
}
add_filter( 'woocommerce_billing_fields', 'my_wc_custom_billing_fields' );
function my_wc_custom_shipping_fields( $fields ) {
$fields['shipping_postcode']['custom_attributes']['maxlength'] = 5;
return $fields;
}
add_filter( 'woocommerce_shipping_fields', 'my_wc_custom_shipping_fields' );
All of them worked fine on the cart calculator (now I can write any number over 4) but when I go to the checkout page and try to write a number over 4 characters on the postcode (shipping or billing), the input still remains with a maxlength of 4 (I've inspected it with Chrome's tools).
Is there any way I can overwrite the entire input on the checkout to allow me to write more than 4 characters on this input?
Or am i doing something wrong with these functions and that is why they are not working on the checkout page?
One way is to do it via jQuery, although the code you have already posted here works without any problems
function action_wp_footer() {
// Returns true on the checkout page, when false, return
if ( ! is_checkout() ) return;
?>
<script>
jQuery(document).ready(function($){
$( '#billing_postcode' ).attr( 'maxlength', '5' );
$( '#shipping_postcode' ).attr( 'maxlength', '5' );
});
</script>
<?php
}
add_action( 'wp_footer', 'action_wp_footer' );
Since Woo 3.5.1 the billing state field is mandatory for certain countries and I'm trying to figure out how to not make it mandatory.
This is the code I'm using, which is not working. It seems to be a core change and I don't know if there's a new hook for it?
Code:
add_filter( 'woocommerce_billing_fields', 'remove_mandatory_state_billing', 10, 1 );
function remove_mandatory_state_billing( $address_fields ) {
$address_fields['billing_state']['required'] = false;
return $address_fields;
}
add_filter( 'woocommerce_shipping_fields', 'remove_mandatory_state_shipping', 10, 1 );
function remove_mandatory_state_shipping( $address_fields ) {
$address_fields['shipping_state']['required'] = false;
return $address_fields;
}
Any help is very much appreciated.
The following code will make the state field optional for all countries in Woocommerce:
add_filter( 'woocommerce_get_country_locale', 'custom_country_locale_state_optional', 10, 1 );
function custom_country_locale_state_optional( $locale ) {
foreach( $locale as $country_code => $state_field ) {
if( isset($locale[$country_code]['state']) ) {
$locale[$country_code]['state']['required'] = false;
}
}
return $locale;
}
Code goes in function.php file of your active child theme (active theme). Tested and works.
Related: Make state checkout field required for a specific country in Woocommerce
So this problem may have a easy solution, but I'm stuck for the moment. After the last update (Woocommerce 3.3.5) I have a problem with the state field on the checkout page, because it is not mandatory and people just skip it. I really need this thing to be mandatory, because I have connected my website to the delivery company server through and API to send the order info directly to them.
I tried adding this to my functions.php and the thing is that when I go to the checkout page, the field has an asterisk, but for like one second.
add_filter( 'woocommerce_billing_fields', 'woo_filter_state_billing',
10, 1 );
add_filter( 'woocommerce_shipping_fields',
'woo_filter_state_shipping', 10, 1 );
function woo_filter_state_billing( $address_fields ) {
$address_fields['billing_state']['required'] = true;
return $address_fields;
}
function woo_filter_state_shipping( $address_fields ) {
$address_fields['shipping_state']['required'] = true;
return $address_fields;
}
Any help is appreciated. Thanks!
By default in Last woocommerce version 3.3.5, state field is required… So in your case something is making that field "not" required.
You can try this (work for billing and shipping fields at the same time):
add_filter( 'woocommerce_default_address_fields' , 'make_state_field_required', 90, 1 );
function make_state_field_required( $address_fields ) {
$address_fields['state']['required'] = true;
return $address_fields;
}
Code goes in function.php file of your active child theme (or active theme). It could works.
This worked better for me as of January 2019
add_filter( 'woocommerce_billing_fields', 'woo_filter_state_billing', 10, 1 );
add_filter( 'woocommerce_shipping_fields', 'woo_filter_state_shipping', 10, 1 );
function woo_filter_state_billing( $address_fields ) {
$address_fields['billing_state']['required'] = true;
return $address_fields;
}
function woo_filter_state_shipping( $address_fields ) {
$address_fields['shipping_state']['required'] = true;
return $address_fields;
}
Maybe you mean "country"? I had that problem, being that field inexplicably optional.
In that case, the code would be:
add_filter( 'woocommerce_billing_fields' , 'make_country_field_required', 90, 1 );
function make_country_field_required( $address_fields ) {
$address_fields['billing_country']['required'] = true;
return $address_fields;
}