简体   繁体   English

显示shopify api的响应说明(cart / add.js)

[英]Display response description from shopify api (cart/add.js)

I'm trying to show an alert every time customer is trying to add variant quantity that is bigger than available quantity. 每次客户尝试添加大于可用数量的变量时,我都会尝试显示警报。 When that's happens I see 422 response from add.js - 当发生这种情况时,我看到来自add.js的422回复 -

{status: 422, message: "Cart Error",…}
description: "All 1 Black Basic High Waisted Briefs - black / 1 are in your cart."
message: "Cart Error"
status: 422

I need to display the description for customers, how that is possible? 我需要为客户显示描述,这怎么可能?

Here is my code - 这是我的代码 -

 var shopifyAjaxAddURL = '/cart/add.js';
  var shopifyAjaxCartURL = '/cart.js';
  var shopifyAjaxStorePageURL = '/search';

  $(document).on('submit', 'form[action="/cart/add"]:not(.noAJAX, .feedback-go_to_cart)', function(e) {
    var $form = $(this);

    //Add to cart
    $.post(shopifyAjaxAddURL, $form.serialize(), function(itemData) {
      //Enable add button
      $btn.html(theme.icons.tick + ' ' + {{ 'products.product.added_to_cart' | t | json }});
      setTimeout(function(){

      //Not added, show message
      if(typeof(data) != 'undefined' && typeof(data.status) != 'undefined') {
        var jsonRes = $.parseJSON(data.responseText);
        window.showQuickPopup(jsonRes.description, $btn);
      } else {
        //Some unknown error? Disable ajax and submit the old-fashioned way.
        $form.addClass('noAJAX');
        $form.submit();
      }
    });

Your code seems bit buggy. 你的代码似乎有点儿麻烦。 Possible solution could be, check if the status is 422 and send the customer an alert message. 可能的解决方案是,检查状态是否为422并向客户发送警报消息。

if( itemData.status === 422 ) { alert('Quantity not available in the inventory') }

Full code might look like: 完整代码可能如下所示:

var shopifyAjaxAddURL = '/cart/add.js';
var shopifyAjaxCartURL = '/cart.js';
var shopifyAjaxStorePageURL = '/search';

$(document).on('submit', 'form[action="/cart/add"]:not(.noAJAX, .feedback-go_to_cart)', function (e) {
    var $form = $(this);

    //Add to cart
    $.post(shopifyAjaxAddURL, $form.serialize(), function (itemData) {
        if( itemData.status === 422 ) { alert('Quantity not available in the inventory') }
        else {
            //Enable add button
        $btn.html(theme.icons.tick + ' ' + {{ 'products.product.added_to_cart' | t | json }});
        setTimeout(function () {

            //Not added, show message
            if (typeof (data) != 'undefined' && typeof (data.status) != 'undefined') {
                var jsonRes = $.parseJSON(data.responseText);
                window.showQuickPopup(jsonRes.description, $btn);
            } else {
                //Some unknown error? Disable ajax and submit the old-fashioned way.
                $form.addClass('noAJAX');
                $form.submit();
            }
        }  
    });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM