MODULES
...
eCommerce
Cart, Checkout, Shipping and O...
Checking Available Inventory in the Cart Layout
19min
use the s e cart inventory check function to check the stock levels of items in the cart and take appropriate action before checkout introduction to give the customer the best experience and avoid disappointment, you'll want to make them aware of any problems with their order as soon as possible using the s e cart inventory check function in the cart layout, you can check each row in the current cart to check that the required quantity is available items will remain in the cart, but a warning message will display the function will also check for any products which have been been disabled, expired or have a selected attribute which has been disabled these products will be automatically removed from the cart and a warning message will be displayed to explain this one way to use this function is as immediate feedback when the user loads the page or adjusts quantity in their cart alternatively, you can run the function on the "checkout" button press, to run a final check and redirect the user to the checkout if successful setting up the cart layout in order for the javascript to read the up to date quantities in the cart, you'll need to add the following attributes to your layouts step 1) add the cart id to each item in the item liquid file of your cart layout, add data s e cart id="{{this cart data cart id}}" to the highest level element step 2) check each item has a quantity input in the item liquid file of your cart layout, check that the \<input> element containing the up to date quantity for this row has the name attribute with the value quantity e g \<input name="quantity"> step 3) add the function see next section for options add the s e cart inventory check({}) function we'll document which event listeners and arguments can be used later in this article a common starting point would be to add the function to a button on a click event e g confirm and checkout step 4) set up an element to display a message when any cart items are no longer for sale if any products in the cart are no longer 'enabled' or have been deleted or if the product is enabled, but one of the selected attributes is not, we'll automatically remove these rows from the cart when the function is run adding the following data attribute to an element will display a message explaining this when it happens data s e cart has removed products meanwhile you can use the following liquid to fetch the same message this is useful if the page is refreshed after running the function and you wish to keep the message up {% if context session cart has removed products == true %} sorry, some products in your cart have been removed because they are no longer for sale {% endif %} you can clear the message from the session when you believe the user has had a chance to read it and it will no longer be relevant (in most cases, you'd display this straight after the liquid version of the message) we'll clear this automatically if the function is run again without removing any products {% session cart has removed products = null %} we'll explain how you can customize the message itself later in the function options how it works the function loops over every row in the cart, and checks the desired quantity against available inventory any disabled, expired or deleted products are removed from the cart so are any products whose selected attributes have been disabled or deleted an item callback function is then run to handle each remaining row of the cart, carrying out a different behaviour whether that item is in stock or not if every item in the cart is in stock, a success callback function will be run, which may for example optionally redirect the user to the checkout page handling products with different attributes as attributes do not currently have their own inventory tracking, the function will share out available inventory between all items in the cart with the same product id this means the quantities of each variation of the product in the cart will be added up and if the total is greater than the global inventory, all of the rows with this product id will return as "out of stock" note that where two rows in the cart list the same product with different attributes, the inventory will be shared between them s e cart inventory check options the s e cart inventory check function accepts a single argument containing a settings object for example s e cart inventory check({checkout url '/checkout' item cb my function name success cb undefined }); the table below details the available settings leave the setting undefined or remove the setting from the object if you desire to keep default behaviour setting key example optional checkout url '/checkout' yes item cb exampleitemfunctionname yes success cb examplesuccessfunctionname yes no longer available message 'sorry, some products in your cart have been removed because they are no longer for sale ' yes note the attribute data s e cart has removed products must be added to an element in the cart layout in order to display the message when needed checkout url passing a checkout url option to the function allows the function to redirect the page to the checkout if the inventory check is successful if you use a custom success function, this will be available in your parameters else, the default success function will use this to redirect the page default behaviour leaving checkout url undefined means the default success function will not redirect to the checkout item cb this option allows you to define a custom callback function which runs against each row in the cart, telling you whether the item is in stock or not the main function will loop over every row of items in the cart for each item, the default error function will be run available parameters number parameter purpose example 1st element a dom element referencing the element with the data attribute data s e cart id where not enough quantity is available you can target elements inside this when adding and removing error messages \<tr data s e cart id="{{this cart data cart id}}">\</tr> 2nd in stock a boolean telling you if this item is in stock or not if true, you may wish to remove previously added error messages if false, you should display the error message to the user true or false 3rd quantity an integer for the quantity desired by the customer 5 4th inventory an integer for the inventory available for this product 4 5th name the name of the product in this row "t shirt" default behaviour for all items the "max" attribute of the quantity \<input> will be adjusted to match the available inventory if the item in the row is out of stock the class s e cart out of stock is added to the row element with the data attribute data s e cart id the following html will be added as a sibling element to the quantity sorry we can't supply this many items of ${name} only ${inventory} in stock if the item in the row is in stock the class s e cart out of stock is removed from the row element with the data attribute data s e cart id previous error message elements with the class 's e cart inventory warning' in this row will be removed success cb the success cb function runs only if all cart rows contain a lower quantity than the available inventory, in other words, it runs if all items remaining in the cart are in stock available parameters number parameter purpose example 1st checkout url a string containing the value of checkout url originally passed as an option to the main function can be used to redirect to the cart, if available '/checkout' default behaviour any s e cart out of stock classes will be removed from all cart rows previous error message elements with the class 's e cart inventory warning' in all rows will be removed if a value for checkout url is available, the page will be redirected to the checkout otherwise, it will not refresh the page using event listeners with s e cart inventory check on page load this can be useful so that as soon as the user arrives at the cart they can be updated on whether items are in stock in this example, we don't want to refresh the page on success because the user has just arrived on the cart page and will need time to review example to be added to the wrapper liquid file \<script> document addeventlistener('domcontentloaded', function() { s e cart inventory check({}) }); \</script> on change this is useful if you want the inventory checked immediately after the user changes the desired quantity in the example we want to time this function straight after the updated quantity is saved to the cart instead of running the function directly then, we'll set the check inventory after option against the s e cart update quantity function example to be added to the item liquid file \<input type="number" id="quantity" name="quantity" min="1" max="{{this inventory quantity | default 1}}" value="{{this cart data quantity}}" onchange=" s e cart update quantity(event target value,{{this cart data cart id}} '{{context authenticity token}}' {confirm immeditately true check inventory after true }); "> on click this is useful if the customer has clicked the "checkout" button and you wish to run a final inventory check first here we make use of the checkout url option and the default success function example to be added to the wrapper liquid file \<button class="btn btn success" onclick="s e cart inventory check({checkout url '/checkout'})"> confirm and checkout \</button>