Virtual Happy Hour this month, Jun 28, we'll be getting a sneak preview at our soon to launch SaaS CMS!

Try our conversational search powered by Generative AI!

Commerce Products Soft Delete

Vote:
 

Hi,

We have a shopping cart application build in MVC-EpiServer version 11.

We would like to delete some existing products from my Epi commerse system as part of syncing with our ERP system.

We need to soft delete (make not available in front end site) it first and then hard deleted in a later point of time.

In order to do the soft delete, we have set the property IsDeleted=true and Deteted=GetDate.

But even if we set the IsDeleted=true, and saved using contentRepository.Save method, the property is not updating to DB. Because if we fetch the same product again through CatalogManager GET, its status is showing as IsDeleted=false.

Could you please suggest if I missed anything?

Code Block

public static void UpdateDeletedProductStatus(this GenericProduct product, string entityCode)

        {

            product.IsDeleted = true;

            product.Deleted = DateTime.Now;

             //update segment url

            product.UpdateUrl(entityCode);

            var catalogManager = ServiceLocator.Current.GetInstance();

            catalogManager.UpdateDeletedProduct(product);

             var contentRepo = ServiceLocator.Current.GetInstance();

            var variant = product.GetVariants().Select(x => contentRepo.Get(x)).FirstOrDefault();

            if (variant != null)

            {

                catalogManager.UpdateVariantStatus(variant.CreateWritableClone());

            }

         }

       public void UpdateDeletedProduct(GenericProduct product)

        {

           contentRepository.Save(product, SaveAction.SkipValidation, AccessLevel.Edit); (Tried with Validation SaveAction.Publish as well)

        }

#195451
Edited, Jul 25, 2018 11:39
Vote:
 

How do you define IsDeleted  and Deleted? 

#195454
Jul 25, 2018 11:58
Vote:
 

Hi Quan,

Thank you for the response. You mean how to define these properties? I just want to set IsDeleted= true everytime.

Thanks,

Shinoj

#195456
Jul 25, 2018 13:18
Vote:
 

My bad, I thought IsDeleted and Deleted are custom properties. They are, however, system properties - Today I learned.

Now the tricky part. Unlike CMS content when you can delete a page and restore it later, the deleting part is actually moving a content to wastebasket, Catalog content has no waste basket concept. When you delete a content, it's gone for good and can't be restored.

For that reason, Catalog content implements IsDeleted and Deleted are required by the interfaces they implement, but those properties are not persisted anywhere.

So for your purpose, you can add two custom properties, IsSoftDeleted and DeletedTime to your product content, they would be saved correctly. 

#195457
Jul 25, 2018 13:45
Vote:
 

Hi Quan,

Thank you for the quick response.

If we add new properies IsSoftDeleted and DeletedTime, we need to make corresponding changes in front end site to exclude the deleted products from listing., right?

Could you please suggest if there any way for soft delete, so that it will not be available for listing/purchase but can reintroduce very quickly, if needed.

Thanks,

Shinoj k

#195463
Edited, Jul 25, 2018 14:06
Vote:
 

You can make IsSoftDeleted a boolean property and filter for that. Isn't that very quick and easy to do?

#195464
Jul 25, 2018 14:08
Vote:
 

Hi Quan,

Yes. As you suggested, let us try to add the new boolean property. I was just thinking about the areas where I have to incorporte this field, as the system fetch the active products from different jobs and tools.

Thanks,

Shinoj K

#195465
Edited, Jul 25, 2018 14:28
Vote:
 

Hi Quan,

We tried the same to add the new property 'IsSoftDeleted' and set it true. But still it seems like not updating to DB.

var allProductsList =contentRepository.GetDescendents(catalog.ContentLink).(....)

var selectedProduct = allProductsList.Where(x => x.ContentLink.ID == "someid").FirstOrDefault();

var writableProduct = selectedProduct.CreateWritableClone<GenericProduct>();

product.SetValue("IsSoftDeleted", true);

contentRepository.Save(product, SaveAction.SkipValidation, AccessLevel.Edit);

We tried with different parameters like SaveAction.ForceCurrentVersion, AccessLevel.Administer in Save method.

I am not getting any error. But when we fetch the same item again, the property is false only.

var updatedProduct = contentRepository.Get<GenericItem>(variantLink)

updatedProduct.IsSoftDeleted is false every time.

Did I missed any step which is required to reflect the change? Could you please give your suggetion?

Thanks,

Shinoj k

#195468
Jul 25, 2018 16:47
Vote:
 

I'm not sure what you are trying to do.

var writableProduct = selectedProduct.CreateWritableClone<GenericProduct>();

but then you update another content 

product.SetValue("IsSoftDeleted", true);

contentRepository.Save(product, SaveAction.SkipValidation, AccessLevel.Edit);

And then you load another content

var updatedProduct = contentRepository.Get<GenericItem>(variantLink)

Also you have to use SaveAction.Publish  or SaveAction.Save 

#195470
Jul 25, 2018 17:08
Vote:
 

Hi Quan,

Apologize for causing confusion. Actually I copied the codes from different libraries of my project. Thats why variable mismatch occured.

Could you please review the following code.

            //Get all products which is to be soft deleted.
            var allProductsList = contentRepository.GetDescendents(catalog.ContentLink);
            var selectedProduct = allProductsList.Where(x => x.ContentLink.ID == 51898).FirstOrDefault();

            //Clone writable product
            var writableProduct = selectedProduct.CreateWritableClone<GenericProduct>();
            writableProduct.SetValue("IsSoftDeleted", true);

            //Saving with IsSoftDeleted = true.
            contentRepository.Save(writableProduct, SaveAction.Publish, AccessLevel.Edit);

           //If we take all products again, the status of IsSoftDeleted is false only. (Even if restart the application)
            var allUpdatedProducts = contentRepository.GetDescendents(catalog.ContentLink);
            var updatedProduct = allUpdatedProducts.Where(x => x.ContentLink.ID == 51898).FirstOrDefault();
            bool isSoftDeleted = updatedProduct.IsSoftDeleted;

I really appreaciate your kind response.

Thanks in advance.

#195471
Jul 25, 2018 19:17
Vote:
 

Not sure why you have to call SetValue. Why not

writableProduct.IsSoftDeleted = true; 

?

Your code to load a specific product is not optimal, but that is something else

#195473
Jul 25, 2018 20:31
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.