CART JSON doesn't include compare at price in item object

RHM
Shopify Partner
26 0 10

Why the cart.json doesn't return the compare at price of each item? 

I'm using an ajax cart, and I would like to display how much the clients are saving on products on sales. 
Each item returns: line_price:1400  / original_line_price:1400  / original_price:1400 / price:1400

But all these properties are related to discount codes, so there is no way to know if a product is on sale.

Wouldn't make sense to add the compare_at_price in the Item object ? 

Robots Hate Monkeys
Jamie_D_
Shopify Staff (Retired)
Shopify Staff (Retired)
531 1 99

Hey Frederico,


You can use the variant ID to fetch the compare at price from the product endpoint. There aren't any immediate plans to add the compare at price to the cart model.

RHM
Shopify Partner
26 0 10

I'm actually storing the compare at price in localstorage when the user clicks on the "add to cart" button, using variant ID as key and the price as value, so I can easily get the value for each cart item.

 

Robots Hate Monkeys
0 Likes
HandHugs
Shopify Expert
165 1 36

how do we use the variant ID to get the compare_at_price? can you post an example?

Jason
Shopify Expert
10273 144 1943

Out of interest, why do you need this via cart.json? What's the use case?

I jump on these forums to help and share some insights. Not looking to be hired, and not looking for work.

Don't hand out staff invites or give admin password to forum members unless absolutely needed. In most cases the help you need can be handled without that.


★ http://freakdesign.com.au ★
0 Likes
HandHugs
Shopify Expert
165 1 36

My client wants to display a message about products on sale being FInal Sale in the cart - and only display the notice if there is a sale product in their cart. Since we are using an AJAX cart, i need to loop through the items in JS and check if one is on sale and then conditionally hide / show the message.

 

 

0 Likes
Raja_Khoury
Tourist
3 0 2

Frederico is right. Spent hours today debuging and I have the same issue. The Cart get endpoint returns line items without the compare at price ( It is included but is equal to sales price ).

Why would we need this via JSON ? Why would Shopify release include in their official theme - I need it because I'd like to show in my cart drawer The Item Sales Price and Original Price, why not ?

Please see here in Timber - In Cart Handlerbars template it is expected a discounted price and if you look also in cart-ajax.js of this theme you would see it is also expecting the discounted price to be reflected in the JSON response.

This is my first attempt at shopify coming from Laravel and to be honest i am very discouraged, documentations are lacking info and are outdated besides many other limitations. Maybe the guys at shopify don't care but it's my humble opinion. 

Jason
Shopify Expert
10273 144 1943

If you need different data to be returned from an endpoint, make an alternate template for the cart that returns a JSON string containing whatever you need.

This might not cover all use cases but should for the one talked about here.

I jump on these forums to help and share some insights. Not looking to be hired, and not looking for work.

Don't hand out staff invites or give admin password to forum members unless absolutely needed. In most cases the help you need can be handled without that.


★ http://freakdesign.com.au ★
0 Likes
Raja_Khoury
Tourist
3 0 2

Hello Jason, Thanks for the reply. I understand and it is what I am actually doing, using timber as a starting point but the problem here seems from the backend.

E.g Choose any product and give it a Price & Compare at price, this should set the product on sales .
Now in my cart drawer ( Front end ) when I fecth the cart items via Json just as Timber did it doesn't include both prices only the Sales / Discounted price.

See Screenshot -  Discounted Price / Original Price / Line Price ect.. all are correct and equal to the sales price but there isn't a way to know if the product has a discount & the price before.


I hope I am missing something here  - What do you think ?

 

 

RHM
Shopify Partner
26 0 10
{%- layout none -%}
{%- for tag in product.tags -%}
  {%- if tag contains 'color' -%}
    {%- assign colortag = tag -%}
  {%- endif -%}
{%- endfor -%}
{
  "details":"{{product.metafields.custom_fields.product_details}}",
  "colortag":"{{colortag}}",
  "product":{{product | json }}
}

What Jason suggest solves the issue. I use custom json templates everywhere. 
Attached an example of product json with 2 extra parameters: a metafield and a tag.
I use this to display via ajax alternative colors.

The default product.json doesn't include metafields, and instead of goign through all the tags by code, i expose the one I need ( colortag ). 

Raja Khoury you can do the same with any template.

Robots Hate Monkeys
0 Likes