Skip to main content
All CollectionsAdvanced TopicsAbandoned checkouts
How to build a prefilled (discounted) cart
How to build a prefilled (discounted) cart

Learn how to load a cart page that contains specific products, optionally with a Discount Ninja promotion applied

Bart Coppens avatar
Written by Bart Coppens
Updated over 8 months ago

Not sure how to edit your theme? Read this article first.

Preparation

  • Open your theme

  • Click "Add a new template":

  • Select the template type "cart" and provide a name. In this example, we're using prefill. You can use a different name if you prefer.

  • Make sure to select "Liquid", not "JSON"

  • Replace the content of the newly created template with the following:

<!doctype html>
<html>
<body>
<script type="text/javascript">
document.addEventListener("la:dn:promotions:loaded", function() {
discountNinja.api.cart.prefill();
});
</script>
</body>
</html>
  • Click "Save"

Usage

You can now send your customers to a prefilled cart using a URL that has the following format:

https://myshop.com/cart?view=prefill&variants=variantidxquantity

This URL has the following required components:

  • myshop.com: replace this with the domain of your shop

  • /cart?view=: this part is mandatory

  • prefill: this is the name of the template; if you selected a different template name use it here

  • &variants=: this part is mandatory and requires a comma-separated list of the variant id of the product variants you want to add to the cart; for each variant you'll need to specify the quantity. Example: to add 2 of variant 1234567 and 1 of variant 2345678 use &variants=1234567x2,2345678x1

Additionally, you can add a token to the URL to trigger a Discount Ninja promotion that has a shareable link. If the trigger of your promotion is ABCDE then the link becomes:

https://myshop.com/cart?view=prefill&variants=variantidxquantity&token=ABCDE

Finally, you can define which page should be loaded once the cart is prefilled using a redirecturl parameter. By default the link will send customers to the cart page. If you want customers to land on the collections page instead you could use:

https://myshop.com/cart?view=prefill&variants=variantidxquantity&token=ABCDE&redirecturl=/collections/all

Behavior

This approach:

  • clears the cart first

  • then adds the specified variants

This ensures the content of the cart will always be as defined in the URL, independent of what a customer had already added to the cart before.

Did this answer your question?