How to examine and change actions on commercetools updateCart query?

How to examine and change actions on commercetools updateCart query?


0

My question is this:

Is there a feature or method within graphql that allows editing of incoming queries EASILY so that I can prevent an action from being performed? I would like to examine my query in a web proxy, edit the actions, and then send it on to its destination.

Context:

I have a mutation running in a proxy service for commercetools that looks like this:

mutation AddLineItem(
  $id: String
  $version: Long!
  $sku: String
  $quantity: Long
  $currencyCode: Currency!
  $centAmount: Long!
  $custom: String!
) {
  updateCart(
    version: $version
    id: $id
    actions: [
      {
        addLineItem: {
          sku: $sku
          quantity: $quantity
          externalPrice: { centPrecision: { currencyCode: $currencyCode, centAmount: $centAmount } }
          custom: { typeKey: "custom-type", fields: [{ name: "field", value: $custom }] }
        }
      }
    ]
  ) {
    id
    version
    lineItems {
      ...LineItemFieldsCart
    }
  }
}

According to the commercetools documentation, when the external price field is set, duplicate items will be added as separate line items to the cart instead of increasing the quantity, as is the default behavior. The thing is, I want the default behavior instead of the duplicate line items. The normal way to mitigate this is to add an update extension to delete the duplicates, but I am working on an already mature system that has multiple update extensions that all operate on the line items, making a delete apparatus a heavy lift in the update extension. I want to edit the query in the proxy so the duplicate item never happens in the first place. Is there an easy way to do this?

2 Answers
2


0

for the add line item update action the current behaviour is to create a separate line item when external prices are used and not add the additional quantity to the already existing line item as you described.
If you want to make sure that the quantity is added to the already existing line item when using external prices, you could do a check prior to adding a new line item and use the update action change line item quantity in case the product variants match with the line items that already exist in the cart. This will let you set the external price there and will not add any additional like items. https://docs.commercetools.com/api/projects/carts#change-lineitem-quantity

1

  • Thankyou for your answer. That is essentially what I am trying to do, but the problem is that I have to rebuild the query that is sent to commercetools and it doesn't seem there is an easy way to extract the actions from the context so that I can edit them. That is where I am running into an issue.

    – sakurashinken

    May 5, 2022 at 20:30


0

as per the CT document :
ExternalPrice:
All update actions that change the quantity of a Line Item with this price mode require the externalPrice field to be given.

New contributor

user3375088 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.



Leave a Reply

Your email address will not be published. Required fields are marked *