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
PHPStorm/IDEA, composer updates and 100% CPU
In one of our projects PHPStorm ran a while on 100% CPU, an investigation showed, that the process „checking for available composer updates“ ran and used all the resources. Most likely it got stuck somewhere. To get rid of the problem, one can turn off this feature. In IDEA Ultimate here: [Settings] -> Languages & […]
Custom Fields, dots and MySQL’s JSON_EXTRACT
We all know custom fields – hopefully – at least this is not about the basics. You can add custom fields to nearly all entites: orders, products, categories, … You can name them whatever you like, e.g. pluginname.property – but using dots . in your custom field name is a bad idea, because then you […]
gpg failed to sign the data
I was doing some work, when suddenly my git failed to commit changed. I dig a little around, asked StackOverflow but it didn’t help. Finally when running the commit manually and not through PHPStorm I got a proper error: Ah! The key is expired! Got it. Did you know you can extend a GPG key? […]