Skip to content

Instantly share code, notes, and snippets.

@freakdesign
Last active November 19, 2020 10:44
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save freakdesign/c0555a0159842190c8550ce1a3935583 to your computer and use it in GitHub Desktop.
Save freakdesign/c0555a0159842190c8550ce1a3935583 to your computer and use it in GitHub Desktop.
JavaScript function to calculate the total stock value within a Shopify store

Get total stock value of all Shopify products

Refers to the freakdesign blog post.

Most people that need to know the total stock value are already likely to be using a third party Inventory Management System, ERP (or similar). If you want to know what your total stock value is you can grab that info via the API - let's look how.

Usage

Paste the code into the browser debug console.

/*!

  JavaScript function to calculate the total stock value within a Shopify store

  Copyright (c) 2016 Jason Bowman (freakdesign.com.au)
  Dual licensed under the MIT and GPL licenses:
  http://www.opensource.org/licenses/mit-license.php
  http://www.gnu.org/licenses/gpl.html

*/
(function(){if(self.fetch){var a={apiLimit:250,productCount:0,variantCount:0,stockCount:0,totalProductValue:0,consoleLine:"------------------------------"},h=function(b){if(!(1>a.productCount)){"undefined"===typeof b&&(b=1);var g=Math.ceil(a.productCount/a.apiLimit);console.log("Getting "+b+" of "+g+" page(s)...");var k=new Request("/admin/products.json?limit="+a.apiLimit+"&page="+b,{method:"GET",credentials:"same-origin",headers:new Headers({"Content-Type":"application/json"})});fetch(k).then(function(a){return a.json()}).then(function(c){1===
b||"undefined"===typeof a.products?a.products=c.products:(c=a.products.concat(c.products),a.products=c);if(g>b)b++,h(b);else{c=a.products;console.log(a.consoleLine);for(var d=c.length-1;0<=d;d--){a.variantCount+=parseInt(c[d].variants.length);for(var f=c[d].variants.length-1;0<=f;f--){var e=c[d].variants[f];e.inventory_quantity&&0<e.inventory_quantity&&(a.stockCount+=e.inventory_quantity,a.totalProductValue+=parseFloat(e.price)*e.inventory_quantity)}}c=["Products count: ",c.length,"\nVariant count: ",
a.variantCount,"\nStock count: ",a.stockCount,"\nTotal value = ",Shopify.Money.format(a.totalProductValue.toFixed(3),Shopify.Money.moneyFormat())].join("");console.log(c)}})["catch"](function(a){console.log("parsing failed",a)})}};(function(){var b=new Request("/admin/products/count.json",{method:"GET",credentials:"same-origin",headers:new Headers({"Content-Type":"application/json"})});fetch(b).then(function(a){return a.json()}).then(function(b){a.productCount=b.count||0;h()})["catch"](function(a){console.log("Function failed: ",
a)})})()}else console.log("fetch() not supported")})();
/*!
JavaScript function to calculate the total stock value within a Shopify store
Copyright (c) 2016 Jason Bowman (freakdesign.com.au)
Dual licensed under the MIT and GPL licenses:
http://www.opensource.org/licenses/mit-license.php
http://www.gnu.org/licenses/gpl.html
*/
(function(){if(self.fetch){var a={apiLimit:250,productCount:0,variantCount:0,stockCount:0,totalProductValue:0,consoleLine:"------------------------------"},h=function(b){if(!(1>a.productCount)){"undefined"===typeof b&&(b=1);var g=Math.ceil(a.productCount/a.apiLimit);console.log("Getting "+b+" of "+g+" page(s)...");var k=new Request("/admin/products.json?limit="+a.apiLimit+"&page="+b,{method:"GET",credentials:"same-origin",headers:new Headers({"Content-Type":"application/json"})});fetch(k).then(function(a){return a.json()}).then(function(c){1===
b||"undefined"===typeof a.products?a.products=c.products:(c=a.products.concat(c.products),a.products=c);if(g>b)b++,h(b);else{c=a.products;console.log(a.consoleLine);for(var d=c.length-1;0<=d;d--){a.variantCount+=parseInt(c[d].variants.length);for(var f=c[d].variants.length-1;0<=f;f--){var e=c[d].variants[f];e.inventory_quantity&&0<e.inventory_quantity&&(a.stockCount+=e.inventory_quantity,a.totalProductValue+=parseFloat(e.price)*e.inventory_quantity)}}c=["Products count: ",c.length,"\nVariant count: ",
a.variantCount,"\nStock count: ",a.stockCount,"\nTotal value = ",Shopify.Money.format(a.totalProductValue.toFixed(3),Shopify.Money.moneyFormat())].join("");console.log(c)}})["catch"](function(a){console.log("parsing failed",a)})}};(function(){var b=new Request("/admin/products/count.json",{method:"GET",credentials:"same-origin",headers:new Headers({"Content-Type":"application/json"})});fetch(b).then(function(a){return a.json()}).then(function(b){a.productCount=b.count||0;h()})["catch"](function(a){console.log("Function failed: ",
a)})})()}else console.log("fetch() not supported")})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment