Clearing a Cloned Product Entity Image using a Pre-Create Plugin

Jared Johnson, 30 April 2020

When cloning a product using the Clone button, the new product that is created will also have the same entity image as the original product. What if you want to stop this from happening so that it has no entity image by default?

A good way to solve this would be a plugin, ideally on pre-create so you don’t have the extra update. However, while the approach of setting the entityimage field to null like the following clears the entity image on an update; in a pre-plugin, setting entityimage to null does not do anything.

Entity update = new Entity("product", target.Id); 
update["entityimage"] = null; 

This is because the entityimage field does not contain any data yet, instead at this point there will be 3 fields set: entityimageid, entityimage_timestamp and entityimage_url. The entityimage_url is constructed using the first 2 fields and will end up looking something like this:


This means that, instead, we should clear these 3 fields in our pre plugin and that will prevent the entity image from being set on the new product.

target["entityimageid"] = null;
target["entityimage_timestamp"] = null;
target["entityimage_url"] = null;