Spent all day trying to figure this but getting nowhere fast.
Basically I need to loop through an array of cart items and pull out the product ID'S. Once I have done that, I can then use the ID's to search through an array of products on my server, and call the correct ones.
Once I am able to locate the desired products on my server, I want to find the products which have ZERO stock.
This is what I have so far:
var getLatestProducts = function () {
var dfd = new $.Deferred(),
productId;
// loop through array (json)
for (var i in cart.contents) {
productId = cart.contents[i].id;
console.log(productId);
// SDK to get desired products from server
moltin.Product.Get(productId, function(product) {
dfd.resolve(product.stock_level);
});
}
return dfd.promise();
};
var promise = getLatestProducts();
promise.done(function(result) {
var stockLevel = result;
if (stockLevel > 0) {
console.log('go to checkout');
} else {
console.log('show alert to stop going through to checkout');
}
});
This is the result I am getting in my console. How do I loop through the 2 objects?