Skip to content

Commit 202e0aa

Browse files
committed
Add information on contexual pricing
1 parent 6b7079a commit 202e0aa

File tree

1 file changed

+68
-0
lines changed

1 file changed

+68
-0
lines changed

README.md

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1063,3 +1063,71 @@ return [
10631063
],
10641064
];
10651065
```
1066+
1067+
### Real-Time Pricing Queries
1068+
1069+
Shopify does not emit webhooks for changes in [price catalogs for different markets](https://shopify.dev/docs/apps/build/markets/catalogs-different-markets#considerations). If you use these features, pricing stored in locally-synchronized product elements may become stale.
1070+
1071+
To fetch current prices directly from Shopify's API in your Twig templates, use the GraphQL client with a query that includes [contextual pricing](https://shopify.dev/docs/api/admin-graphql/latest/objects/ProductVariantContextualPricing):
1072+
1073+
```twig
1074+
{# Define the GraphQL query with contextual pricing for a specific country #}
1075+
{% set priceQuery %}
1076+
query getProductPrice($id: ID!) {
1077+
product(id: $id) {
1078+
variants(first: 100) {
1079+
nodes {
1080+
id
1081+
title
1082+
price
1083+
compareAtPrice
1084+
gbPricing: contextualPricing(context: {country: GB}) {
1085+
price {
1086+
amount
1087+
currencyCode
1088+
}
1089+
compareAtPrice {
1090+
amount
1091+
currencyCode
1092+
}
1093+
}
1094+
}
1095+
}
1096+
}
1097+
}
1098+
{% endset %}
1099+
1100+
{# Execute the query #}
1101+
{% set response = craft.shopify.api.query(priceQuery, {
1102+
id: product.shopifyId
1103+
}) %}
1104+
1105+
{# Access the pricing data #}
1106+
{% if response %}
1107+
{% for variant in response.variants.nodes %}
1108+
{% set pricing = variant.gbPricing %}
1109+
{% if pricing and pricing.price %}
1110+
{{ pricing.price.amount|currency(pricing.price.currencyCode) }}
1111+
{% else %}
1112+
{{ variant.price|currency }}
1113+
{% endif %}
1114+
{% endfor %}
1115+
{% endif %}
1116+
```
1117+
1118+
Key elements of this approach:
1119+
1120+
- `contextualPricing(context: {country: XX})` returns market-specific prices (use the country code directly, e.g., `GB`, `US`, `DE`)
1121+
- Use an alias like `gbPricing:` to name the result for easy access in Twig
1122+
- Pass the product's `shopifyId` (already a GID) directly to the query
1123+
- The `query()` method returns the first result directly, so access `response.variants` (not `response.data.product.variants`)
1124+
- Falls back to the default `variant.price` if contextual pricing is not available
1125+
1126+
> [!WARNING]
1127+
> Real-time API calls add latency to page rendering and count against [rate limits](#rate-limits). If some staleness is acceptable, wrap the query in a [`{% cache %}` tag](https://craftcms.com/docs/5.x/reference/twig/tags.html#cache):
1128+
>
1129+
> ```twig
1130+
> {% cache using key "pricing:#{product.id}:GB" for 5 minutes %}
1131+
> {# pricing query here #}
1132+
> {% endcache %}
1133+
> ```

0 commit comments

Comments
 (0)