From time to time, you might want to remove all the images that are associated with a Magento product. This might be for any number of reasons, but starting from a blank slate is often a good plan.
Here, we’ll have a look at a script that will remove all images from a product, and then add some new ones that we’ve got ready. We’re working on the assumption here that the client has had a number of new shots done for all products, so we’ll be running through all products in the system. We’re going to also assume that there is one image per product to be added and that it is named the same as the SKU of the product.
Removing product images
Firstly, we’ll grab all products in the system.
1 |
$collection = Mage::getModel('catalog/product')->getCollection(); |
If you have a lot of products in your store, you might want to filter this, so you’re only dealing with a limited, and managable product range.
To remove images from a product, the easiest way is via the Product Attribute Media API Model. So, we’re going to grab that model now.
1 |
$mediaApi = Mage::getModel("catalog/product_attribute_media_api"); |
This model has a remove
method which takes two variables as input. The product id, and the file you’re trying to remove (as remember, a product might have multiple images). We’re removing all images in our example, so we’re going to retrieve all images for the product and then loop through them removing each one, one at a time. If we wanted to only remove a particular image, or image tagged specifically (eg. the base image, swatch small images or thumbnail), then we could check within this loop.
1 |
$items = $mediaApi->items($product->entity_id); |
$items
will be an array of media attached to the product – meaning we can iterate through it and get each item. $item['file']
will be the filename of the asset, so we can call the catalog/product_attribute_media_api
model’s remove method.
Here’s the code in full.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
error_reporting(E_ALL); ini_set('memory_limit', '1024M'); require_once '../app/Mage.php'; umask(0); Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID); // load all products $collection = Mage::getModel('catalog/product')->getCollection(); $mediaApi = Mage::getModel("catalog/product_attribute_media_api"); foreach ($collection as $product) { echo $product->entity_id; try { $items = $mediaApi->items($product->entity_id); foreach($items as $item) { if($mediaApi->remove($product->entity_id, $item['file'])){ echo " image removed"; } else { echo $item['file'] . " image not removed"; } } } catch (Exception $exception){ echo $exception->getMessage(); die(); } echo "\n"; } |
I’ve added a quick bit of debugging in so that we can see line-by-line, product-by-product if the code is passing or failing. If you’d like to live in a world of make-believe and wonder, simply comment out or remove the “echo” statements.
Adding Images
Now that we’ve removed all the images, we’re going to add some new ones.
Preparing image files
Magento needs the images to be in a specific folder in order to import media into the system when you’re using dataflow or product import services within the admin system – namely /media/import/
When adding images programmatically, you don’t need to do this, but I’m going to follow best practice in my example and make other developer’s lives easier rather than having random folders of images laying around – so, I’m going to store them in here anyway.
Importing the images
As mentioned previously, we’re going to make some assumptions in our script:
– All new images to import is in /media/import/new-product-images-2014/
– There is only a single image per product
– The image has the same name as the SKU of the product
– The image should be set as the main image, small image & thumbnail for the product
Given the above information, there is actually only one key bit of information we need to add the product image into Magento. Once we have a $product
object, we can call the addImageToMediaGallery()
method on it to add an image from a path on the filesystem.
The addImageToMediaGallery()
method takes 4 attributes.
- $file – The file path of the image in the filesystem
- $mediaAttribute – Array of where the image should appear
- $move – Should the image be moved (if true) or copied
- $exclude – Mark the image as disabled in the product page view
1 2 |
$_productImage = "media/import/new-product-images-2014/mysku.jpg"; // our reference to the new image $_product->addImageToMediaGallery($_productImage, array( 'image', 'small_image', 'thumbnail' ), false, false); |
Image Credit: 0A77m2_DSC2695
Comment or tweet @douglasradburn