I'm currently writing a module which in its essence applies a pre-defined set of images to a product media gallery. It basically loops through a delimited list of filenames and uses addImageToMediaGallery
to add the images to the product.
The code in the observer is:
$ourProduct = $observer->getEvent()->getProduct();
$importDir = Mage::getBaseDir('media') . DS . 'import';
$tID = $this->_getRequest()->getPost('template_id');
$productsData = Mage::helper("imageimport")->getMedia($tID);
$k=0;
foreach($productsData as $fileName){
$k++;
$filePath = trim($importDir. DS . $fileName);
if($fileName!="") {
if (file_exists($filePath)) {
$ourProduct->addImageToMediaGallery($filePath, array('image', 'small_image', 'thumbnail'), false, true);
$ourProduct->save();
Mage::log('Adding image '. $filePath .' to ' . $ourProduct->getSku(), null, 'imageimport.log');
} else {
Mage::log('File does NOT exist: ' . $filePath, null, 'imageimport.log');
echo "<br>";
}
}
}
The problem I have with this is that it just creates another image in the media/catalog folder. This site has a buttload of products so having this feature will create needless duplicates of the same image.
So the question I'm posing is - within the confines of addImageToMediaGallery
, am I able to simply reference an image instead of duplicating it?
If not, am I somehow able to utilize Magento's import dataflow behavior within an extension?
Hopefully I've titled this correctly - it's a strange question to ask.
Thanks