Forum

Thread tagged as: Question, Problem, Shop

Perch Shop - "Use As My Billing Address"

Hi guys,

Is anyone able to shed light on this for me? I've looked around but I'm not finding much for it.

I'm looking to have a checkbox for "Use As My Billing Address" underneath the Shipping Address form for a new customer, which will allow the customer to go through the checkout process without having to manually enter a billing address.

I've played around with AJAX but that wasn't going anywhere, my current code is here: site.js

// Billing Address Form Filler
$(document).ready(function(){
    $("form.customer-create").submit(function(){
        if($("input#use-billing").checked){
            var form = [];
            $("form.customer-create input").each(function(f){
                //console.log($(this).attr('id') + ":" + $(this).val());
                form[f] = $(this).val();
            });
            var country = $("form.customer-create select").val();
            $("input[name='address_1']").val(form[8]);
            $("input[name='address_2']").val(form[9]);
            $("input[name='postcode']").val(form[10]);
            $("input[name='city']").val(form[11]);
            $("input[name='county']").val(form[12]);
            $("input[name='country']").val(country);
            return true;
        }
        alert($('form.customer-create'));
        console.log($('form.customer-create'));
    });
});

customer_create.html

<h2>Create an account</h2>

  <perch:form id="register" method="post" app="perch_shop" class="default-form customer-create">
  <div>
    <perch:input type="text" id="first_name" required="true" label="First name" placeholder="First Name"/>
    <perch:error for="first_name" type="required">Please add your name</perch:error>
  </div>
  <div>
    <perch:input type="text" id="last_name" required="true" label="Last name" placeholder="Last Name"/>
    <perch:error for="last_name" type="required">Please add your name</perch:error>
  </div>
  <div>
    <perch:input type="email" id="email" required="true" placeholder="Please enter your email address" helper="PerchMembers_Members::check_email" />
    <perch:error for="email" type="required">Please add your email address</perch:error>
    <perch:error for="email" type="helper">That email address is already in use</perch:error>
  </div>
  <div>
    <perch:input type="password" id="password" required="true" match-with="password2" placeholder="Password"/>
    <perch:error for="password" type="required">Please add a password</perch:error>
    <perch:error for="password" type="match">Passwords do not match</perch:error>
  </div>
  <div>
    <perch:input type="password" id="password2" required="true" placeholder="Confirm Password"/>
    <perch:error for="password2" type="required">Please repeat your password</perch:error>
  </div>
  <h3>Shipping address</h3>
  <div>
    <perch:input type="text" id="shipping_first_name" required="true" label="First name" placeholder="First Name"/>
    <perch:error for="shipping_first_name" type="required">Please add your name</perch:error>
  </div>
  <div>
    <perch:input type="text" id="shipping_last_name" required="true" label="Last name"  placeholder="last Name"/>
    <perch:error for="shipping_last_name" type="required">Please add your name</perch:error>
  </div>
  <div>
    <perch:input type="text" id="shipping_address_1" required="true" label="Address 1"  placeholder="Address 1"/>
    <perch:error for="shipping_address_1" type="required">Please add the first line of your address</perch:error>
  </div>
  <div>
    <perch:input type="text" id="shipping_address_2" label="Address 2" placeholder="Address 2"/>
  </div>
  <div>
    <perch:input type="text" id="shipping_postcode" label="Postal code" placeholder="Postal Code"/>
  </div>
  <div>
    <perch:input type="text" id="shipping_city" label="City"  placeholder="City"/>
  </div>
  <div>
    <perch:input type="text" id="shipping_county" label="County"  placeholder="State or County"/>
  </div>
  <div>
    <perch:label for="shipping_country">Country</perch:label>
    <perch:input type="select" options="<perch:shop id="country_list" />
    " value="236" id="shipping_country" label="Country" /> </div>

  <input type="checkbox" id="use_billing" value="use_billing" checked><label id="use_billing" for="use_billing">Use This As My Billing Address</label>

  <div id="billing-address">
  <h3>Billing address</h3>
  <div>
    <perch:input type="text" id="address_1" required="true" label="Address 1" placeholder="Address 1"/>
    <perch:error for="address_1" type="required">Please add the first line of your address</perch:error>
  </div>
  <div>
    <perch:input type="text" id="address_2" label="Address 2" placeholder="Address 2"/>
  </div>
  <div>
    <perch:input type="text" id="postcode" label="Postal code" placeholder="Postal Code"/>
  </div>
  <div>
    <perch:input type="text" id="city" label="City" placeholder="City"/>
  </div>
  <div>
    <perch:input type="text" id="county" label="County" placeholder="State of County"/>
  </div>
  <div>
    <perch:label for="country">Country</perch:label>
    <perch:input type="select" options="<perch:shop id="country_list" />
    " value="236" id="country" label="Country" /> </div>
    <perch:label>&nbsp;</perch:label>
  </div>
  <perch:input class="submit-button" type="submit" value="Register" />
  <perch:success>
    <p>Thanks!</p>
  </perch:success>
</perch:form>

The problem with this code is that I get an error in the console: An invalid form control with name='address_1' is not focusable.

Any ideas?

Harry Ray

Harry Ray 0 points

  • 4 years ago
Rachel Andrew

Rachel Andrew 394 points
Perch Support

This looks like a front-end issue, happy to leave it here in case other Perchers want to help but you may get more eyes on it in a more general webdev forum as this doesn't have anything Perch specific about it.

Duncan Revell

Duncan Revell 78 points
Registered Developer

It looks like you're using name= in your javascript, but only actually using id= in your html.

@Duncan - oh, you're right. Oops!

I can pass through the registration without listing a billing address, however, it won't let me check out fully if I don't register a billing address - is this expected behaviour?

Duncan Revell

Duncan Revell 78 points
Registered Developer

If you only want your customer to enter one address, flip it around. Get them to enter a billing address (which is needed) and provide a tick-box to "use this as shipping address".

Just a quick note - the name selectors work, because when the form renders on the front end, each field renders with a name.

I'll flip it around though - good idea!

Thanks for the help! Harry

Duncan Revell

Duncan Revell 78 points
Registered Developer

Ah, I did wonder before posting my reply, but didn't check. Good to know. I also can't recall if any prefixes are added to either id or name - but checking source will confirm.

Drew McLellan

Drew McLellan 2638 points
Perch Support

You can always use a data- attribute on an input to give yourself a handy reference that won't change.