Skip to Content
author's profile photo Former Member
Former Member

How to delete orphan base product after assigning variants to different base product?

I want to delete the base product if the variant is attached to another base product and the original base product does not have any variants.

Product p1 has v1 and v2 as variants.
Now, v1 and v2 are attached to p2.
So, p1 has no variants now.
At this stage p1 does not has any variants at all, so p1 should be deleted.

I tried this with prepare interceptor on Variant. In this interceptor I checked if:


but here, it is giving false as the variant is still not removed (as the interceptor is called before removing the variant).

Now I am testing with:

 productModel.getVariants().size() == 1

and this condition is working fine.
But, the problem is that:modelservice.remove(baseProduct) is still not working.

As suggested, I can't remove the base product before removing the variants, so now I am removing the variants first.


Problem is it's still not working.

I know why it's not working. Actually, I am setting the variants attribute in base product as null, but that is having no effect at all in the DB because its the variant that stores the primary key of base product in the attribute baseProduct.
Which brings me to square one. So, I have two different questions now:

  1. How do I delete the base product if it's just become an orphan (original question)?

  2. Why do we have a setVariants method at all, if it does nothing ?

Now that I know that interceptors just won't work, I am trying some alternate solution. Possible solutions:

  1. Using the after save listener event.
    Disadvantage: Using the after save listener, each time the product is saved will cause very high load on the server!

  2. Creating a cronjob to remove all the orphan base products at the end of the day (just like Remove Empty Categories/Carts Cronjob).
    Disadvantage: It might interfere with an otherwise normal product import process and delete the base products midway!

    Which of the lesser evil should I choose. I am planning to go with #2.

Add a comment
10|10000 characters needed characters exceeded

Related questions

1 Answer

  • author's profile photo Former Member
    Former Member
    Posted on Aug 09, 2016 at 04:35 PM

    Seeing as there is no event/interceptor triggered after removing the variant you will have to write a cleanup job.

    You cannot remove the Product before the Product Variant because the Product Variant has the base product defined as a mandatory field.

    Add a comment
    10|10000 characters needed characters exceeded

Before answering

You should only submit an answer when you are proposing a solution to the poster's problem. If you want the poster to clarify the question or provide more information, please leave a comment instead, requesting additional details. When answering, please include specifics, such as step-by-step instructions, context for the solution, and links to useful resources. Also, please make sure that you answer complies with our Rules of Engagement.
You must be Logged in to submit an answer.

Up to 10 attachments (including images) can be used with a maximum of 1.0 MB each and 10.5 MB total.