How to display compare_price on cart page for each cart item
Have you ever needed to display the compare_at price for each cart item?
Unfortunately as you've probably discovered this field isn't included in the /cart.json response
Here's an easy approach to retrieve the data for each cart item and displaying it with Liquid
fetch('/cart.js').then(data => data.json())
.then((cart) => {
cart.items.forEach((item) => {
fetch(`/variants/${item.id}.json`).then(data => data.json())
.then((data) => {
const variant = data.product_variant;
console.log(variant, variant.compare_at_price);
// ... do whatever you need to do with compare at price here
})
})
})
Thanks to Sam Webb for this week's tip.