在Shopify中更新/删除购物车属性 [英] Update/Remove cart attributes in Shopify

查看:481
本文介绍了在Shopify中更新/删除购物车属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用购物车属性将每个产品的额外信息添加到购物车(从产品页面). 我专门在订单项属性上使用购物车属性,因为客户需要能够按顺序编辑此信息,而稍后订单项就不会允许.

I am using cart attributes to add extra information for each product to the cart (from the product page). I am specifically using cart attributes over line item properties because the client needs to be able to edit this information in the order later on which line item properties don't allow.

添加信息就可以了,当客户决定从购物车中删除商品时,就会出现问题,因为尽管删除了商品,但购物车属性仍然保留,因为它没有专门绑定到产品上(除了命名之外)约定).

Adding the information works just fine, the problem comes in when a customer decides to remove an item from the cart because although the item is removed, the cart attribute remains since it is not specifically tied to the product (other than a naming convention that we give it.)

那么,有没有一种方法可以更新/删除特定的购物车属性?

So, is there a way to update/remove specific cart attributes?

提前感谢您的答复!

推荐答案

自从引入购物车属性以来,我就一直使用它们,并发现了与它们相关的大多数错误(Shopify一直在修复),这给了我有足够的信心将这个问题告知您.

I have used cart attributes since they were introduced, and found most of the bugs associated with them along the way (which Shopify fixed along the way), giving me enough confidence to inform you on this question.

要编辑它们,只需为购物车提供与创建它们相同的密钥,然后更改信息即可.如果您提供一个空值,则将删除键.它只是一个key:value存储.和订单项一样,顺便说一句,它们可以在我的书中进行编辑.我设法通过简单地更改键值并提供订单项索引值来对其进行编辑,也许您忽略了某些内容?

To edit them, simply provide the cart with the same key you used to create them, and change the information. If you provide an empty value, you'll remove the key. It is just a key:value store. As are line items, which by the way, are editable in my book. I managed to edit them by simply changing the value of the key, and by providing the line item index value, something maybe you overlooked?

无论如何,如果您使用提供的Shopify Javascript API代码并检查该小型库,则将看到用于创建属性的函数.稍作改动,您就可以引入自己的购物车属性CRUD功能来满足您的需求.

Anyway, if you take the provided Shopify Javascript API code, and examine that small library, you'll see the functions used to create attributes. With a little minor change, you can introduce your own cart attribute CRUD functionality to suit your needs.

这是一个展示更新购物车属性的旧示例,数据是一个字符串,即)"attributes [fizz] ='buzz'(请注意,因此您可以存储JSON):

Here is an old example showing off updating the cart attributes, data is a string ie) "attributes[fizz]='buzz' (and note that you can thus store JSON):

updateCartAttributes: function(data, callback) {
  var params = {
    type: 'POST',
    url: '/cart/update.js',
    data: data,
    dataType: 'json',
    success: function(cart) {
      if ((typeof callback) === 'function') {
        callback(cart);
      }
      else {
        Shopify.api.onCartUpdate(cart);
      }
    },
    error: function(XMLHttpRequest, textStatus) {
      Shopify.api.onError(XMLHttpRequest, textStatus);
    }
  }; 
  $.ajax(params);
}

这篇关于在Shopify中更新/删除购物车属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