尝试向 Shopify 购物车发出帖子请求

时间:2021-04-23 17:22:08

标签: jquery shopify shopify-api

第一次尝试这个。如果客户花费 200 美元或更多,我想将产品添加到客户的购物车。没有收到任何回应,想知道我做错了什么。此代码在 checkout.liquid

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
    {% assign found_title = false %}
    {% for item in checkout.line_items %}
      {% if item.variant.id == 6564672929928 %}
        {% assign found_title = true %}
      {% endif %}
    {% endfor %}

    {% if checkout.subtotal_price > 20000 and found_title == false %}
    <script>
      $( document ).ready(function () {
        variantId = 6564672929928
        jQuery.post('/cart/add.js', {
          items: [{
            quantity: 1,
            id: 6564672929928            
          }],
          function(res){
            console.log(res)
          }
        })
      })
    </script>

1 个答案:

答案 0 :(得分:0)

请尝试以下代码:

$(function() {
  var variantId = 6564672929928;
  $.post('/cart/add.js', {
      items: [{
        quantity: 1,
        id: variantId
      }]
    },
    function(res) {
      console.log(res)
    }
  });
});

在您的数据对象中,您未能关闭对象:

jQuery.post('/cart/add.js', {

这个被启动的对象从来没有被关闭过。