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' );
Related
I want to show postcode/zip field even for the countries that do not use postcodes/zip on WooCommerce checkout page.
WooCommerce hides postcode/zip field by default for countries that don't use them.
I have used following filter in theme functions.php but it doesn't work.
add_filter( 'woocommerce_default_address_fields' , 'custom_override_default_address_fields' );
function custom_override_default_address_fields( $address_fields ) {
$address_fields['postcode']['hidden'] = false;
return $address_fields;
}
How can I override this behavior?
You can use the woocommerce_get_country_locale filter hook, to unhide this by default for all countries.
So you get:
function filter_woocommerce_get_country_locale( $country_locale ) {
// Loop through
foreach( $country_locale as $key => $locale ) {
// Isset
if ( isset ( $locale['postcode']['hidden'] ) ) {
// When true
if ( $locale['postcode']['hidden'] == true ) {
// Set to false
$country_locale[$key]['postcode']['hidden'] = false;
}
}
}
return $country_locale;
}
add_filter( 'woocommerce_get_country_locale', 'filter_woocommerce_get_country_locale', 10, 1 );
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.
In WooCommerce, I have set different shipping methods, but one in particular must be exclusive for companies.
For this I am using the following code:
add_filter( 'woocommerce_package_rates', array( $this, 'package_rates' ), 10, 2 );
public function package_rates( $rates, $package ) {
$company_rate_id = 'flat_rate:7';
if(!empty(WC()->customer->get_billing_company())){
$company_rates = $rates[ $company_rate_id ];
$rates = array( $company_rate_id => $company_rates );
}else{
unset( $rates[ $company_rate_id ] );
}
return $rates;
}
The solution works, but only if the billing company already exist and is saved in the database. So if a customer updates this information on the checkout page, it doesn't work.
A possible solution would be to save this field live (billing_company).
I tried the following function:
add_filter( 'woocommerce_checkout_fields' , 'trigger_update_checkout_on_change' );
function trigger_update_checkout_on_change( $fields ) {
$fields['billing']['billing_company']['class'][] = 'update_totals_on_change';
return $fields;
}
This updates the shipping method, the problem is that again, the field is not saved in the database and the package_rates() function can not find it live.
This is a bit more complicated than that… jQuery and Ajax code are required to allow showing/hiding shipping methods based on a checkout field user input.
The following code will enable show/hide pre defined shipping methods based on checkout billing company field:
// Conditionally show/hide shipping methods
add_filter( 'woocommerce_package_rates', 'shipping_package_rates_filter_callback', 100, 2 );
function shipping_package_rates_filter_callback( $rates, $package ) {
// The defined rate id
$company_rate_id = 'flat_rate:7';
if( WC()->session->get('company' ) === '1' ) {
$rates = array( $company_rate_id => $rates[ $company_rate_id ] );
} else {
unset( $rates[ $company_rate_id ] );
}
return $rates;
}
// function that gets the Ajax data
add_action( 'wp_ajax_get_customer_company', 'wc_get_customer_company' );
add_action( 'wp_ajax_nopriv_get_customer_company', 'wc_get_customer_company' );
function wc_get_customer_company() {
if ( isset($_POST['company']) && ! empty($_POST['company']) ){
WC()->session->set('company', '1' );
} else {
WC()->session->set('company', '0' );
}
die(); // (required)
}
// The Jquery Ajax script
add_action( 'wp_footer', 'custom_checkout_script' );
function custom_checkout_script() {
if( WC()->session->__isset('company') )
WC()->session->__unset('company');
// Only on checkout when billing company is not defined
if( is_checkout() && ! is_wc_endpoint_url() ):
?>
<script type="text/javascript">
jQuery( function($){
if (typeof wc_checkout_params === 'undefined')
return false;
var fieldId = 'input#billing_company';
function companyTriggerAjax( company ){
$.ajax({
type: 'POST',
url: wc_checkout_params.ajax_url,
data: {
'action': 'get_customer_company',
'company': company,
},
success: function (result) {
// Trigger refresh checkout
$('body').trigger('update_checkout');
}
});
}
// On start
if( $(fieldId).val() != '' ) {
companyTriggerAjax( $(fieldId).val() );
}
// On change
$(fieldId).change( function () {
companyTriggerAjax( $(this).val() );
});
});
</script>
<?php
endif;
}
// Enabling, disabling and refreshing session shipping methods data
add_action( 'woocommerce_checkout_update_order_review', 'refresh_shipping_methods', 10, 1 );
function refresh_shipping_methods( $post_data ){
$bool = true;
if ( WC()->session->get('company' ) === '1' )
$bool = false;
// Mandatory to make it work with shipping methods
foreach ( WC()->cart->get_shipping_packages() as $package_key => $package ){
WC()->session->set( 'shipping_for_package_' . $package_key, $bool );
}
WC()->cart->calculate_shipping();
}
Code goes in function.php file of your active child theme (or active theme). Tested and works.
Related: Remove shipping cost if custom checkbox is checked in WooCommerce Checkout
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;
}
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 );