Line item properties disappearing in checkout

Hello, I have a Shopify plus store and I am adding line item properties in the checkout using the Cart Ajax API. This adds the properties to the correct line item. However, sometimes, when refreshing or navigating to the next page they disappear. This does seem random, as sometimes it works fine and then does not without any code changes.

The steps to reproduce are the following:

{"line":1,"properties":{"deliveryMethod":"HD"}}

This will sometimes work fine, I can see orders with the correct line item properties in the Shopify admin, however, it will randomly fail sometimes. The x-request-id for a request that has failed is 537575c0-7818-4c2f-b68f-bcc054909b38 if this can provide any info on why it has failed.

Two issues that I can see coming up for you:

  1. Even though it’s not documented very well, it’s an asynchronous response. Await it.

I use some code like this:

function queue(lineItem, properties){
    if(!islocked){
      islocked=true;
      changeProperties(lineItem, properties);
      
    }
    else{
      loops++;
      if(loops>=10){
        error = { "Error":"Could not update line item option properties"};
        unlock(lineItem, properties);
      }
      else{
        setTimeout(function() {
          queue(lineItem,properties);
        },2000);
      }
      
    }
  }

And the implementation of the code to push the change:

async function changeProperties(line, properties){
   
    let result;
    try{
      const body = JSON.stringify({
        line,
        properties
      });
      result = await fetch(`${routes.cart_change_url}`, {...fetchConfig(), ...{ body }}).then(() =>{
        unlock(line, properties);
      });
      return result;
    }
    catch(error){
      console.error(error);
    }

  }

A couple of small details (ie the variables that lock and set cart ui) are excluded. I use this when updating quantities, that impact other items in the cart.

  1. If you don’t pass all of the existing properties, it overwrites and will drop the others.