Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
JavaScript function to calculate the total stock value within a Shopify store

Get total stock value of all Shopify products + breakdown of inventory stock value for each product vendor

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.

Added feature

Found this script to calculate the total stock value of my inventory on my Shopify store, which was really useful since Shopify doesn't provide that info out of the box. I quickly realized that I also needed to know the breakdown of my inventory value for each vendor (supplier) and each product type that is on my store. I took the original script from freakdesign, unminified it, then added a few lines to display that info for each vendor.

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 {
                            var allproducts = a.products;
                            var vendors = new Object();
                            var productTypes = new Object();
                            console.log(a.consoleLine);
                            for (var d = allproducts.length - 1; 0 <= d; d--) {
                                a.variantCount += parseInt(allproducts[d].variants.length);
                                for (var f = allproducts[d].variants.length - 1; 0 <= f; f--) {
                                    var e = allproducts[d].variants[f];
                                    e.inventory_quantity && 0 < e.inventory_quantity && (a.stockCount += e.inventory_quantity, a.totalProductValue += parseFloat(e.price) * e.inventory_quantity)
                                    if (allproducts[d].vendor in vendors){
                                        vendors[allproducts[d].vendor]+= parseFloat(e.price) * e.inventory_quantity;
                                    } else {
                                        vendors[allproducts[d].vendor]= parseFloat(e.price) * e.inventory_quantity;
                                    }
                                    if (allproducts[d].product_type in productTypes){
                                        productTypes[allproducts[d].product_type]+= parseFloat(e.price) * e.inventory_quantity;
                                    } else {
                                        productTypes[allproducts[d].product_type]= parseFloat(e.price) * e.inventory_quantity;
                                    }
                                }
                            }
                            output = ["Products count: ", allproducts.length, "\nVariant count: ",
                                a.variantCount, "\nStock count: ", a.stockCount, "\nTotal value = ", Shopify.Money.format(a.totalProductValue.toFixed(3), Shopify.Money.moneyFormat())
                            ].join("");
                            console.log(output)
                            console.table(vendors)
                            console.table(productTypes)
                        }
                    })["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 {
var allproducts = a.products;
var vendors = new Object();
var productTypes = new Object();
console.log(a.consoleLine);
for (var d = allproducts.length - 1; 0 <= d; d--) {
a.variantCount += parseInt(allproducts[d].variants.length);
for (var f = allproducts[d].variants.length - 1; 0 <= f; f--) {
var e = allproducts[d].variants[f];
e.inventory_quantity && 0 < e.inventory_quantity && (a.stockCount += e.inventory_quantity, a.totalProductValue += parseFloat(e.price) * e.inventory_quantity)
if (allproducts[d].vendor in vendors){
vendors[allproducts[d].vendor]+= parseFloat(e.price) * e.inventory_quantity;
} else {
vendors[allproducts[d].vendor]= parseFloat(e.price) * e.inventory_quantity;
}
if (allproducts[d].product_type in productTypes){
productTypes[allproducts[d].product_type]+= parseFloat(e.price) * e.inventory_quantity;
} else {
productTypes[allproducts[d].product_type]= parseFloat(e.price) * e.inventory_quantity;
}
}
}
output = ["Products count: ", allproducts.length, "\nVariant count: ",
a.variantCount, "\nStock count: ", a.stockCount, "\nTotal value = ", Shopify.Money.format(a.totalProductValue.toFixed(3), Shopify.Money.moneyFormat())
].join("");
console.log(output)
console.table(vendors)
console.table(productTypes)
}
})["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