Edit product data using the new product model
Learn how to use the new product model to edit the product, variant, and option data that you created.
What you'll learn
Anchor link to section titled "What you'll learn"In this guide, you'll learn how to update data for an existing product either by overwriting the data in a single mutation, or by updating its individual fields incrementally through multiple mutations.
You'll also learn the workflow for updating a product and its variants and operations using updated product operations.
You've created a new product with variants and options. Now you want to make the following changes:
Change the title from
My Cool Socks
toMy Extra Cool Socks
.Get rid of sizing, and switch to a one-size-fits-all model.
Add a new
Material
option with values ofCotton
andWool
, listing all existing socks asCotton
.Rename the
Material
option toFabric
.Swap
Color
andMaterial
’s positions to makeMaterial
options and variants display first in the product.Remove two color options.
Add two new colors of cotton socks.
Add five new colors of wool socks.
Shape of the product data
Anchor link to section titled "Shape of the product data"The following diagram represents the original shape of the product data:
The diagram displays the "OptionValues" that are assigned to each variant, and the "Values" that belong to each "Option" relationship of the product's variants to their option values. On the right, there's the relationship of the product's options to their option values. The values that are represented in "OptionValues" correspond with a value in the product's options.
The following is the corresponding data structure:
Several mutations to update the product data
Anchor link to section titled "Several mutations to update the product data"The following mutations should be used when Shopify is the system for managing product domain information. The mutations are designed for smaller changes per request. Consequently, the valid calls are easier to construct and their individual execution duration is shorter.
Update the product title
Anchor link to section titled "Update the product title"Update the product title with the productUpdate
mutation. This example updates the product title from My Cool Socks
to My Extra Cool Socks
:
The following diagram illustrates the product data after the update:
Remove the Size option
Anchor link to section titled "Remove the Size option"Because you're moving the product to a one-size-fits-all model, you can remove the Size
option.
Remove an option with the productOptionsDelete
mutation, passing the the ID of the Size
option. However, the product is already offered in multiple sizes, so you need to decide which variants no longer need to be offered.
You can delete unneeded variants in the following ways:
Delete the variants that you don’t want to preserve, until each product variant remaining represents a unique
Color
. Update the remaining variants to all have oneSize
. Then, delete theSize
option.You might want to delete variants manually if you need fine-grained control over which variants you want to delete, For example, to preserve variants with specific SKUs or prices to be preserved.
Use the
strategy
argument with a value ofPOSITION
. This strategy automatically resolves duplicate option combinations for you, by deleting any duplicated option combinations by position (descending).This behavior uses the same strategy that the Shopify admin uses to automatically delete any product variants that would be a duplicate of another variant after a product option is deleted. The
POSITION
strategy enables you to implement the same approach in your app with minimal effort.
The following example deletes the Size
option using strategy: POSITION
:
After the product option and its variants are deleted, the product has the following structure:
Add the Material option
Anchor link to section titled "Add the Material option"Add a new Material
option with the productOptionsCreate
mutation. When you run this mutation, all existing variants are backfilled to use the first specified option value for the new option. In this example, the option is Cotton
.
After the new product option is added, the product has the following structure:
Update the Material option
Anchor link to section titled "Update the Material option"You can also use the productOptionUpdate
mutation to rename a product option or reorder the options. This example renames Material
to Fabric
and changes its position to display first in the product:
After the option is renamed and the options are reordered, your product has the following structure:
Update variant option values by ID
Anchor link to section titled "Update variant option values by ID"Each time you query a product option's optionValues
, both Wool
and Cotton
are returned.
Because Cotton
was backfilled for existing variants, its hasVariants
value is true, where Wool
's hasVariants
value is false
. Option values with a hasVariants
value of false
are referred to as orphaned option values.
The following example uses the productVariantsBulkUpdate
mutation to update the Green
color sock to be Wool
. The option value is referenced by its id
.
In the response, Wool
’s hasVariants
value is now true
because the option is no longer orphaned.
After the Wool
option value is assigned to a variant, your product has the following structure:
Reorder the options
Anchor link to section titled "Reorder the options"Use the productOptionUpdate
mutation to set the new position of each individual option manually. However, if you only need to reorder options and option values, or need to reorder multiple options and option values, then you might want to use the productOptionsReorder
instead.
The following example switches the order of all of the options and option values, displays Color
first with a value order of Blue
, Red
, Green
, and then displays Fabric
with a value order of Wool
, Cotton
:
After the options and option values are reordered, your product has the following structure:
Remove colors
Anchor link to section titled "Remove colors"The following example uses the productVariantsBulkDelete
mutation to remove the variants that use the Red
and Green
color options:
The option values for which all variants were deleted are preserved as orphaned option values.
After the variants are removed, your product has the following structure:
Tip: Clean up unused option values
Anchor link to section titled "Tip: Clean up unused option values"You can preserve unused option values to reference by ID or name at a later point. Alternatively, you can remove the values from the option using the productOptionUpdate
mutation. The following is an example:
Add variants
Anchor link to section titled "Add variants"This example uses the productVariantsBulkCreate
mutation to add the following new variants:
- Cotton socks available in yellow and orange
- Wool socks of different colors
For the mix of existing and new variants, new colors are applied using a reference value or ID. Any new values are added as option values automatically.
After the new variants are added, your product has the following structure:
Learn more about how to migrate from GraphQL to REST with the following resources: