Woocommerce Override Email Label - php

I edited the Order Notes field in the checkout to be:
// Hook in
add_filter( 'woocommerce_checkout_fields' , 'custom_override_checkout_fields' );
// Our hooked in function - $fields is passed via the filter!
function custom_override_checkout_fields( $fields ) {
$fields['order']['order_comments']['placeholder'] = 'Enter Purchase Order Number if applicable';
$fields['order']['order_comments']['label'] = 'Purchase Order #';
return $fields;
}
However, the confirmation and notification emails still say "Note" as the field label. I just want to change the label in the emails.

There are a whole different set of filters for the emails that are generated by woocommerce. Changing the label for the checkout field only changes the note in the checkout area! the label isn't passed through to the order emails, or to paypal for that matter.
You'll have to add another filter to override the field name in the email. Something like this:
/**
* Add the field to order emails
**/
add_filter('woocommerce_email_order_meta_keys', 'my_custom_checkout_field_order_meta_keys');
function my_custom_checkout_field_order_meta_keys( $keys ) {
$keys[] = 'My Field';
return $keys;
}

Related

How to clear WooCommerce billing email and billing phone checkout fields after place order?

How do I clear a specfic checkout fields in WooCommerce after placing an order?
For example Billing email and Billing Phone. So that when the registered customer makes his/her next order, he/she has to fill that fields again?
I see this code, but this clean all the fields, i need only a specific field. Any advice?
add_filter('woocommerce_checkout_get_value','__return_empty_string',10);
The woocommerce_checkout_get_value hook has 2 arguments:
$value argument that is returned
$input argument to target checkout field(s)
So in your case, you get:
function filter_woocommerce_checkout_get_value( $value, $input ) {
// Target checkout fields. Multiple fields can be added, separated by a comma
if ( in_array( $input, array( 'billing_phone', 'billing_email' ) ) ) {
$value = '';
}
return $value;
}
add_filter( 'woocommerce_checkout_get_value' , 'filter_woocommerce_checkout_get_value' , 10, 2 );
You could probably achieve this with some custom javascript:
document.getElementById('fieldID').value = ''
Or jQuery:
$('#fieldID').val('');

WooCommerce place an order without filling in email address

I am trying to place an order without filling in the email field at the checkout form.
I used this code to remove all field values (except country) :
add_filter( 'woocommerce_checkout_get_value' , 'clear_checkout_fields' , 10, 2 );
function clear_checkout_fields( $value, $input ){
if( $input != 'billing_country' )
$value = '';
return $value;
}
When placing the order without filling in the email field the order gets placed with the email of the account it is placed with. I want to be able to place an order without filling in an email address so it shows no email address on the orders page on the WooCommerce dashboard.
Any suggestions?
Set default value for this field to admin email.
add_filter( 'woocommerce_checkout_fields', 'woo_default_email' );
function woo_default_email($fields) {
$fields['billing']['billing_email']['default'] = get_option('admin_email');
return $fields;
}
Then just remove field from output in your template and email message.

Woocommerce set billing field value equal to other billing field value

I want the billing field 'company email' to be equal to Billing E-mail address.
I thought this would work but I only get an empty field:
add_filter( 'woocommerce_admin_billing_fields', 'custom_admin_billing_fields', 10, 1 );
function custom_admin_billing_fields( $billing_fields ) {
$billing_fields['tlr_company_email']['value'] = $billing_fields['email']['value'];
return $billing_fields;
}
I think the proper way of doing it is to update once the payment is completed, after the checkout. The following code is hooked to the payment complete trigger. It will copy the billing email to the custom company billing email.
add_action('woocommerce_payment_complete', 'replicate_billing_email', 100, 1);
function replicate_billing_email($order_id){
$order = new WC_Order($order_id); //obatin the completed order as object
$billing_email = $order->get_billing_email(); //get the billing email address
update_post_meta($order_id, 'tlr_company_email', $billing_email); //update the company email
update_post_meta($order_id, '_tlr_company_email', $billing_email); //it maybe saved like that, with underscore, ommit if not needed
}
To test it, complete a test order.

Remove phone from billing/shipping fields everywhere in Woocommerce?

