【问题标题】:Shopify cart update not working with axiosShopify 购物车更新不适用于 axios
【发布时间】:2021-03-25 19:52:46
【问题描述】:

我正在尝试从我的购物车中永久删除产品项目,但我创建的功能仅从 DOM 中删除数据,并且在浏览器重新刷新时重新出现。这是我到目前为止所做的。

cart.liquid 文件中带有 JS 点击功能的 HTML

<a @click="removeFromCart(item)" class="product-remove">remove</a>

我的 CartForm.js 文件中的 removeFromCart 函数

      removeFromCart(item){
        let productInfo = this.cart.items.reduce(
          (accumulator, target) => ({ ...accumulator, [target.variant_id]: target.quantity}),
        {});

        const myJSON = JSON.stringify(productInfo);

        axios.post('/cart/update.js', { updates: productInfo })
        .then((res) => {
          this.cart.items.splice(this.cart.items.indexOf(productInfo), 1);
        })
        .catch((error) => {
          new Noty({
            type: 'error',
            timeout: 3000,
            layout: 'topRight',
            text: 'Cart not updated'
        }).show();
        })
      },

任何建议都会很棒。谢谢

【问题讨论】:

标签: javascript api vue.js axios shopify


【解决方案1】:

Shopify's Ajax API reference 可能有助于阐明为什么这对您不起作用。

如果您需要一个有效的 sn-p 代码来让事情动起来,这对我有用:

function removeCartItem(variantId) {
  let update = {};
  update[variantId] = quantity;

  axios({
    method: 'post',
    url: '/cart/update.js',
    data: { updates: update }
  })
  .then( (response) => {
    // The response should confirm the removal of the item from cart.items
   // Then you probably want to update your state so cart items gets re-rendered
  })
  .catch( (error) => {
    console.error('Error:', error);
  });
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-12-06
    • 1970-01-01
    • 1970-01-01
    • 2015-01-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-01
    相关资源
    最近更新 更多