Skip to content

Product Variants

Joshua Harms edited this page Jan 21, 2025 · 2 revisions

Product Variants

Warning

All public Shopify apps (i.e. those published on the Shopify App Store) are required to switch over to using the GraphQL product API by February 1st. All custom apps are required to switch by April 1st. Go here for a migration guide on using ShopifySharp's GraphService to query and mutate Shopify's GraphQL API.

A product variant is a different version of a product, such as differing sizes or differing colors. Without product variants, you would have to treat the small, medium and large versions of a t-shirt as three separate products; product variants let you treat the small, medium and large versions of a t-shirt as variations of the same product.

Creating a Product with a variant in one go

 var product = new Product()
 {
     Title = "Test Product Walter",
     Vendor = "Burton",
     BodyHtml = "<strong>Good snowboard!</strong>",
     ProductType = "Snowboard",
     Images = images,
     //Make sure to give your product the correct variant option
     Options = new List<ProductOption>
     {
         new ProductOption
         {
             Name = "Color"
         }
     },
     //And then create a collection of variants or assign the "Variants" property
     //to an already defined collection.
     Variants = new List<ProductVariant>
     {
         new ProductVariant
         {
             Option1 = "Black",
         },
         new ProductVariant
         {
             Option1 = "Green",
         },
     }
 };

Creating a Product Variant

var service = new ProductVariantService(myShopifyUrl, shopAccessToken);
var variant = await service.CreateAsync(productId, new ProductVariant()
{
    Option1 = "blue",
    Price = 123.45,
});

Getting a Product Variant

var service = new ProductVariantService(myShopifyUrl, shopAccessToken);
var variant = await service.GetAsync(variantId);

Updating a Product Variant

var service = new ProductVariantService(myShopifyUrl, shopAccessToken);
var variant = await service.UpdateAsync(variantId, new Variant()
{
    Price = 543.21
});

Listing Product Variants

var service = new ProductVariantService(myShopifyUrl, shopAccessToken);
var variants = await service.ListAsync(productId);

Counting Product Variants

var service = new ProductVariantService(myShopifyUrl, shopAccessToken);
var count = await service.CountAsync(productId);

Deleting a Product Variant

var service = new ProductVariantService(myShopifyUrl, shopAccessToken);

await service.DeleteAsync(productId, variantId);