簡體   English   中英

顯示shopify api的響應說明(cart / add.js)

[英]Display response description from shopify api (cart/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

我需要為客戶顯示描述,這怎么可能?

這是我的代碼 -

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

你的代碼似乎有點兒麻煩。 可能的解決方案是,檢查狀態是否為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();
            }
        }  
    });

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM