0

I have a woocommerce checkout page, where customers are writing whatever way they like, all lowercase all uppercase etc.

How can I do the checkout fields of Name, Surname, Address to be in capital letters?

Thank you

2
  • Do you want to display them as capital letters on the checkout page or get saved to database as capital letters?
    – pawelkmpt
    Commented Nov 18, 2019 at 16:07
  • I guess both would be a good idea thank you Commented Nov 18, 2019 at 16:39

1 Answer 1

2

If you want the values to display uppercase (or capitalized), then adding CSS to your theme would do the trick.

.your-input-class{
  text-transform: uppercase; /* or you can use 'capitalize' */
}

If you are concerned with how the values are being saved in the database, you would need to add custom code to your website that hooks into the action that saves the checkout fields, which is woocommerce_checkout_posted_data found here.

You can use something like the following to capitalize the users input data. Our example can be "jAnE" and "DOe" for the first and last name.

<?php

add_filter('woocommerce_checkout_posted_data', 'mg_custom_woocommerce_checkout_posted_data');
function mg_custom_woocommerce_checkout_posted_data($data){
  // The data posted by the user comes from the $data param. 
  // You would look for the fields being posted, 
  // like "billing_first_name" and "billing_last_name"

  if($data['billing_first_name']){
    /*
      From jAnE to JANE
    */
    $data['billing_first_name'] = strtoupper($data['billing_first_name']);
  }

  if($data['billing_last_name']){
    /*
      From DOe to DOE
    */
    $data['billing_last_name'] = strtoupper($data['billing_last_name']);
  }

  return $data;
}

The code above has not been tested - please do not use on a production site without testing first.

Note in the above example I am using the strtoupper PHP function, but you can swap that with ucfirst to capitalize the first letter instead of the whole string.

References:

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.