Copy billing to shipping address in WooCommerce

copy billing to shipping address woocommerce

There is no function in WordPress to copy billing to shipping address fields that WooCommerce adds to user profiles.  This is quite a tedious task if you are manually creating users via the WordPress dashboard.  This article will show you how to use jQuery with a bit of PHP in WordPress to copy the billing to shipping address in WooCommerce user fields.

This plugin is now available for download at WordPress.Org

Download Plugin

Copy Billing to Shipping Address jQuery

To copy billing to shipping address in WooCommerce we need to write some jQuery that will duplicate our values entered for the billing fields.  Here is the jQuery that will copy the billing to shipping address fields.

In order for this script to copy billing to shipping address fields we need to add a checkbox control to the user edit page in WordPress and also load this Javascript on that page.  This script will copy the billing to shipping address fields when a checkbox is checked and delete the shipping field values when it becomes unchecked.

 

Load jQuery script on WordPress dashboard

We only want to load our jQuery script to copy billing to shipping addresses when we are on the user edit page.  To do this we insert the following code in our themes functions.php file.

This will only load our script into the header for the page ‘user-edit.php’.

 

Checkbox to copy billing to shipping address

Now in order to display a checkbox to copy billing to shipping address on the user edit page we need to add the following code to functions.php.

Since we assign a low priority to our function it will appear at the bottom of the user edit page.  There are two hooks used here to copy the billing to shipping address called edit_user_profile and show_user_profile.  Basically these two hooks cover the scenarios of editing your own profile or another person’s profile.

 

The finished product

Here is the result, we have created a combobox to copy billing to shipping address fields that WooCommerce has add via the WordPress dashboard.

copy billing to shipping address woocommerce

31 thoughts on “Copy billing to shipping address in WooCommerce

  1. very impressed. immediate service. works perfectly. you should sell this plugin must be heaps of people wanting this functionality.
    solved a big headache for us – much appreciated

      1. Andrew, first thanks for your efforts. the internet would be just a mess without kind folks like you. next, is there any way I can get a copy of that plug-in you set up for Matt? I am having the same issue with WC and needing to duplicate billing to shipping.

  2. Is there a way to add this feature to a themes frontend for the user to be able to check the box himself… For example using a plugin like usersultra pro… how would you add a check box to that? or would you add it to the theme? What hook would you use instead? it wouldn’t be user-edit.php would it?

  3. I’ve seen a lot of themes that automatically duplicate the billing to shipping address already with a checkbox. My recommendations would be based on what theme you were using…

  4. Hi Andrew, I’ve tried to edit my functions.php and use your code. Thank you. But will my changes be lost if I update my theme?

    And where should I save the jquery?

    Can you give me a hint?

    Thank you very much

    1. I’ve sent you a copy of the plugin so you won’t seen to worry about your theme code being overwritten. Saying that you should always use a child theme and never directly edit the parent functions.php file.

  5. Hi Andrew

    i have used code above its nice but nothing copied from billing address to shipping address form. can you please give me any suggestion. in shipping tab am selected default to billing address radio button in woocommerce setting page

  6. Great work!

    Any way to add a “duplicate” checkbox to front-end? At the moment a checkbox is added only to the dashboard but new themes enable front-end account administration. This way a customer would be able to fill-in his billing details and check the “duplicate” box and hit “Save.” This would save some time and improve user experience for those who are just setting up their account.

    1. Yes you could add some code that duplicated the billing to shipping in the front end. What I’ve actually done is write some back end code that hook into the new order event so that when a customer completes their order it automatically duplicates the billing to shipping address in the background. Much neater this way as you don’t need any addition steps on the front end. I will write an post showing you how to do this 🙂

  7. Hey I was wondering if you have this working successfully for 4.3.1 or above? I received many errors, however I’m also integrated with the dokan plugin.

    Also, in duplicate-address.php, lines 17 and 18 are duplicated

    update_user_meta($user_id, ‘shipping_company’, $all_meta_for_user[billing_company][0]);
    update_user_meta($user_id, ‘shipping_company’, $all_meta_for_user[billing_company][0]);

    I don’t think that would be intentional.

    Let me know if the plugin will be certified to newer versions of WordPress.

    Thank you!

  8. Hey Andrew

    First of all , Nice job done by you, is there a way in which we could do this in batch? Since i have thousands of users and i need to copy all their billing details to shipping.

  9. Hey,

    I’m getting an error after activating the plugin which is:

    [04-Oct-2017 16:56:30 UTC] PHP Warning: Missing argument 3 for Woocommerce_Duplicate_Billing_Address_Public::order_duplicate_billing_address() in /home/sitename/public_html/wp-content/plugins/woocommerce-duplicate-billing-address/public/class-woocommerce-duplicate-billing-address-public.php on line 167
    [04-Oct-2017 16:56:30 UTC] PHP Fatal error: Call to a member function get_user() on a non-object in /home/sitename/public_html/wp-content/plugins/woocommerce-duplicate-billing-address/public/class-woocommerce-duplicate-billing-address-public.php on line 171

    Please help me,

    I’ll be thankful to you

    Thanks !

Leave a Reply

Your email address will not be published. Required fields are marked *