How To Load And Retrieve A Variant or Product From Episerver Commerce

In today’s guide, we’re going to cover how to get products and variants from Episerver commerce to use in your website.

Getting A Product/Variant Using The Code

If you want to simply get a reference to a product and you know the catalog or product code, then you can use the ReferenceConverter.

var referenceConverter = ServiceLocator.Current.GetInstance<ReferenceConverter>();
var code = "variant-code"; 
var variantLink = referenceConverter.GetContentLink(code );
var repo= ServiceLocator.Current.GetInstance<IContentRepository>();
var variant= repo.Get<VariationContent>(variantLink );

Getting A Product/Variant Using The Id

If you want to simply get a reference to a product and you only know the Id, you can also do that. Note: You will also need to provide the workId/version Id.

var referenceConverter = ServiceLocator.Current.GetInstance<ReferenceConverter>();
var commerceId = 22;
var workId = 0;
var variantLink= referenceConverter.GetContentLink(commerceId , workId);
var repo= ServiceLocator.Current.GetInstance<IContentRepository>();
var variant= repo.Get<VariationContent>(variantLink );

Getting the entry Id from a content link

var contentReference = new ContentReference(4);
var referenceConverter = ServiceLocator.Current.GetInstance<ReferenceConverter>();
var entryId = referenceConverter.GetObjectId(contentReference);

Conclusion

In today’s guide, we’ve covered the different ways of retrieving products or variants from Episerver commerce via the Api. All products and variants have a unique code, sometimes known as a SKU, that is associated to it. If you know this you can find a product via the code.

Like Episerver CMS you can also use the Id to look up a product. In these instances you will also need to add the version/work id for the content you want. By default, if you use this it should work in the majority of situations.

Jon D Jones

Software Architect, Programmer and Technologist Jon Jones is founder and CEO of London-based tech firm Digital Prompt. He has been working in the field for nearly a decade, specializing in new technologies and technical solution research in the web business. A passionate blogger by heart , speaker & consultant from England.. always on the hunt for the next challenge

More Posts

1 reply

Trackbacks & Pingbacks

  1. […] and if you want to see some code examples of how it works I suggest you have a look at these posts How To Load And Retrieve A Variant or Product From EpiServer Commerce and How To Get a Catalogue in EpiServer […]

Leave a Reply

Want to join the discussion?
Feel free to contribute!

Leave a Reply

Your email address will not be published. Required fields are marked *