Skip to main content

Determine if Customer's Next Order has a Free Gift

In this example you'll check a customer's upcoming order and check if it contains a free gift.

Pre-requisites

  • You have a Shopify store.
  • You have access to Rewards in your Recharge account. (See Rewards)
  • You have a customer with an upcoming charge or order.
  • You have basic knowledge of HTML and JavaScript.

Explanation

In this example we will list a customer's upcoming orders using listOrders and check the order's line items to see if line items are a free gift. We will use the LineItem.offer_attributes property to determine this by checking for offer_attributes.type === 'free_gift'.

Example Code

Methods and Types:

info

Note that this can also be done with the Charges API. You can use charges.listCharges to check a customer's upcoming Charges for free gift line items.

The Charges API can be used to check for free gifts applied to upcoming charges for existing subscriptions where there might not necessarily be an Order object.

<button id="check-order-free-gift-btn">Check if Upcoming Order has Free Gift</button>
<p id="order-free-gift-description"></p>

<script>
document.getElementById('check-order-free-gift-btn').addEventListener('click', async () => {
const session = await getSession();

const list_response = await recharge.order.listOrders(session, {
limit: 25,
sort_by: 'scheduled_at-asc',
status: 'queued',
});

if (list_response.orders.length === 0) {
return;
}

const upcoming_order = list_response.orders[0];
const free_gift_line_items = upcoming_order.line_items.filter(
item => item.offer_attributes && item.offer_attributes.type === 'free_gift'
);

if (free_gift_line_items.length > 0) {
document.getElementById(
'order-free-gift-description'
).innerText = `This order has 1 or more free gifts: ${free_gift_line_items.map(item => item.title).join(', ')}`;
}
});
</script>