Shopify Functions problem with data in cart after logging in via New Customer Account

Shopify Functions problem with data in cart after logging in via New Customer Account

MaksymK
Shopify Partner
2 0 0

Hi,

the problem occurred when creating validation in shopify functions when logging into the user account (via New Customer Account). In one case the quantity value we get is a value before cart update and in another case the quantity value is after cart update. Steps to reproduce:


  • as a guest add item with qty 3 to the cart
  • Increase qty by 1 (in this moment when you update qty from 3 to 4 our validation function receive cart object as an input where item qty is already 4)
  • Login to the new customer account
  • Increase qty by 1 (in this moment when you update qty from 3 to 4 our validation function receive cart object as an input where item qty is still 3)

As you can see it’s not consistent and it does not allow us to implement our logic because we can not know which value before or update update will be returned to our function

 

Example:

in fragment

 

if (minCartQty !== null && quantity > minCartQty) {
message = `Minimum quantity is ${minCartQty}. New quantity is ${quantity}`;

if the product in the cart has 4 quantities, and the minimum is 4, when I try to reduce the quantity to 3 I get the message

"Minimum quantity is 4. New quantity is 3.

 

Which is correct. However, when we want to add a quantity check immediately after loading the page and add above the const erros, something like this

Reply 1 (1)

MaksymK
Shopify Partner
2 0 0
return {
errors: [{
localizedMessage: `Quantity is ${input.cart.lines[0].quantity},
target: "$.cart",
}];
};

 

so in the same case as above sometimes we get "Quantity is 3" and sometimes "Quantity is 4"

 

We noticed the incorrectly communicated quantity mainly after logging into the account. That is:

the user who is not logged in receives Quantity is 3, which is correct (because the new qty will be 3)

but when we already have the product in the cart and we log into the account, suddenly there is Quantity is 4 (i.e. it gives not the quantity that should be there, just what is currently in the cart)