eylul
eylul

Reputation: 101

Woocommerce copy shipping address to billing address

I have a Woocommerce florist shop. As people tend to send flowers to other people, I have changed the order of the billing & shipping fields by copying form-checkout.php to my child theme folder and moved the shipping part to the top. However, I also would like to include a button / checkbox that enables the users to copy shipping info to billing fields. Woocommerce offers just the opposite of this option (copying billing info to shipping) but I need the reverse version of this. Any help would be very much appreciated.

Upvotes: 1

Views: 6740

Answers (2)

Rohit Kaushik
Rohit Kaushik

Reputation: 1302

as answered above I make some changes and that codes worked fine for me like

Use checkbox instead of button and I am coping Billing address fields values to shipping address fields.

//Paste this code in your theme js file
jQuery(document).on('change', '#copy_to_billing', function() {
        if(this.checked) {
            jQuery("[name='shipping_first_name']").val(jQuery("[name='billing_first_name']").val());
            jQuery("[name='shipping_last_name']").val(jQuery("[name='billing_last_name']").val());
            jQuery("[name='shipping_address_1']").val(jQuery("[name='billing_address_1']").val());
            jQuery("[name='shipping_address_2']").val(jQuery("[name='billing_address_2']").val());
            jQuery("[name='shipping_city']").val(jQuery("[name='billing_city']").val());
            jQuery("[name='shipping_state']").val(jQuery("[name='billing_state']").val());
            jQuery("[name='shipping_postcode']").val(jQuery("[name='billing_postcode']").val());
            jQuery("[name='shipping_country']").val(jQuery("[name='billing_country']").val());
        }
    });

Go to your-theme/woocommerce/checkout/form-shipping.php (if file not exist then copy from woocommerce in same path under your theme folder). and use the checkbox field with same id 'copy_to_billing'

<input id="copy_to_billing" type ="checkbox" name="copy_to_billing" value ="1" class="copy_billing woocommerce-form__input woocommerce-form__input-checkbox input-checkbox"><span><?php esc_html_e( 'Shpping address is same as billing', 'woocommerce' ); ?></span>

Upvotes: 1

Tasos
Tasos

Reputation: 7587

You can use jQuery for that. Here is part of the code that you can use

  $("#copy_to_billing").on("click", function(){
    if (this.checked) {
      $("[name='first_name']").val($("[name='shipping_first_name']").val());
      $("[name='last_name']").val($("[name='shipping_last_name']").val());
      $("[name='billing_address_1']").val($("[name='shipping_address_1']").val());
      $("[name='billing_address_2']").val($("[name='shipping_address_2']").val());
      $("[name='billing_city']").val($("[name='shipping_city']").val());
      $("[name='billing_state']").val($("[name='shipping_state']").val());
      $("[name='billing_zip']").val($("[name='shipping_zip']").val());
      $("[name='billing_country']").val($("[name='shipping_country']").val());
    }
  });

Then you only need to add a button to trigger the code like that:

<button id="copy_to_billing">Copy shipping details</button>

Upvotes: 3

Related Questions