Shopify: Update Variant prices for markets/countries
Fabian Blechschmidt
We want to update prices for a Variant for a single market, but my research says, that neither Shopify itself can do this via CSV import, nor the awesome Matrixify App we use. But the Matrixify support says it should work. I couldn’t make it work, but give it a try!
So I dig into the backend, were it is possible and found priceListFixedPricesUpdate on GraphQL API – but be careful, to this date it is only available on the unstable API.
This is my working implementation, with a prices.csv file of the format:
VARIANT_ID<Tab>price
78907612318191 115
78912359550959 115
And this is the PHP code:
<?php
declare(strict_types=1);
require __DIR__ . '/../vendor/autoload.php';
use Shopify\Auth\FileSessionStorage;
use Shopify\Context;
Context::initialize(
'API KEY',
'API_TOKEN - NOT SECRET!'
'write_products, read_products',
'https://yourstore.myshopify.com/',
new FileSessionStorage('/tmp/php_sessions'),
'unstable',
true,
true,
);
$client = new Shopify\Clients\Graphql(
'yourstore.myshopify.com',
'API_TOKEN - NOT SECRET!'
);
$query
= /** @lang GraphQL */
<<<'QUERY'
mutation MarketPriceListByVariantUpdate(
$priceListId: ID!
$pricesToAdd: [PriceListPriceInput!]!
$pricesToDeleteByIDs: [ID!]!
) {
priceListFixedPricesUpdate(
pricesToAdd: $pricesToAdd
priceListId: $priceListId
variantIdsToDelete: $pricesToDeleteByIDs
) {
userErrors {
code
message
__typename
}
__typename
}
}
QUERY;
$pricesToAdd = [];
$allVariants = file('prices.csv');
foreach (array_chunk($allVariants, 200) as $variants) {
$pricesToAdd = [];
foreach ($variants as $line) {
$price = explode("\t", $line);
$pricesToAdd[] = [
'price' => [
'amount' => trim($price[1]) . ".00",
'currencyCode' => "EUR",
],
'variantId' => "gid://shopify/ProductVariant/" . $price[0],
];
}
$variables = [
'priceListId' => "gid://shopify/PriceList/19953877231",
'pricesToAdd' => $pricesToAdd,
'pricesToDeleteByIDs' => [],
];
$response = $client->query(["query" => $query, "variables" => $variables]);
echo $response->getBody() . "\n";
}
The code creates a Shopify client and the mutation (don’t ask me how this works, I learned GraphQL today.
And then we iterate over the prices and variants, build the variables for the query and fire it for each 200 variants – 250 are allowed.
Other articles from this category
Two Bitbucket Pipelines runners, one ID, and a git clone error for a directory that wasn’t there
A pipeline run failed with this: Fine, I thought, some leftover build directory, I’ll just rm -rf it and re-run. Except the directory wasn’t there. I checked. Twice. Git was very sure a non-empty directory existed at that exact path a few seconds ago, and the filesystem was equally sure it had never heard of […]
Monitoring our own product-export feeds with n8n and OhDear (and a bug I found while writing this)
Last post was about how a product export can get stuck forever after a queue worker gets OOM-killed mid-generation — is_running never resets, the export silently stops updating, and nothing in Shopware tells you. That post is exactly why this workflow exists. We run about 20 product-export feeds across several shops (Google Shopping, Idealo, Geizhals, […]
product-export:generate does nothing — until you turn off „Generate via scheduler“
We run several product exports (Google Shopping, a couple of price comparison feeds) with „Generate via scheduler“ checked in the Sales Channel settings. One night the queue worker died with an OOM kill — two big exports had become due in the same tick and the box didn’t have RAM for both at once. Fine, […]