Insights, perspectives, and stories on software, business, and innovation.
Shopify has recently done one of it's trademark non backward-compatible deprecations. It's literally caused quite a few of our integrations to have a few hiccups!. productVariantUpdate ..... kaput!
We use this frequently to update/input barcode, SKU, RRP price and sale price... it's a necessary function for our majority fashion-related clients.
The update now needs to use productVariantsBulkUpdate instead... but this needs a few changes to the input payload.
This post provides a technical walkthrough, using PHP in a procedural function style, to demonstrate how to use the productVariantsBulkUpdate GraphQL mutation to set some of those pesky variant fields!.
I'm going to presume for now you have the Shopify GraphQL delivery methods already to go in your application (using cURL and pre-saved Shopify credentials and access tokens etc).
/**
* Executes a GraphQL mutation to set the prices for some/all of the variations for a Shopify product .
*
* @param string $variants An array of the variations you want to update on this call.
* Each variation structured as its own associative array as follows;
* [
* "id" => "xxx", // either bigint/int64 numeric or in Shopify gid form; gid://Shopify/ProductVariant/xxxxxx
* "price" => 999, // this is the float/decimal RRP price
* "compareAtPrice" => 999 //this is the float/decimal price is you want the item to be on sale. Set to 0 variant at full RRP.
* ]
* @param string $productid, This is the ID of the variant's parent product. Either bigint/int64 numeric or in Shopify gid form; gid://Shopify/Product/xxxxxx
* @return array|bool The decoded response array on success, or false on cURL error.
*/
function update_shopify_prices(
$variants = [],
$productid
) {
###### 1. Define the GraphQL Mutation #######
$query = '
mutation productVariantsBulkUpdate($productId: ID!, $variants: [ProductVariantsBulkInput!]!) {
productVariantsBulkUpdate(productId: $productId, variants: $variants) {
product {
id
}
productVariants {
id
sku
barcode
price
compareAtPrice
}
userErrors {
field
message
}
}
}
';
###### 2. Define the Mutation Variables ######
//first setup the GraphQL variables structure
$variables = [
'productId'=>'gid://Shopify/Product/' . basename($productid),
'variants' => []
];
//now loop round the incoming variants and update $variables
//we use the basename native function and then prepend of a gid to ensure the incoming values result in a gid format;
foreach($variants as $variant){
$variables['variants'][]=[
'id' => 'gid://Shopify/ProductVariant/' . basename($variant['id']),
'price' => number_format((float)$variant['price'],2),
'compareAtPrice' => number_format((float)$variant['compareAtPrice'],2)
];
}
###### 3. Construct the API Request Payload ######
$payload = json_encode([
'query' => $query,
'variables' => $variables
]);
###### 4. Post payload to Shopify ######
//{....}
}
As you can see, the key difference here is the requirement to specify the parent product. Nevertheless , it's a powerful function to do individual product pricing updates. Doing it en-masse however... completely different kettle of fish.... that needs the full bulk file update regime that Shopify offers.... one for a future post!
Dev Partners Ltd can help you leverage the GraphQL API to build robust, scalable integrations for your Shopify, PIM (Product Information Management), and ERP (Enterprise Resource Planning) projects. Master your data flow and streamline your operations. Contact us now!
Published“After ongoing issues and delays with our previous developers, we brought in Rob & Jason at Dev Partners to take over all our websites and bespoke software. What a breath of fresh air - they were fully focused, with excellent pre-launch designs and clear communication throughout. Highly recommended to anyone.”
If clunky systems are slowing you down, let’s talk. No sales patter. No corporate waffle. Just a straightforward conversation about fixing the stuff that wastes your time.
Two normal blokes who happen to be good at untangling messy systems. We’ll talk to you like humans, not consultants, and help you find the simplest way forward.
We’ll reply within one working day. No spam. No waffle.