Shopify Liquid 和 Ajax:根据条件在购物车中添加和删除产品

Shopify Liquid and Ajax: Adding and Removing Products in Cart Based on Conditions

提问人:Ahmad 提问时间:11/16/2023 更新时间:11/16/2023 访问量:30

问:

我正在开发一个 Shopify 商店,并尝试使用 Liquid 和 Ajax 实现一项功能。我有一个脚本,当存在符合条件的产品时,将免费产品添加到购物车。此外,如果符合条件的产品已从购物车中删除,我想删除免费产品。

这是我正在使用的现有代码:

<script>
  
  window.onload = function(){
    let cartContainsFreeProduct = false;
    let cartContainsQualifyingProduct = false;
 
    const qualifyingProductVariantId = ***ENTER VARIANT ID OF PRODUCT THAT QUALIFIES FOR FREE PRODUCT***; 
    const freeProductVariantId = ***ENTER VARIANT ID OF THE FREE PRODUCT***;
           
    {% for item in cart.items %}
      if({{ item.id }} === freeProductVariantId){
        cartContainsFreeProduct = true; 
      }
      if({{ item.id }} === qualifyingProductVariantId){
         cartContainsQualifyingProduct = true;
      }
    {% endfor %}
    
//  If cart contains qualifying product and doesn't already contain free product, add qty 1 of free product
    if(cartContainsQualifyingProduct && cartContainsFreeProduct === false) {
        
      jQuery.post('/cart/add.json', { quantity: 1, id: freeProductVariantId })
      .done(function() {window.location.reload()})    
        
     } 
  }
  
</script>

添加免费产品的脚本似乎工作正常,但是从购物车中删除 qualifyingProductVariantId 时如何删除产品 freeProductVariantId

javascript jquery ajax shopify

评论


答: 暂无答案