Shopify Theme from Scratch Part 12: Customer Accounts 1/2
Shopify Theme from Scratch Part 12: in this tutorial we’re going to be build four of the seven customer account templates: login.liquid, register.liquid, reset_password.liquid, and activate_account.liquid.
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.
...
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.
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…
…will turn into this:
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:
...
Customer Login
Instructions for resetting your password has been sent.
{% form 'customer_login' %}
{{ form.errors | default_errors }}
...
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:
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 %}
Guest Login
{% form 'guest_login' %}
{% endform %}
{% 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.
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).
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.
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.
Customers can change their passwords through this template :)
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.
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.
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.
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.
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!
17 Comments
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!
@Cheryll Have you enabled customer accounts, as outlined in Part 11?
Yes I have.
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 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
Perfect! Thank You! That worked beautifully!
My pleasure Cheryll :)
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.
@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!
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.
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!
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.
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.
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
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
Hey tetchi,
NICE tuts…. very helpful..
have a question,,,
why the success message.. doesn’t show when reset password is done..
Raniel, to show a success or error message, you must set form erros to display INSIDE a {% form %} function.