cartjs icon indicating copy to clipboard operation
cartjs copied to clipboard

Expose more data on product

Open bakura10 opened this issue 9 years ago • 7 comments

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!

bakura10 avatar Jan 20 '16 08:01 bakura10

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:

  1. 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;
  2. 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.

gavinballard avatar Jan 20 '16 10:01 gavinballard

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! :)

bakura10 avatar Jan 20 '16 10:01 bakura10

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.

gavinballard avatar Jan 20 '16 10:01 gavinballard

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...).

bakura10 avatar Jan 20 '16 10:01 bakura10

@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!

xiangcatherine avatar Jul 23 '18 17:07 xiangcatherine

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.

gavinballard avatar Jul 25 '18 11:07 gavinballard

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!

xiangcatherine avatar Jul 25 '18 18:07 xiangcatherine