Skip to content

Cart Transform function calculates line.cost.totalAmount.amount differently for custom line items #560

Open
@kylebuildsstuff

Description

@kylebuildsstuff

Packages

"@shopify/app": "^3.58.2",
"@shopify/cli": "3.66.1",
"@shopify/shopify_function": "0.0.3",

Describe the bug

When using the Cart Transform function to update the price of a line item it returns line.cost.totalAmount.amount as the price of the line item multiplied by the quantity for normal products but for custom line items it returns the value as just the price of one line item, disregarding the quantity.

While it may seem simple to just change the calculation to account for quantity too, this would fix one problem and create another, as normal products don't require you to account for quantity when using totalAmount.amount but custom line items do.

Steps to reproduce

  1. Create cart transform function to update line item.

Example code:

// run.ts

// 10%
let priceChangeValue = 10

const cartTotalAmount = cart?.lines?.reduce?.((total, line) => {
          return total + (Number(line?.cost?.totalAmount?.amount) || 0);
        }, 0);
        const newPrice = cartTotalAmount * (Number(priceChangeValue) / 100);

return {
          update: {
            cartLineId: lineId,
            price: {
              adjustment: {
                fixedPricePerUnit: {
                  amount: newPrice,
                },
              },
            },
          },
        };
// run.graphql
query RunInput {
  cart {
    lines {
      id
      quantity
      cost {
        totalAmount {
          amount
          currencyCode
        }
      }

      merchandise {
        ... on ProductVariant {
          id
        }
      }
    }
  }

  presentmentCurrencyRate
...
  1. Create draft order with the item in question along with a custom line item
  2. Adjust the quantity of the custom line item
  3. Create Add normal product to order/draft order
  4. Adjust quantity of normal product

Expected behavior

line.cost.totalAmount.amount should return the total amount of the line with the quantity already accounted for for custom line items, or if it's already working as intended then line.cost.totalAmount.amount should return the total amount of the line without the quantity account for for normal products.

Additional context

Video demo of issue:

https://youtu.be/5ejoPdP_akw

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions