Email Subscription
Customers can opt in to email marketing through the theme in two ways:
Newsletter Sign-Up Form
You can add a basic newsletter sign-up form to your template using Handlebars form tags and the necessary parameters. In the form, include an input with the following attributes: 'customer'
Attribute | Value |
---|---|
name | contact[email] |
type | email |
<div class="customer-email-box">
<label for="email">Email address</label>
<input type="email" name="contact[email]" />
</div>
<div class="customer-submit-btn">
<input type="submit" value="Sign up" />
</div>
When customers sign up through this form, a customer is created using the provided email, and the accepts_marketing
attribute of the associated customer
object is set to true
.
Note
For another example of a newsletter sign-up form, check out the Seed implementation.
Customer Registration Form Checkbox
In the customer registration form, you can include a checkbox to allow customers to opt in to email marketing. This requires the following input in the form:
Input | Type | name Value |
---|---|---|
Accepts Marketing | checkbox | customer[acceptsEmailMarketing] |
<div class="customer-account_box">
<label for="email"></label>
<input type="text" name="customer[email]" placeholder="" />
</div>
<div class="customer-password_box">
<label for="password"></label>
<input type="password" name="customer[password]" placeholder="" />
</div>
<div class="verifycode">
<input type="text" name="customer[verifycode]" placeholder="" />
<button class="verifycode-button"></button>
</div>
<div class="customer-first-name">
<label for="first-name"></label>
<input type="text" name="customer[firstName]" placeholder="" />
</div>
<div class="customer-last-name">
<label for="last-name"></label>
<input type="text" name="customer[lastName]" placeholder="" />
</div>
<div class="customer-birthday">
<label for="birthday"></label>
<input type="date" name="customer[birthday]" placeholder="" />
</div>
<div class="age">
<label for="age"></label>
<input type="text" name="attribute[age]" placeholder="" />
</div>
<div class="customer-marketing">
<input type="checkbox" name="customer[acceptsEmailMarketing]" value="true">
<label for="accepts-marketing">Sign up for email marketing</label>
</div>
<div class="customer-submit-box">
<input class="submit-button" type="submit" value="">
</div>
<script>
class Register {
constructor() {
this.form = document.querySelector('#create-customer-form');
this.form.addEventListener('submit', this.onSubmitHandler.bind(this));
this.verifyCodeButton = this.form.querySelector('.verifycode-button');
this.verifyCodeButton.addEventListener(
'click', this.onSendVerifyCodeHandler.bind(this)
);
this.register = new window.Shopline.customerAccount.Register(this.form)
}
// Logic to send email or SMS verification code
onSendVerifyCodeHandler(e) {
e.preventDefault();
if (this.verifyCodeButton.getAttribute('aria-disabled') === 'true') return;
this.verifyCodeButton.setAttribute('aria-disabled', true);
this.register.sendVerifyCode()
.then(response => {
if (response.errorMessage) {
this.verifyCodeError = true;
// Handle failure scenario
}
})
.finally(() => {
if (!this.verifyCodeError) {
this.verifyCodeButton.removeAttribute('aria-disabled');
}
})
}
// Registration logic
onSubmitHandler(e) {
e.preventDefault();
this.register.submit()
.then(response => {
window.location.href = '/user/center';
})
.catch(error => {
// Handle registration failure scenario
})
}
}
// Initialize the user login and registration JS SDK
window.Shopline.loadFeatures(
[
{
name: 'customer-account-api',
version: '0.2'
}
],
function(error) {
if (error) {
throw error;
}
new Register();
}
);
</script>
Was this article helpful to you?