All things Shopify and commerce
We're moving the community! Starting July 7, the current community will be read-only for approx. 2 weeks. You can browse content, but posting will be temporarily unavailable. Learn more
I'm building an app with shopify as headless CMS, I need to make a part where a visitor can subscribe to our news letter, two cases:
-When client has account, I can subscribe using storefront API to update 'acceptsMarketing' and set it to true so that marketing apps can use it to email our campaigns.
-When it's a visitor, I'm not sure how to do this, is there a way to subscribe even a visitor?
Hi @techboas
It sounds like you're working on integrating a newsletter subscription feature into your Shopify-based headless setup. You’ve already got the approach sorted out for logged-in customers by updating the acceptsMarketing field via the Storefront API, but handling guest (visitor) subscriptions is where you're stuck. Let’s break it down.
Since visitors don’t have accounts, they aren’t recognized by Shopify's customer model. This means you can’t update acceptsMarketing directly for them. However, there are two solid approaches to get around this:
Many email marketing platforms like Klaviyo, Mailchimp, and Omnisend offer APIs that allow you to subscribe users without needing a Shopify account. This is the easiest and most scalable approach. Here’s how you can do it:
fetch('https://a.klaviyo.com/api/v2/list/YOUR_LIST_ID/members', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
api_key: 'YOUR_KLAVIYO_PRIVATE_KEY',
profiles: [{ email: "visitor@example.com" }]
})
});
This will add the visitor's email to your Klaviyo list.
If you prefer keeping all data in Shopify, you can create a customer record for visitors (without requiring them to log in) and set acceptsMarketing to true.
POST /admin/api/2024-01/customers.json
{
"customer": {
"first_name": "Newsletter",
"last_name": "Subscriber",
"email": "visitor@example.com",
"accepts_marketing": true
}
}
Limitations: This will add visitors to your customer list, which may not always be ideal unless you want to manage them as contacts in Shopify.
If you need any other assistance, feel free to reply and I will try my best to help.
Best regards,
Daisy