Shopify: Update Variant prices for markets/countries

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.

Leave a Reply