I don't need to collect customer phone numbers in woocommerce so I've used the following code to remove them from the checkout process:
add_filter( 'woocommerce_checkout_fields', 'pbj_woocommerce_checkout_fields' );
function pbj_woocommerce_checkout_fields( $fields ) {
unset($fields['billing']['billing_phone']);
unset($fields['shipping']['shipping_phone']);
$fields['billing']['billing_email']['class'] = array('form-row-wide');
return $fields;}
(sorry for hideous code pasting, first time!)
That works great on the checkout page but once in the "my account" area, the user is still required to add a phone number if they edit their billing or shipping address. I have added this code:
function pbj_remove_billingphone($fields) {
unset( $fields ['billing_phone'] );
return $fields;}
add_filter( 'woocommerce_billing_fields', 'pbj_remove_billingphone' );
which has removed it from billing, but I can't get anything to work to remove it from shipping. Any help on either something that universally removes the phone number requirement everywhere, or tips on what to filter/unset to remove the shipping phone number on the account page? Thank you!
Here is the complete way to do it in account and checkout pages (for both pages):
// Remove billing phone (and set email field class to wide)
add_filter( 'woocommerce_billing_fields', 'remove_billing_phone_field', 20, 1 );
function remove_billing_phone_field($fields) {
$fields ['billing_phone']['required'] = false; // To be sure "NOT required"
$fields['billing_email']['class'] = array('form-row-wide'); // Make the field wide
unset( $fields ['billing_phone'] ); // Remove billing phone field
return $fields;
}
// Remove shipping phone (optional)
add_filter( 'woocommerce_shipping_fields', 'remove_shipping_phone_field', 20, 1 );
function remove_shipping_phone_field($fields) {
$fields ['shipping_phone']['required'] = false; // To be sure "NOT required"
unset( $fields ['shipping_phone'] ); // Remove shipping phone field
return $fields;
}
Code goes in function.php file of your active child theme (or active theme). Tested and works
Normally shipping email and phone fields doesn't exist by default in WooCommmerce

Checkout fields - Make billing_address_2 above billing_address_1

In WooCommerce checkout fields, I'm trying to make billing_address_2 above billing_address_1 on the checkout form.
So instead of it being:
Street Address
Apartment
I would like to have:
Apartment
Street Address
Please also note, I'm using the Theme called Avada.
How can I achieve this?
Thanks.
Update (related to your comment)…
Here you have an addition that removes "Address" text label from Address1 field and set it to Address2 field, making also that field (optionally) required and changing a little bit the place holder… I have also another solution (see below after the code).
Here is the code:
add_filter( 'woocommerce_checkout_fields', 'custom_billing_fields_order' );
function custom_billing_fields_order( $fields ) {
// 1. Customizing address_1 and address_2 fields
$fields['billing']['billing_address_1']['label'] = ''; // Removing the label from Adress1
$fields['billing']['billing_address_2']['label'] = __('Address', 'theme_domain');
$fields['billing']['billing_address_2']['required'] = true; // Making Address 2 field required
$fields['billing']['billing_address_2']['placeholder'] = __('Apartment, suite, unit etc...', 'woocommerce');
// 2. Custom ordering the billing fields array (toggling address_1 with address_2)
$custom_fields_order = array(
'billing_first_name', 'billing_last_name',
'billing_company',
'billing_email', 'billing_phone',
'billing_country',
'billing_address_2', 'billing_address_1', ## <== HERE, changed order
'billing_postcode', 'billing_city'
);
foreach($custom_fields_order as $field)
$new_ordered_fields[$field] = $fields['billing'][$field];
// Replacing original fields order by the custom one
$fields['billing'] = $new_ordered_fields;
// Returning Checkout customized billing fields order
return $fields;
}
Instead of toggling that 2 fields, you could invert the fields placeholders and add make (optionally) required the field Address2, so you will not need to reorder the fields. You can do it this way:
add_filter( 'woocommerce_checkout_fields', 'custom_billing_fields_placeholders' );
function custom_billing_fields_placeholders( $fields ) {
// 1. Customizing address_1 and address_2 fields
$fields['billing']['billing_address_1']['placeholder'] = __('Apartment, suite, unit etc...', 'woocommerce');
$fields['billing']['billing_address_2']['required'] = true; // Making Address 2 field required
$fields['billing']['billing_address_2']['placeholder'] = __('Street address', 'woocommerce');
// Returning Checkout customized billing fields
return $fields;
}
Code goes in function.php file of your active child theme (or theme) or also in any plugin file.
Code is tested and works.
Related answers: Checkout fields: Hiding and showing existing fields
Official documentation: Customizing checkout fields using actions and filters

Categories