【问题标题】:Display response description from shopify api (cart/add.js)显示来自 shopify api (cart/add.js) 的响应描述
【发布时间】:2019-06-09 07:27:21
【问题描述】:

每当客户尝试添加大于可用数量的变体数量时,我都会尝试显示提醒。发生这种情况时,我会看到来自 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

我需要为客户显示描述,这怎么可能?

这是我的代码 -

 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();
      }
    });

【问题讨论】:

    标签: javascript jquery shopify


    【解决方案1】:

    您的代码似乎有点错误。可能的解决方案是,检查状态是否为 422 并向客户发送警报消息。

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

    完整代码可能如下所示:

    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();
                }
            }  
        });
    

    【讨论】:

      猜你喜欢
      • 2020-09-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-07-08
      • 1970-01-01
      • 2023-03-18
      • 1970-01-01
      相关资源
      最近更新 更多