1

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?

enter image description here

Richy
  • 167
  • 3
  • 13

1 Answers1

0

I'm not entirely familiar with what you're doing but it seems you're resolving the promise multiple times, which probably only works the first time. You should instead try to use one promises for each request, gather those promises together in one "parent" promise and then return this one promise in getLatestProducts. This promise should then be resolved only when all child promises are resolved.

You could try out this, but I can' test this and I'm not familiar with JQuery Promises so I can't guarantee this will work. Hope it still helps you! :)

var getLatestProducts = function() {
  var dfd = new $.Deferred();
  var productIds = [];
  var products = [];

  for (var i in cart.contents) {
    productIds.push(cart.contents[i].id);
  }

  var promises = $.map(productIds, function(productId, idx) {
      return moltin.Product.Get(productId, function(product) {
          products.push(product);
      });
  }
  $.when.apply($, promises).then(function() {
      dfd.resolve(products);
  }
  return dfd.promise();
};

var promise = getLatestProducts();

promise.done(function(result) {
  var products = result;

  products.forEach(function(product) {
    // products should be an array of all products that were in the cart
    // now filter those you want (stock level etc.)
    // and do awesome stuff
  });
}
philmtd
  • 65
  • 1
  • 6
  • Yes I think you are right philmtd. How would I approach this? – Richy Aug 16 '16 at 20:03
  • I updated my answer. Hope this helps you. As I also said, I'm not familiar with the JQuery promise API so this might not work. It might give you an idea of what you need to do though :) – philmtd Aug 16 '16 at 20:21
  • Tidied up a few mistakes, and it seems to be working, however this isnt working: products.forEach(function(product) {}); – Richy Aug 16 '16 at 21:02
  • You should check in your debugger what data type products in that context is. You'll probably want it to be an array. However that part of my answer is not essential, you can also loop through this array with a for-loop or however you like or process the data in a totally other way. – philmtd Aug 16 '16 at 21:05
  • been looking at this for the past hour and i cant resolve it. The promise.done isnt showing an array with objects, its just showing objects – Richy Aug 16 '16 at 22:02
  • Are you debugging this thoroughly? You need to check what kind of objects it shows. Maybe the result is nested somewhere in a property. If it shows anything that means that something must have happened. I can't see your debugger, unfortunately, to help you more. – philmtd Aug 17 '16 at 08:09
  • I have added an image above for you to see my result in console log. I can remove it once this has been resolved :) – Richy Aug 17 '16 at 12:47
  • But this is an array. You could go e.g. with `for(var i = 0; i < result.length; i++) { var product = result[i]; ... }` – philmtd Aug 17 '16 at 14:29
  • I have tried this, however result.length returns 0. Dont understand it – Richy Aug 17 '16 at 14:34
  • think it may have something to do with this http://stackoverflow.com/questions/12206885/what-is-cleanest-way-to-turn-array-of-jquery-promises-into-a-jquery-promise-of-a – Richy Aug 17 '16 at 15:34
  • How can result.length return 0 when in the screenshot above I clearly see result.length to be 2? :S You're right, it might have something to do with that other question - I'm sure you're close to finding the solution! – philmtd Aug 19 '16 at 08:29