What's your biggest current challenge? Have your say in Community Polls along the right column.
Our Partner & Developer boards on the community are moving to a brand new home: the .dev community forums! While you can still access past discussions here, for all your future app and storefront building questions, head over to the new forums.

Re: Get price of product in cart line

Solved

Get price of product in cart line

fwallace
Shopify Partner
45 1 4

There seems to be no way to get the price of a product variant in a cart line.

 

I can construct the run.graphql like this:

 

query RunInput {
  cart {
    lines {
      quantity
      
      merchandise {
        __typename
        ...on ProductVariant {
            id
            product {
              two_for_tag: hasAnyTag(tags: ["2FOR25"])
            }
        }
      }
    }
  }
}

But there is no way to get the price?

 

Shopify what the heck? How in the WORLD can I not get the price of a product variant if I want specialized discounts? This functionality is missing unless I miss my guess.

 

For example, if I want to a 2 for 25 discount on all product variants with that tag, I want to discount pairs of those product variants down to 12.50, and the product variant prices could be all sorts of prices. I sure as heck don't want to change them to some uniform value, that's dumb (margins are not the same across products). And I want to incentivize customers to purchase  more to get the deal just like Supermarkets.

 

Note: I CAN get the price of a product in Shopify Scripts. 

 

How can I achieve this discount without using Scripts?

 

ProductVariant has only according to docs:

 

https://shopify.dev/docs/api/functions/reference/product-discounts/graphql/common-objects/productvar...

 

id, metafield, sku, weight, weight unit, title, requires_shipping, but NOT price.

Accepted Solution (1)
fwallace
Shopify Partner
45 1 4

This is an accepted solution.

Thanks, but that is not available in the Shopify Function run.graphql.

 

I have found this works:

 

query RunInput {
  cart {
    lines {
      id
      quantity
      cost {
        amountPerQuantity {
          amount
          currencyCode
        }
        subtotalAmount {
          amount
          currencyCode
        }
      }
      
      merchandise {
        __typename
        ...on ProductVariant {
            id
            product {
              two_for_tag: hasAnyTag(tags: ["2FOR25"])
            }
        }
      }
    }
  }
}

 

Looking at my logs, I get this:

 

Input(Stdin):

{
  "cart": {
    "lines": [
      {
        "id": "gid://shopify/CartLine/0",
        "quantity": 2,
        "cost": {
          "amountPerQuantity": {
            "amount": "67.99",
            "currencyCode": "USD"
          },
          "subtotalAmount": {
            "amount": "135.98",
            "currencyCode": "USD"
          }
        },
        "merchandise": {
          "__typename": "ProductVariant",
          "id": "gid://shopify/ProductVariant/43012684841138",
          "product": {
            "two_for_tag": true
          }
        }
      }
    ]
  }
}

 

So amountPerQuantity { amount } is what I needed.

 

So this works.

View solution in original post

Replies 2 (2)

DaveedValencia
Shopify Partner
43 7 7

I use the GraphQL endpoint but not for functions. Is there a reason something like this wouldn't work in your case? (Cart id will need to be dynamic "1" is just a place holder for reference). You can add more fields as well.

query runinput {
  cart(id: 1) {
    lines {
      nodes {
        merchandise {
          ... on ProductVariant {
            id
            price {
              amount
            }
            compareAtPrice {
              amount
            }
            title
          }
        }
        quantity
      }
    }
  }
}
fwallace
Shopify Partner
45 1 4

This is an accepted solution.

Thanks, but that is not available in the Shopify Function run.graphql.

 

I have found this works:

 

query RunInput {
  cart {
    lines {
      id
      quantity
      cost {
        amountPerQuantity {
          amount
          currencyCode
        }
        subtotalAmount {
          amount
          currencyCode
        }
      }
      
      merchandise {
        __typename
        ...on ProductVariant {
            id
            product {
              two_for_tag: hasAnyTag(tags: ["2FOR25"])
            }
        }
      }
    }
  }
}

 

Looking at my logs, I get this:

 

Input(Stdin):

{
  "cart": {
    "lines": [
      {
        "id": "gid://shopify/CartLine/0",
        "quantity": 2,
        "cost": {
          "amountPerQuantity": {
            "amount": "67.99",
            "currencyCode": "USD"
          },
          "subtotalAmount": {
            "amount": "135.98",
            "currencyCode": "USD"
          }
        },
        "merchandise": {
          "__typename": "ProductVariant",
          "id": "gid://shopify/ProductVariant/43012684841138",
          "product": {
            "two_for_tag": true
          }
        }
      }
    ]
  }
}

 

So amountPerQuantity { amount } is what I needed.

 

So this works.