Developer forum

Forum » Development » Is it possible to automatically deactivate masterproduct when all variants are inactive?

Is it possible to automatically deactivate masterproduct when all variants are inactive?

Hans Ravnsfjall
Hans Ravnsfjall
Reply

Hi

We have an ecom solution where we import products from daily. Products are imported as separate products, and then combined into variant groups. That´s all fine.

Proucts/Variants are enriched in PIM, but some of the data is updated via Import. Active status of variants is one of the thing that is updated with daily import.

But a problem occurs, when all variants of a product are deactivated through the Import. The problem is, that the masterproduct (container product) that is created when combining products to variantgroup, does not get deactivated - and therefore, it is shown in the productlist.

Is there a way we can automatically deactivate masterproduct  for products where all variants are inactive? Could this be done by eg. running a Scheduled SQL task? If so - Any info on how the SQL query should look like?

Or is there maybe a different way of doing this?

Thanks in advance 🙏

/Hans


Replies

 
Nuno Aguiar Dynamicweb Employee
Nuno Aguiar
Reply

Hi Hans,

 

You can use a ProductSave notification subscriber to do any updates like that. And it would be better than doing in SQL, because then it triggers any other DW events or customizations that may be associated with a product being updated.

 

Something like

 

namespace My.Namespace
{
    [Dynamicweb.Extensibility.Notifications.Subscribe(Ecommerce.Product.BeforeSave)]
    public class ProductBeforeSave : Dynamicweb.Extensibility.Notifications.NotificationSubscriber
    {
        public override void OnNotify(string notification, Dynamicweb.Extensibility.Notifications.NotificationArgs args)
        {
            if (!(args is Ecommerce.Product.BeforeSaveArgs productArgs) || !ExecutingContext.IsBackEnd())
            {
                return;
            }

            var product = productArgs.Product;
            // ADD CODE HERE - check if there are variants, and if so, if there isn't any active one and if so do product.Active = false;
        }
    }
}

 

Best Regards,

Nuno Aguiar

 

You must be logged in to post in the forum