Set up back in stock for Magento 2
Learn how to enable back in stock tracking for Magento 2, so you can send email alerts to customers interested in purchasing out of stock items.
This guide refers to Klaviyo's v1/v2 legacy APIs.
We are in the process of updating these guides, please check back later! For more information about the differences between legacy and new APIs, check out the comparison chart and [new API overview] (ref:api_overview).
Learn how to enable back in stock for your Magento 2 store, allowing you to send email alerts to customers interested in purchasing out of stock items. You have full control of (and responsibility for) when, where, and how the trigger button appears; the appearance, text, and behavior of the modal; and all other aspects of the feature's behavior and appearance within your store.
Once you are successfully sending back in stock events to Klaviyo, you can set up a back in stock flow to ensure that profiles who subscribe to receive back in stock emails are notified.
Magento 2 setup recommendations
To implement back in stock for Magento 2, we recommend you have the following:
- Form or input field on the product page that renders when an item is out of stock. The form should collect email addresses of customers who want to be notified when an item gets restocked.
- On form submission, send the data to Klaviyo's back in stock API. When a customer submits the form, make a call to the API, subscribing the customer to restock notifications.
- Once the data has been communicated to the API, configure automated back in stock alert notifications.
Account settings and flow triggering
Once a Subscribed to Back in Stock event has been captured, the Back in Stock Settings will appear under Account > Settings > Email in your account. If you don't see these settings, make sure to trigger an event first. You can use these settings to determine how you would like to trigger back in stock flows.
To set up a back in stock alert flow, take a look at our How to build a back in stock flow guide.
Subscribing customers to back in stock alerts
When a customer subscribes to an alert when an item returns in stock, send a request to our back in stock endpoint. There are server-side and front-end versions of this request.
Server-side request
To send subscription information to the server-side API, use a POST request. The information is sent to the server via the data JSON object, and serialized as a URL-encoded form. The JSON object requires all of the following arguments:
a
- your public API keyemail
- the email address of the person subscribing to alerts for this itemvariant
- the ID of the variantplatform
- this should be set tomagento_two
store
- the numeric store ID
curl -X POST \ https://a.klaviyo.com/api/v1/catalog/subscribe \
-F a=abc123 \
-F [email protected] \
-F variant=64814-0004 \
-F platform=magento_two \
-F store=1
Front-end request
A back in stock subscription request can also be sent using JavaScript. To do this, include the following parameters as an ajax request.
a
- your public API keyemail
- the email address of the person subscribing to alerts for this itemvariant
- the ID of the item (must correspond with $id in the field mapping step)platform
- this should be set tomagento_two
store
- the numeric store ID
$.ajax({
type: "POST",
url: "https://a.klaviyo.com/onsite/components/back-in-stock/subscribe",
data: {
a: "abc123",
email: "[email protected]",
variant: "64814-0004",
platform: "magento_two",
store: 1
},
success: function(response){
console.log(response) }
})
API responses
Successful back in stock subscription
{"email": "[email protected]", "success": true}
Invalid email address or API key
{"errors": ["There was something wrong with your request. Please try again."], "data": {}, "success": false}
Missing form data parameters
{"status": 400, "message": "There was something wrong with your request: * email * This field is required."}
Invalid variants & duplicate subscriptions
- The API does not validate variant IDs. If an invalid variant ID for an item is included in a request, the API will return a standard success response. No event will be created for this profile, and the customer will not be queued to receive a back in stock message.
- It is not possible to submit multiple subscriptions for a profile on the same item variant. If multiple requests are submitted for the same email address on the same variant, only the first will be accepted.
- There is no limit to the number of unique item variants a profile can subscribe to.
- After an individual receives an email notifying them that a specific variant is back in stock, their existing subscription is cleared. They are then able to sign up for another subscription on that same variant.
Additional resources
Updated over 1 year ago