Tetchi x Wordpress

Shopify Theme from Scratch Part 12: Customer Accounts 1/2

part12_banner

Today we’re going to set up four of the seven customer account templates. You may be thinking “whoa whoa, four templates in one tutorial!? You’re crazy!!”. Don’t sweat! A lot of these templates are really simple and we can re-use a lot of the same CSS classes across templates.

Let’s dew this!

1. Add customer links to the header

The first thing we want to do is add a couple of links in our theme’s header bar so that customers can log in to their customer account or create a customer account. Remember, anything that we want to appear on every page of our theme should go inside theme.liquid.

Open theme.liquid and add in the following code.


...

  
...

Now let’s add a bit of CSS to line up the links nicer. Open style.css, and up in the “theme.liquid” section, add the following line in red:


...

/**** theme.liquid ****/

.container{width: 960px; margin: 0 auto;}
.header-toolbar{background:#000; color: #fff; font-size:12px; padding:10px 0;}
.customer-links{padding:7px 0;}

...

You should now see two link in the top left corner of your theme for logging in and creating an account (Figure 12.1).

customer links

Figure 12.1: Two new links for customers to log in or create a customer account.

2. Set up login.liquid

2.1. Add the login form

Open the login.liquid template. Delete the contents that are currently in it and add the following code:


{% layout settings.customer_layout %}

The “{% layout settings.customer_layout %}” line is inserted at the top of every customer account template. It tells the template which layout to use: either “default” or “theme” (recall in Part 11 how the Theme Settings give you the option to choose a layout for the customer account pages: either Shopify’s default layout or the theme’s layout).

You should now see a form with two fields: Email Address and Password, as well as a link for password resetting.

login template

Figure 12.2: login.liquid – we’ll tidy it up shortly.

2.2. Add the password reset form

The login page also includes a form in case a customer forgets his/her password. Let’s go ahead and add the code for that. Still inside login.liquid, insert the code in red at the very bottom:


...

    {% endform %}
  

Reset Password

We will send you an email to reset your password. {% form 'recover_customer_password' %} {{ form.errors | default_errors }}
or Cancel
{% endform %}

You’ll now see the password reset form below the login form.

reset_form

Figure 12.3: Another form is added to login.liquid in case customers forget their password.

2.3. Make the two forms toggle

What we’re going to do next is make it so that the password reset form is hidden by default, and make the “Forgot Password?” link makes the login form and password reset form toggle on every click. Let’s begin by adding some CSS to hide the password reset form, and at the same time tidy up the form a bit.

Open style.css, and add the following bit of CSS at the very bottom:


...

/**** Customer Accounts  ****/

.customer-login, .guest-login{float:left; width:465px;}
.customer-login{margin-right:30px;}
.form-row{margin-bottom:10px;}
.form-row input[type="text"],.form-row input[type="email"],.form-row input[type="password"], .form-row label, .form-row select{display:inline-block;}
.form-row input[type="text"],.form-row input[type="email"],.form-row input[type="password"]{width:250px; border:1px solid #ccc;}
.form-row select{width:250px;}
.form-row label{width:120px;}
.form-row .btn{margin-top:10px;}
.recover-password,.reset-success{display:none;}

Now at the very bottom of login.liquid, add the following jQuery code.


...



The “Forgot your password?” link will now make it so that the two forms toggle. Click the “Forgot your password?” link, and this…

forgot_pass1

Figure 12.4: The fields are all lined up now and the password reset form is now hidden.

…will turn into this:

forgot_pass2

Figure 12.5: When you click the “Forgot your password?” link, the two forms will toggle.

Of course, hitting “Cancel” will do the opposite 🙂

2.4. Add a success message for the password reset form

Next we’re going to add this small bit of text below that indicates that the password reset form has been successfully submitted. This is done by using the form.posted_successfully Liquid variable and using its value to show the success message.

We’ll begin by adding a little paragraph for the success message. It will be hidden by default, thanks to the CSS that we added in 2.3 (.recover-password,.reset-success{display:none;}). Add the code in red below:


...

  
  

Next, within the password reset form, add the following code in red:


...

Reset Password

We will send you an email to reset your password. {% form 'recover_customer_password' %} {{ form.errors | default_errors }} {% if form.posted_successfully? %} {% assign reset_success = true %} {% endif %} ...

Here we’re creating a variable called “reset_success” and setting it to true if the password reset form was sent successfully. We’re then going to use the same variable within the jQuery snippet that was added in Step 2.3 to unhide the success message.

Within the jQuery snippet that we added earlier, add the code in red below:




Now, when you submit the password reset form, you will get a nice little confirmation message:

password reset confirmation

Figure 12.6: Confirmation message after a success password reset form submission.

2.5. Add the guest login form

The last thing we’re going to do is add the Guest Login form. This form is only output when you go to check out from the cart page.

Inside login.liquid, add the code below:


...

    {% endform %}
  
{% if shop.checkout.guest_login %} {% endif %}
...

On your storefront, go ahead and add a product to the cart and click the “check out” button on the cart page. You will be taken to the login page again, but this time you’ll see an option to check out as a guest.

continue as guest

Figure 12.7: Before a customer checks out, the customer should be given the option to log in or check out as a guest.

That was a doozie of a template huh? No worries, the other three are much simpler.

3. Set up register.liquid

Open register.liquid and delete the contents that are currently in it. Replace it with the following code:


{% layout settings.customer_layout %}

Create Account

{% form 'create_customer' %} {{ form.errors | default_errors }}
{% endform %}

Notice that in the markup of this template, we’re using a lot of the same classes from the login.liquid template, such as “form-row”. This makes it so that the forms in these templates will look consistent with each other.

To view this template, click on the “Create an Account” link that we created in Step 1. You should now see a neatly-styled register page (Figure 12.8).

register

Figure 12.8: Customers can create customer accounts on their own via register.liquid.

4. Set up reset_password.liquid

Open reset_password.liquid and remove whatever’s in there now. Replace its contents with the following code:


{% layout settings.customer_layout %}

Reset Account Password

Please enter a new password for {{ email }} {% form 'reset_customer_password' %} {{ form.errors | default_errors }}
{% endform %}

To view this template, you must send out a “reset password” email through the admin first. Go to the Customers page in your admin, and select the customer account that you created in Part 11. Click the “Reset password” button in the top-right corner, and hit Submit.

reset_password_btn

Figure 12.9: You can send a password reset email through the “Reset password” button in the Customer’s admin page.

An email will then be sent to the email assigned to your test customer account. Within it you’ll find a link to reset the password. Click that link and you’ll be taken to the reset_password.liquid page.

password reset email

Figure 12.10: The reset_password.liquid can be viewed by clicking the link in the password reset email.

Customers can change their passwords through this template 🙂

reset_password_account

5. Set up activate_account.liquid

Open activate_account.liquid. Replace its contents with the code below:


{% layout settings.customer_layout %}

Activate Account

Please enter a password for your account. {% form 'activate_customer_password' %} {{ form.errors | default_errors }}
or
{% endform %}

Viewing this template requires an extra step as well. What you’ll have to do first is add a new customer to your shop by clicking the green “Add customer” button in the top right of the Customers page in the admin.

add customer

Figure 12.11: The “Add Customer” button will register a new customer in the Customers page of the admin, but this new customer will not have his/her own customer account yet.

Go ahead and fill this form out to create another customer for your shop. The difference between this new customer and the customer you created in Part 11 is that this new one does not yet have a customer account for viewing past orders and changing addresses. In other words, the customer is registered in the “Customers” page in the admin, but does not yet have his/her own customer account for logging in through the login.liquid template.

send account

Figure 12.12: Clicking the “Send account invite” button will open a lightbox containing an activation URL.

Copy the URL that is inside the pop-up, and paste it into the URL bar of your browser. Hit enter to go to that URL.

account url

You will now see the contents of the activate_account.liquid template that we just set up. Again, since we’re using the same CSS classes, the look-and-feel of this template is consistent with the others.

active account template

Take a breather!

That’s all for today! Pat yourself on the back for slaying four templates all at once (^0^)/. Just three more customer account templates to go!

← Back to table of contents

17 Comments

Khoi Nguyen

July 29th, 2016

Raniel, to show a success or error message, you must set form erros to display INSIDE a {% form %} function.

Raniel

July 10th, 2016

Hey tetchi,

NICE tuts…. very helpful..

have a question,,,

why the success message.. doesn’t show when reset password is done..

Mike

November 2nd, 2015

I realize this tutorial is a bit old, and you probably aren’t supporting it anymore, but for the benefit of others:

Guest login form appears to be deprecated. The cart “Checkout” button goes straight to https://checkout.shopify.com which is unthemable (unless a Plus theme, which I don’t know how to setup yet!)

Recover password has strange behaviour when the email is incorrectly entered. Once the form is submitted, the page refreshes and the error message shows up within div.recover-password, which is hidden. Here is a simple fix. Add it to the script at the bottom of login.liquid:

if( $(‘.recover-password .errors’).length ) {
$(“.customer-login, .guest-login, .recover-password”).toggle();
}

This will (instantaneously) toggle back to the recover password form if there is an error shown there. A bit inelegant, but it is a workaround for the behaviour of form.posted_successfully? – which always returns true for address forms. See: https://docs.shopify.com/themes/liquid-documentation/objects/form#form-posted_successfully

Joy Haines

September 23rd, 2015

Hi Tetchi,
I was wondering if you could help me. I’ve been following your shopify theme tutorial which is great thanks!
I’m having problems with multiple dropdown fields to each of our 3 kinds of contact forms in our website. When I did a test only one of the dropdown field info showed up in the email. Why is this? I’m using a solo theme.
Looking forward to hearing from you.
Cheers,
Joy Haines
Quick Print Tees

password for website: sandman

Rashad

October 24th, 2014

Each one of these transactions may need additional operations to take place.

Performed range of clinical functions in a 70-bed neonatal ICU, assdessed patients’ developmental stages and conditions, administered medications, maintained
patient charts and responded to medical emergencies.

If we can prevent an emergency from happening in the first place,
we are that much more effective in saving the lives of our children.

Parveen Verma

August 16th, 2014

Hi Tetchi,
Thanks for detailed tutorials, you have been of a great help to many of us.

I have a question related to customer accounts.

Can you please guide me if there is at all possibility of entertaining login/signup errors on the same page without reloading the page?

For example: if a user hits login or signup button without entering anything, the page reload and then throws related error.

Can we handle this with jquery? I see that the errors refer to this form {{ form.errors | default_errors }} but i can’t find it anywhere.

Thanks for your support in advance.

Katy

July 31st, 2014

Thank you very much for these clear and easy to follow tutorials. You have been very helpful! This is a small thing, and I hate to point out a grammatical error, but you might want to edit “Instructions…has been sent” to “Instructions….have been sent.
Thanks again!

Carson Shold

January 27th, 2014

Just a note on this, I was confused with how the success message was shown when the ‘recover_customer_password’ was submitted.

When the page reloads, the liquid variable form.posted_successfully? is set to true ONLY within the form that was submitted, which in this case is hidden. By assigning the variable ‘reset_success’ within the hidden form, it allows us to check if ‘reset_success’ is true later on in the page with JavaScript, and subsequently show the form.

tetchi

October 10th, 2013

@Tim – sorry for the late response! This is actually being caused by a bug in Shopify where {{ form.errors }} is not being output properly. I will let you know once it’s fixed!

Tim

October 3rd, 2013

Hey tetchi, this tutorial series has been a fantastic help to me. Thanks so much!

I’m having a problem with this section. Basically, on your forgot your password form, if you submit an email address that isn’t in the database, it should show an error. However, after hitting the submit button, the regular login form displays again with no message indicating that the email address is invalid. Only after clicking “forgot your password” again do you see the error.

I was pulling my hair out trying to figure out what I did wrong, but I notice the same behavior in your demo shop. Is there a way we can have it so if the email address entered in the forgot your password box is not in the database, the page will reload with the forgot your password form shown by default? Or at the very least, can we have the error message appear on the regular login form so the user isn’t completely in the dark?

Thanks again for this great tutorial series.

tetchi

July 25th, 2013

My pleasure Cheryll 🙂

Cheryll

July 23rd, 2013

Perfect! Thank You! That worked beautifully!

tetchi

July 23rd, 2013

@Cheryll Looks like you have it set to “Accounts are required”. The register link will only be shown when you have it set to “Accounts are optional”. The code assumes that if you have it set to “Accounts are required”, you don’t want customers to create an account for themselves.

If you’d like to make the Register form link appear regardless, use this code instead: http://pastie.org/pastes/8167599/text

tetchi

July 23rd, 2013

Hey Cheryll, I’ll get back to you tomorrow on this, I think I know the issue. I’m just on the road now so I can’t troubleshoot right away.

Cheryll

July 22nd, 2013

Yes I have.

tetchi

July 22nd, 2013

@Cheryll Have you enabled customer accounts, as outlined in Part 11?

Cheryll

July 22nd, 2013

I am having trouble with the create an account link. I have copied and pasted and followed the directions but I seem to have missed something because it is not showing up.

Any ideas?

Thanks in advance!