【发布时间】:2023-02-20 23:02:11
【问题描述】:
首先,我总是喜欢发布可运行的示例,但由于这是 js 和服务器端在 shopify 上呈现液体的混合,所以我无法获得运行示例。
在 shopify 中,您可以从产品模板访问 product 对象,就像 {{ product }} 一样。
购物车对象有一个items 属性,它是购物车中所有项目的数组。购物车中的每个 item 对象都不同于 product 对象。 product 对象有一个变体列表,购物车 item 对象没有。
这样做的目的是能够编辑购物车中商品的大小。
我的问题是,您如何才能获得所有链接的变体?您将不得不向上移动到产品并获得那里所有变体的列表,从它的变体 product_id。
这很棘手的原因是,当您获得购物车对象的获取响应时,您会为购物车中的每个 item 获得一个 product_id。除非您在产品页面上,否则您无法获得产品对象。
只是为了帮助可视化购物车是这样的:
{
items: [
{
handle: 'product-handle',
product_id: 123,
variant_title: 'product variant'
}
]
}
需要完成的是:
{
items: [
{
handle: 'product-handle',
product_id: 123,
/**
* to get this you need first access to the product object from the product
* template. You could convert the product to json with a filter
* e.g. const product = {{ product | json }} but you don't have the
* opportunity to be on the product template each time you edit a cart item
*/
variants: [
{ color: 'white', size: 's' },
{ color: 'white', size: 'm' }
]
}
]
}
【问题讨论】:
标签: javascript vue.js shopify liquid