Creating Product Variants and reading it using .NET OrderCloud SDK in Sitecore OrderCloud

Hello everyone, in this blog we will see, how we can create product variant in Sitecore OrderCloud.

Let's understand what is product variant. I will explain you in terms of example. Consider we are having TShirt. TShirt can be of different color and size. Lets consider there are two colors(Blue and Green) and there are 3 sizes(small,medium and large). So for TShirt, if we calculate the variants for the TShirt, variants is calculated as 1*2*3 = 6 different variants.

Now let's concentrate on how we can create a product variant. Follow the below steps:

Step 1: Create a Spec(In the context of OrderCloud, a "spec" typically refers to a specification or a set of specifications related to a particular entity or feature within the platform).We will be creating Size Spec.Follow below screenshot for your reference.

Step 2: Create a Spec Option. Lets create two Spec option for size. Large and Small.Follow below screenshot for refernce.

Verify your Spec Options for the Spec by giving a Get call to Spec Option api. See below image for reference.

Step 3: Assign your Spec to a Product using Spec Product assignment API. We will be requiring SpecId of the Spec and Product ID for linking of both.Once success, verify it by calling get call to Spec Prduct Assignment API.Follow below screenshot for reference.

Step 4 : Generate Product Variant: Now we will generate variants for that product. Navigate to product section in left panel. First fetch all the product variant for the product.Refer below screenshot. We can see there are only 4 variant for that product based upon color.After generation the new variant, there should be 8 variants in count.

Lets do a post call to generate our variant based on Size. Also use Overrite option for generating proper variants. If you are already having variants define, then the count will mismatch if overrite option is not selected.

Now lets verify by fetching that Single Product to check the generated variants.

Now let's concentrate on reading this variant details using OrderCloud .NET SDK. This request will be from Seller perspective. The Seller can be a Marketplace Admin user or individual seller.

OrderCloudConfig.cs

       
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace OrderCloudConsoleApp
{
    internal class OrderCloudConfig
    {
        internal const string CLIENT_ID = "##############################";
        internal const string USER_NAME = "#############################";
        internal const string PASSWORD = "############################";
        internal const string API_URL = "###########################";
        internal const string AUTH_URL = "##########################";
    }
}

	   

Program.cs

       
using OrderCloud.SDK;
using OrderCloudConsoleApp;

var client = new OrderCloudClient(new OrderCloudClientConfig
{
    ClientId = OrderCloudConfig.CLIENT_ID,

    //This is required for password grant flow:
    Username = OrderCloudConfig.USER_NAME,
    Password = OrderCloudConfig.PASSWORD,

    Roles = new[] { ApiRole.ApiClientAdmin,ApiRole.ProductAdmin,ApiRole.FullAccess},
    ApiUrl = OrderCloudConfig.API_URL,
    AuthUrl = OrderCloudConfig.AUTH_URL,

});

try
{
    //Get single  Product from Admin Perspective
    var productvariantlist = await client.Products.ListVariantsAsync("Deskproduct1");
    Console.WriteLine(productvariantlist.Meta.TotalCount.ToString());
    foreach(var variant in productvariantlist.Items)
    {
        Console.WriteLine(variant.ID);
    }
    Console.Read();
}
catch(Exception ex)
{
    Console.WriteLine(ex.ToString());
    Console.Read();
}


	

Final Output:

Thanks for reading!!. Happy learning.

You can check my other blogs too if interested. Blog Website

Comments

Popular posts from this blog

Sitecore XM Cloud Form Integration with Azure Function as Webhook

Automate RSS Feed to Sitecore XM Cloud: Logic App, Next.js API & Authoring API Integration

Create and Fetch Content From Sitecore Content Hub One using GraphQL and React