0

I want yo use test variable in update.js but it shows error when I use as variable but when I pass this value directly it works can someone please tell me how can use dynamic variable to change quantity of existing products in cart

I have updated my code It will allow user to add only 5 items more than 5 items will be removed It will create string which will look like this 32082238341235:0,39470423048307:0,32164693278835:0,32164693835891:1

and finally the all IDs and qunatity will be updated by update.js

I have got error in last step its shows {"status":404,"message":"Cart Error","description":"Cannot find variant"}

when i try to update all products

jQuery.getJSON('/cart.js', function(cart) {
  var items_new = cart.items;
  var count = 0;
  count = cart.item_count;
  var item_to_remove = count - 5;

  if (count > 5) {
    var item_to_remove = count - 5;
    var combine = ""
    if (item_to_remove > 0) {
      for (var i = 0; i < items_new.length; i++) {
        if (count > 5) {
          var c_id = items_new[i].variant_id;
          var c_quantity = items_new[i].quantity;

          if (c_quantity >= item_to_remove) {


            var q = c_quantity - item_to_remove
            var data_multiple = c_id + ":" + q + ",";


            debugger;



            count = count - item_to_remove;
            console.log(data_multiple);
            var combine = combine + data_multiple;

          } else {

            var data_single = c_id + ":" + 0 + ",";

            count = count - c_quantity;
            item_to_remove = item_to_remove - c_quantity
            console.log(data_single)

            var combine = combine + data_single;
          }

        }

      }


      console.log(combine.slice(0, -1));
      var test = combine.slice(0, -1);

      console.log({
        updates: {
          test
        }
      });
      jQuery.post('/cart/update.js', {
        updates: {
          test
        }
      });





    }
    t._rerenderCart()

  }
  t.sidebarDrawer.open()
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

8
  • Where is this getJSON function being called from?
    – kiner_shah
    Commented Jan 4, 2022 at 10:48
  • @kiner_shah It is shopify's default It will get all cart data
    – Sunny
    Commented Jan 4, 2022 at 10:50
  • 1
    Looks like you're building up a string in the combine variable, which eventually ends up as the test variable. As a result, test is a string like "794864229:0,794864101:5". You'd need it to be an actual object instead. Also, doing { updates: { test } } is a shorthand for { updates: { test: "whatever the value of test is" } } and not the same as { updates: test }. (This is relevant since test isn't a valid variant_id.) As it is now, you'd get output like { updates: { test: "794864229:0,794864101:5" } }.
    – natancodes
    Commented Jan 4, 2022 at 10:57
  • Exactly as @natancodes pointed out, you should pass a proper object to updates, not a string.
    – kiner_shah
    Commented Jan 4, 2022 at 10:58
  • 1
    I'd approach it by changing the line var combine = "" to var combine = {}. Going from there, instead of constructing the strings like data_multiple, you could then assign properties to the object, like combine[c_id] = q.
    – natancodes
    Commented Jan 4, 2022 at 11:11

1 Answer 1

1

You need to store test as an object within parentheses {} and then pass to updates by using the spread ... operator.

var test = {39470423048307 : 0, 32164693278835 : 0, 32164693835891 : 1};
console.log({updates: {...test}});
jQuery.post('/cart/update.js', {updates:{...test}});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

In your updated code, you are passing a string to updates rather than an object. You should create an object from the data inside the for loop and pass that object to updates. See lines with // CHANGE HERE:

...
var combine = {};
if (item_to_remove > 0) {
  for (var i = 0; i < items_new.length; i++) {
    if (count > 5) {
      var c_id = items_new[i].variant_id;
      var c_quantity = items_new[i].quantity;

      if (c_quantity >= item_to_remove) {


        var q = c_quantity - item_to_remove;
        
        // CHANGE HERE
        combine[c_id] = q;
        
        count = count - item_to_remove;

      } else {
        // CHANGE HERE
        combine[c_id] = 0;

        count = count - c_quantity;
        item_to_remove = item_to_remove - c_quantity;
      }
      console.log(combine);
    }

  }

  // CHANGE HERE
  var test = combine;

  console.log({
    updates: test
  });
  jQuery.post('/cart/update.js', {
    updates: test
  });
...
10
  • Precisely so, although the spread operator isn't strictly necessary. i.e. { updates: test } should work just as well.
    – natancodes
    Commented Jan 4, 2022 at 10:23
  • @natancodes, ohh yes, you are right :-)
    – kiner_shah
    Commented Jan 4, 2022 at 10:24
  • @kiner_shah tried this It shows {"status":404,"message":"Cart Error","description":"Cannot find variant"} Error
    – Sunny
    Commented Jan 4, 2022 at 10:25
  • 1
    @natancodes, Spread operator can be useful though for example in case when updates should have some other data as well.
    – kiner_shah
    Commented Jan 4, 2022 at 10:26
  • @Sunny, is that URL proper? It works (proceeds further), so the problem is URL which you are trying to access.
    – kiner_shah
    Commented Jan 4, 2022 at 10:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.