-
-
Notifications
You must be signed in to change notification settings - Fork 313
Product Variants
Joshua Harms edited this page Jan 21, 2025
·
2 revisions
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.
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",
},
}
};
var service = new ProductVariantService(myShopifyUrl, shopAccessToken);
var variant = await service.CreateAsync(productId, new ProductVariant()
{
Option1 = "blue",
Price = 123.45,
});
var service = new ProductVariantService(myShopifyUrl, shopAccessToken);
var variant = await service.GetAsync(variantId);
var service = new ProductVariantService(myShopifyUrl, shopAccessToken);
var variant = await service.UpdateAsync(variantId, new Variant()
{
Price = 543.21
});
var service = new ProductVariantService(myShopifyUrl, shopAccessToken);
var variants = await service.ListAsync(productId);
var service = new ProductVariantService(myShopifyUrl, shopAccessToken);
var count = await service.CountAsync(productId);
var service = new ProductVariantService(myShopifyUrl, shopAccessToken);
await service.DeleteAsync(productId, variantId);