cartjs
cartjs copied to clipboard
Expose more data on product
Hi,
One of our customer is using our theme. However, they'd like to be able to use display the product's tags in the cart. But it seems that CartJS do not expose those properties.
Would it be possible to expose the full "product" object (currently, only a subset with "product_id", "product_description", "product_title" and "product_type" are exposed, it would be nice to have a "product" hash with all the properties).
Thanks!
Hi @bakura10!
We just expose the same product data that Shopify's Ajax API returns in the cart object, which doesn't include tags. To use and render that information using Cart.js, there are two potential options:
- On page initialisation, use a GET request to load in product information for each product item in the cart see here, and link that information to your cart model;
- If you have a reasonably limited list of products, you could render a mapping of product IDs or handles to tags using Liquid and the
all_products
variable and pass that to CartJS on initialisation.
Pulling in the product information via Ajax would potentially be something we could enable in the core library, but as it involves the expense of multiple additional Ajax requests it wouldn't be enabled by default.
Hi,
Thanks for the answer. The all_products is definitely not a solution for us in themes as we cannot know in advance how many products.
The other idea sounds nice, it would indeed be good to have an opt-in parameter "loadProduct" to CartJS that would do this code transparently.
Thanks! :)
Cool. I'll look into this (probably don't hold your breath on it being included soon though ;)). If you end up implementing something yourself do let me know and I can look in to incorporating it.
No problem ;). I'm not raelly in a hurry either (yeah, we're starting apps so I have tons of work next to themes :D...).
@gavinballard
I'm looking into this option: "On page initialisation, use a GET request to load in product information for each product item in the cart see here, and link that information to your cart model"
Could you give a bit more guidance and information on how to link the information retrieved through the GET request to the cart model?
Thank you - this plug-in is amazing and has helped me a lot!
Hi @xiangcatherine, thanks for the kind words! It would depend a little bit on how you're planning on using the information. I have pretty limited time for diving into these things these days, but if you can provide a use case I can do what I can.
Thank you @gavinballard !
My client displays text on the cart page depending on product tags, for instance:
{% if product.tags contains "preorder-4weeks" %}
{% if settings.week2-show %}
<div class="complimentary custom-tag-wrap">
<p>{{ settings.preorder-4weeks }}</p>
</div>
{% endif %}
{% endif %}
I understand using a GET request to load product information, but how do I link that information to the cart model?
Thank you again!