Error 422: Add to Cart when item is cart and quantity is greater than available

New Member
3 0 21

Hey all!

 

I am currently building a theme with an Ajax approach when adding items to a cart, however, I keep getting an error 422 with the following description in the console:

 

POST https://localhost:3000/cart/add.js 422 (Unprocessable Entity)

[shop_events_listener] Error in handleXhrDone:  Cannot read property 'to_s' of undefined

I know this is happening because the item being added to the cart is greater than what's available in the inventory. Here's the case:

 

Item X is already in cart (quantity of 1)

Item X inventory quantity is set to 1

 

Here's my jQuery post method:

 

function postToCart() {
    let postCart = $.post({
      url: '/cart/add.js',
      dataType: 'json',
      data: $form.serialize(),
      success: function(data) {
      }
    })
      .done(function(data) {
        updateCartCount();
        console.log('done');
      })
      .fail(function(data) {
        document.querySelector('[data-submit-button]').disabled = true;
        document
          .querySelector('[data-submit-button]')
          .classList.add('cursor-not-allowed');
        document.querySelector('[data-submit-button-text]').innerText =
          theme.strings.unavailable;
      });
  }

 

Any ideas how to get rid of the error?

0 Likes