How to clear specific billing field value from WooCommerce checkout - 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.

Related

Set a default value for shipping city and state in Woocommerce My account

In woocommerce already the shipping country in restricted to IN
On checkout the default value of Country, state & city is set
I want to do that same in my-account/edit-address/ only in shipping address
tried using this code but its not working
add_filter( 'woocommerce_shipping_fields' , 'default_shipping_address' );
function default_shipping_address( $fields ) {
$fields['shipping_city']['default'] = 'Mumbai';
$fields['shipping_state']['default'] = 'Maharashtra';
return $fields;
}
this is not working let me know what's wrong or is there any other way to do this?
You are pretty near… As Shipping State value in WooCommerce is a state code, you need to replace 'Maharashtra' with 'MH' (For India) as follows:
add_filter( 'woocommerce_shipping_fields' , 'set_default_my_account_shipping_city_state_values' );
function set_default_my_account_shipping_city_state_values( $fields ) {
// Targeting My account section
if( is_account_page() ) {
$fields['shipping_city']['default'] = 'Mumbai';
$fields['shipping_state']['default'] = 'MH'; // <== HERE
}
return $fields;
}
Code goes in functions.php file of the active child theme (or active theme). Tested and works.
Alternative: If it really doesn't work for you, you can try to use the following:
add_action( 'template_redirect' , 'set_default_shipping_city_and_state_values' );
function set_default_shipping_city_and_state_values( $fields ) {
if( is_wc_endpoint_url( 'edit-address' ) ) {
if ( WC()->customer->get_shipping_city() === '' ) {
WC()->customer->Set_shipping_city('Mumbai');
}
if ( WC()->customer->get_shipping_state() === '' ) {
WC()->customer->Set_shipping_state('MH');
}
}
}
You can use the address hook.
add_filter( 'woocommerce_default_address_fields' , 'default_shipping_address' );
function default_shipping_address( $fields ) {
$fields['shipping_city']['value'] = 'Mumbai';
$fields['shipping_state']['value'] = 'Maharashtra';
return $fields;
}

Display custom text on WooCommerce Order Received page if specific coupons are used

I'm attempting to display a custom thank you message on the Woocommerce order received page if one of three specific coupon codes are used during checkout.
Our Woocommerce version is 2.6.11.
I've tried a few variations of the below code but cannot get it working, am I doing something incorrectly?
//show custom coupon thankyou
function coupon_thankyou($order_id) {
$coupon_id = '1635';
$order = wc_get_order($order_id);
foreach( $order->get_items('coupon') as $coupon_item ){
if( $coupon_item->get_code() = $coupon_id ){
echo '<p>This is an custom thank you.</p>';
}
}
}
add_action('woocommerce_thankyou','coupon_thankyou');
There is a mistake in your IF statement condition where = has to be replaced with == or ===. Also with coupons, you need to use the coupon code slug (but not the post ID).
To display a message on Order received page better use woocommerce_thankyou_order_received_text filter hook, this way (for Woocommerce 3+):
// On "Order received" page (add a message)
add_filter( 'woocommerce_thankyou_order_received_text', 'thankyou_applied_coupon_message', 10, 2 );
function thankyou_applied_coupon_message( $text, $order ) {
$coupon_code = '1635'; // coupon code name
foreach( $order->get_items('coupon') as $coupon ){
if( $coupon->get_code() === $coupon_code ){
$text .= '<p>'.__("This is an custom thank you.").'</p>';
}
}
return $text;
}
Code goes in function.php file of your active child theme (or active theme). It should works now.
Updated
For the versions of Woocommerce before 3.0, you should use use the following instead:
// On "Order received" page (add a message)
add_action( 'woocommerce_thankyou', 'thankyou_applied_coupon_message', 10, 1 );
function thankyou_applied_coupon_message( $order_id ) {
$coupon_code = '1635'; // coupon code name
$order = wc_get_order( $order_id );
foreach( $order->get_items('coupon') as $coupon ){
if( $coupon['name'] === $coupon_code ){
echo '<p>'.__("This is an custom thank you.").'</p>';
}
}
}
Code goes in function.php file of your active child theme (or active theme). Tested and works.

Make state checkout field optional in Woocommerce

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

Make Woocommerce checkout state field required

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;
}

Pre-filing checkout post code with a custom value

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 );

Categories