Sunil
Feb 28, 2021
  2388
(4 votes)

Delete Multiple Variants From Product & Catalog

In EpiServer sometimes you need to Delete Multiple Variants from a Catalog and Product.

We can do that by using CatalogSystem.

Here I am going to show you how we can remove the Variants from Product & Catalog.

We may have two things:

  • Delete Variants by Product
  • Delete all Variants under Catalog

To perform this we will pull all the catalog items first then we will look product nodes under it then variants and remove those.

Delete Variants by Products:

public void DeleteVariantsByProduct(ContentReference productContentReference)
        {
            var children = _contentRepository.GetChildren<CatalogContentBase>(productContentReference);

            foreach (var child in children)
            {
                if (child.GetType().BaseType == typeof(VariationContent))
                {
                    try
                    {
                        // Delete 
                        _catalogSystem.DeleteCatalogEntry(child.ContentLink.ID, true);
                    }
                    catch (Exception exception)
                    {
                        // Log Exception
                    }
                }
            }
        }

I created a Variant Service Class where you can find the two Methods for Delete Variants by Product and Delete all Variants under Catalog.

using EPiServer;
using EPiServer.Commerce.Catalog.ContentTypes;
using EPiServer.Core;
using Mediachase.Commerce.Catalog;
using System;

namespace DemoCommerce
{
    public class VariantService
    {
        private readonly ICatalogSystem _catalogSystem;
        private readonly IContentRepository _contentRepository;
        private readonly ReferenceConverter _referenceConverter;

        public VariantService(
            ICatalogSystem catalogSystem,
            IContentRepository contentRepository,
            ReferenceConverter referenceConverter)
        {
            _catalogSystem = catalogSystem ?? throw new ArgumentNullException(nameof(catalogSystem));
            _contentRepository = contentRepository ?? throw new ArgumentNullException(nameof(contentRepository));
            _referenceConverter = referenceConverter ?? throw new ArgumentNullException(nameof(referenceConverter));
        }

        public void DeleteVariantsByProduct(ContentReference productContentReference)
        {
            var children = _contentRepository.GetChildren<CatalogContentBase>(productContentReference);

            foreach (var child in children)
            {
                if (child.GetType().BaseType == typeof(VariationContent))
                {
                    try
                    {
                        // Delete 
                        _catalogSystem.DeleteCatalogEntry(child.ContentLink.ID, true);
                    }
                    catch (Exception exception)
                    {
                        // Log Exception
                    }
                }
            }
        }

        public void DeleteAllVariants()
        {
            // Get Catalog Node Items
            var catalogItems = _contentRepository.GetChildren<CatalogContent>(_referenceConverter.GetRootLink());

            foreach (var catalogItem in catalogItems)
            {
                //Loop through all catalog items
                var nodes = _contentRepository.GetChildren<CatalogContentBase>(catalogItem.ContentLink);

                foreach (var node in nodes)
                {
                    DeleteCatalogEntries(node);
                }
            }
        }

        private void DeleteCatalogEntries(CatalogContentBase node)
        {
            var children = _contentRepository.GetChildren<CatalogContentBase>(node.ContentLink);

            foreach (var child in children)
            {
                switch (child.ContentType)
                {
                    case CatalogContentType.CatalogNode:
                        DeleteCatalogEntries(child);
                        break;
                    case CatalogContentType.CatalogEntry:
                        if (child.GetType().BaseType == typeof(VariationContent))
                        {
                            try
                            {
                                // Delete 
                                _catalogSystem.DeleteCatalogEntry(child.ContentLink.ID, true);
                            }
                            catch (Exception exception)
                            {
                                // Log Exception
                            }
                        }

                        break;
                }
            }
        }
    }
}

Thanks & Regards

Sunil

Feb 28, 2021

Comments

Praful Jangid
Praful Jangid Mar 1, 2021 07:19 AM

Good start Sunil, Keep it up. Will read it and provide you my feedback on this.

Sunil
Sunil Mar 1, 2021 07:21 AM

Thank you Praful :)

Quan Mai
Quan Mai Mar 1, 2021 08:55 AM

You can use the content API all the way, which is the recommended way to do so. Mixing between content API and DTO Api is not recommended.

Also I don't think this will work

public void DeleteVariantsByProduct(ContentReference productContentReference)

{ var children = _contentRepository.GetChildren<CatalogContentBase>(productContentReference);

Sunil
Sunil Mar 25, 2021 02:45 PM

Thank you Quan for your time to view my post & sorry for the delay in reply.

I agree that DTO API's is not recommended over Content API's now I have updated the code.

Please let me know if any issue.

Please login to comment.
Latest blogs
Integrating Optimizely DAM with Your Website

This article is the second in a series about integrating Optimizely DAM with websites. It discusses how to install the necessary package and code t...

Andrew Markham | Sep 28, 2024 | Syndicated blog

Opticon 2024 - highlights

I went to Opticon in Stockholm and here are my brief highlights based on the demos, presentations and roadmaps  Optimizely CMS SaaS will start to...

Daniel Ovaska | Sep 27, 2024

Required fields support in Optimizely Graph

It's been possible to have "required" properties (value must be entered) in the CMS for a long time. The required metadata haven't been reflected i...

Jonas Bergqvist | Sep 25, 2024

How to write a bespoke notification management system

Websites can be the perfect vehicle for notifying customers of important information quickly, whether it’s the latest offer, an operational message...

Nicole Drath | Sep 25, 2024

Optimizely DAM – An Introduction

I presented a talk about the Optimizely DAM at the OMVP summit during Opticon 2024 in Sweden. I have now converted that talk into two blog posts....

Andrew Markham | Sep 25, 2024 | Syndicated blog

Simple and Effective Personalization with Optimizely Data Platform (ODP)

As we dive into the amazing capabilities of Optimizely One, let’s shine a spotlight on the Optimizely Data Platform (ODP). This simple tool unifies...

Alex Harris - Perficient | Sep 24, 2024 | Syndicated blog