Customize woocommerce order tracking page - woocommerce

in the default order tracking page for wooocommerce there are two fields { ordreid and email }
.
I want to make email field unrequired
I tried with this code in function.php file but it wasn't successful :(
add_filter( 'woocommerce-order-tracking', 'ts_unrequire_wc_email_field');
function ts_unrequire_wc_email_field( $fields ) {
$fields['order_email']['required'] = false;
return $fields;
}
any help will be useful .. and i hope that you are all safe during the breakout of covid-19 <3**

Related

Woocommerce remove email field validation

I am using Woocommerce register form.
My requirement for the email field is optional therefore may I ask is there any hook I can use to remove the built in email field validation?
I have another hook to do the validation for that.
List of available hooks
https://woocommerce.github.io/code-reference/hooks/hooks.html
I've used https://www.businessbloomer.com/woocommerce-checkout-customization/ as a reference in order to customize a checkout field.
add_filter( 'woocommerce_checkout_fields', 'bbloomer_required_woo_checkout_fields' );
function bbloomer_required_woo_checkout_fields( $fields ) {
$fields['billing']['billing_email']['required'] = false;
return $fields;
}

Woocommerce - Remove fields from woocommerce_form_field including validation

I am trying to customise the Woocommerce myaccount page, in particular the edit address page.
I want to display both the shipping + billing address forms on a single page. Ideally, in a single form with a one save button. I also need to remove a lot of the fields, so that it's a much simpler form of just an address (no name, company, etc).
I have implemented the code found on This Answer. It works nicely in that it shows both forms. However, I cannot remove the fields from the forms. If I try code like this:
add_filter( 'woocommerce_billing_fields' , 'custom_override_billing_fields' );
add_filter( 'woocommerce_shipping_fields' , 'custom_override_shipping_fields' );
function custom_override_billing_fields( $fields ) {
unset($fields['billing_country']);
unset($fields['billing_company']);
unset($fields['billing_first_name']);
unset($fields['billing_last_name']);
unset($fields['billing_phone']);
unset($fields['billing_email']);
return $fields;
}
function custom_override_shipping_fields( $fields ) {
unset($fields['shipping_country']);
unset($fields['shipping_company']);
unset($fields['shipping_first_name']);
unset($fields['shipping_last_name']);
return $fields;
}
It doesn't work, the fields are no longer shown but the form does not save on click... it just redirects to /my-account/edit-address/billing/ - and doesn't save. (the same form shown on this page doesn't save either).
I've also tried:
foreach ( $billing_fields as $key => $field ) :
if($key != 'billing_first_name' && $key != 'billing_last_name') :
woocommerce_form_field( $key, $field, $userMeta[$key][0] );
endif;
endforeach;
This removes the field from displaying, BUT the validation still exists - and any filter code I add to functions using
woocommerce_checkout_fields to remove the validation doesn't seem to affect this form at all.
Is there a way to either:
Remove fields from this form generated by woocommerce_form_field including the validation?
Create a custom form that allows me to set the input fields manually in the code, and update any fields that are there, ignoring the validation from Woocommerce completely?
This should work 100%. You need to state whether the fields you are removing is from billing or shipping and this is done by adding the ['billing'] or ['shipping'], whichever it is.
After this, adding the function directly to woocommerce_checkout_fields will apply both for billing and shipping.
For phone and company fields you can disable it in admin panel itself, do it.
Edit: And yes, all validation that was involved with the fields in the past will be removed. You can then apply any validation you need.
add_filter( 'woocommerce_checkout_fields' , 'brandimagemarketer_remove_billing_fields_checkout' );
function brandimagemarketer_remove_billing_fields_checkout( $fields ) {
unset($fields['billing']['billing_country']);
unset($fields['billing']['billing_first_name']);
unset($fields['billing']['billing_last_name']);
unset($fields['billing']['billing_email']);
unset($fields['shipping']['shipping_country']);
unset($fields['shipping']['shipping_first_name']);
unset($fields['shipping']['shipping_last_name']);
unset($fields['shipping']['shipping_email']);
return $fields;
}

How to add verified badge in front of author name across wordpress blog

Good Day,
I have been trying to add verification badge to WordPress users but I no success. I tried using administrator to do the testing by checking if user has role administrator and trying to enter code here update_user_meta (display_name) but I wasn't able to add icons to the display name.
I have as well tried creating a custom field in user profile called verify_user (text field) .... In which I entered the value "verified" and saved ... I have been searching for hooks to use but haven't seen one. I'm not sure which hook/filter to use here
Please is there any solution to adding this verification icon to author's display name in which when the word "verified" is entered into the custom field created in user profile or any other approach available (e.g if user has specific role). I don't mind if the little structure I wrote above would be changed.
Thanks
I was able to get a solution which worked exactly as i wanted. For anyone who might have same task to tackle;
function add_verification_bagdge_to_authors($display_name) {
global $authordata;
if (!is_object($authordata))
return $display_name;
$icon_roles = array(
'administrator',
'verified_author',
);
$found_role = false;
foreach ($authordata->roles as $role) {
if (in_array(strtolower($role), $icon_roles)) {
$found_role = true;
break;
}
}
if (!$found_role)
return $display_name;
$result = sprintf('%s <i title="%s" class="fa fa-check-circle"></i>',
$display_name,
__('This is a Verified Author', 'text-domain-here')
);
return $result;
}
add_filter( 'the_author', 'add_verification_bagdge_to_authors' );
The way i was able to tackle this was to create a user role called Verified Author (using add_role() function from Wordpress Codex ), which will be the role assigned to verified author across the website. All Users with Admin Role are automatically Verified while user role has to be switched from either contributor/Author role to Verified Author.
The above code was able to do 98% of the task but when a verified author / administrator comments, their display name doesn't show the verified badge. I was able to use the below code to fix that (combining the code with a shortcode).
function my_comment_author( $author = '' ) {
// Get the comment ID from WP_Query
$comment = get_comment( $comment_ID );
if ( ! empty($comment->comment_author) ) {
if (!empty($comment->user_id)){
$user=get_userdata($comment->user_id);
$author=$user->display_name.' '. do_shortcode('[this_is_verified-sc]'); // This is where i used the shortcode
} else {
$author = $comment->comment_author;
}
} else {
$author = $comment->comment_author;
}
return $author;
}
add_filter('get_comment_author', 'my_comment_author', 10, 1);
Shortcode to return the Verified Badge if user is admin or verified author
function add_verification_bagdge_to_authors_sc($display_name) {
global $authordata;
if (!is_object($authordata))
return $display_name;
$icon_roles = array(
'administrator',
'verified_author',
);
$found_role = false;
foreach ($authordata->roles as $role) {
if (in_array(strtolower($role), $icon_roles)) {
$found_role = true;
break;
}
}
if (!$found_role)
return $display_name;
$result = sprintf('%s <i title="%s" class="fa fa-check-circle"></i>', $display_name, __('This is a Verified Author', 'text-domain-here') );
return $result;
}
add_shortcode( 'this_is_verified-sc', 'add_verification_bagdge_to_authors_sc' );
Please Note: Not all Magazine theme are properly coded / have a hard coded Block and module elements, so the verified badge might not work on their block / module element. I experienced this during the whole website design process, so we had to change up theme and the only modification i had to make to the new theme was to remove the html escape added to their block/module code, so that the icon can be rendered. i.e. in their module/block code they had something like this esc_html( the_author() ) and i removed the esc_html to have just the_author() an it all worked.
Its not really a straight forward solution but that is how i was able to tackle the task without using a plugin. Just add the codes to your functions.php file and that's it. I hope it helps someone.
Thanks to Kero for pointing me in the right direction, providing the code i was able to work with and manipulate

Set billing_address_2 label in Woocommerce checkout

I'm trying to set / show a label for the billing_address_2 field on the Woocommerce checkout page, but can't find a way to do this. Does anyone know a solution?
The code below (which works fine on other fields) does not do the job.
add_filter( 'woocommerce_checkout_fields' , 'custom_rename_wc_checkout_fields' );
function custom_rename_wc_checkout_fields( $fields ) {
$fields['billing']['billing_address_2']['label'] = 'Building number';
return $fields;
}
Updating this question with an answer for the sake of future readers.
As of WooCommerce 3.5.1, commit 87054ece9a4c05db72e139730ed1764c63fab635 adds the 'screen-reader-text' label_class to both Billing & Shipping Address 2 fields for accessibility purposes as per Issue #21182. This class will keep the label hidden unless it is also changed (e.g. make it blank, like the Address 1 field).
Here's how I updated my functions.php to get the billing/shipping_address_2 labels back (I'm using the separate billing/shipping filters as I have made changes to other fields I didn't include in the code for brevity).
// Billing Fields.
add_filter( 'woocommerce_billing_fields', 'custom_woocommerce_billing_fields' );
function custom_woocommerce_billing_fields( $fields ) {
$fields['billing_address_2']['label'] = 'Address 2';
$fields['billing_address_2']['label_class'] = '';
return $fields;
}
// Shipping Fields.
add_filter( 'woocommerce_shipping_fields', 'custom_woocommerce_shipping_fields' );
function custom_woocommerce_shipping_fields( $fields ) {
$fields['shipping_address_2']['label'] = 'Address 2';
$fields['shipping_address_2']['label_class'] = '';
return $fields;
}

Woocommerce I am looking for billing address and order-received email hooks

I have to make two customization on my woocommerce site.
I need to know two main hooks. Someone out there please help me out!
Show custom field values to billing address on order-received page.(I added custom fields on checkout page.)
Need to include these values to customers' order-received email, too.
Thanks for stopping by.
To show custom field in order-received page you have to use
woocommerce_thankyou hook.
Here is the code:
// define the woocommerce_thankyou callback
function action_woocommerce_thankyou($order_id)
{
$my_custom_field = get_post_meta($order_id, '_billing_my_field', TRUE);
}
// add the action
add_action('woocommerce_thankyou', 'action_woocommerce_thankyou', 10, 1);
UPDARED This section after post author comment
To add custom billing fields in WooCommerce email you have to use
woocommerce_email_customer_details hook; this will be displayed just
before the customer details.
Here is the code:
add_filter('woocommerce_email_customer_details', 'custom_woocommerce_email_order_meta_fields', 10, 3);
function custom_woocommerce_email_order_meta_fields($order, $sent_to_admin, $plain_text)
{
$_billing_my_field = get_post_meta($order->id, '_billing_my_field', true);
if ($plain_text)
{
echo 'My field is ' . $_billing_my_field;
}
else
{
echo '<p>My field is ' . $_billing_my_field . '</p>';
}
}
All code goes in function.php file of your active child theme (or theme) or also in any plugin file.
Please Note:
I have assuming that you have added a custom billing fields as $fields['billing']['billing_my_field'] using woocommerce_checkout_fields hook.
All the codes are tested and fully functional.

Resources