问题
我正在与Magento进行集成,它首先从产品中删除所有图像,然后添加新图像。问题是,当我添加它们时,"缩略图"、"small_image"answers"图像"只在默认存储中设置,而不是在单个存储中设置。
代码
public function setProductImages($entityId, $images, $websiteIds, $removeFirst = true){
$product = Mage::getModel('catalog/product')->load($entityId);
if($removeFirst){
//First I need to reset image selection before I can remove all images
$values = array(
'image' => 'no_selection',
'small_image' => 'no_selection',
'thumbnail' => 'no_selection'
);
//Go through all sites and stores and set the image selection values
foreach($websiteIds as $websiteId) {
$website = Mage::getModel('core/website')->load($websiteId);
foreach ($website->getStoreIds() as $storeId) {
Mage::getSingleton('catalog/product_action')->updateAttributes(array($product->getId()), $values, $storeId);
}
}
//Set the selection values on admin store
Mage::getSingleton('catalog/product_action')->updateAttributes(array($product->getId()), $values, 0);
//Remove all images on product
$mediaGalleryAttribute = Mage::getModel('catalog/resource_eav_attribute')->loadByCode($product->getEntityTypeId(), 'media_gallery');
$gallery = $product->getMediaGalleryImages();
foreach ($gallery as $galleryImage) {
$imageFile = $galleryImage->getFile();
$mediaGalleryAttribute->getBackend()->removeImage($product, $imageFile);
}
$product->save();
}
foreach($images as $image) {
file_put_contents($path . DS . $image->filename, $image->content);
$type = in_array($image->name, array('image', 'small_image', 'thumbnail')) ? $image->name : null;
$product->addImageToMediaGallery($path . DS . $image->filename, $type, true, false);
}
$product->save();
}
有没有办法告诉addImageToMediaGallery设置一系列商店?还是我错过了什么?
通过替换解决问题
foreach($images as $image) {
file_put_contents($path . DS . $image->filename, $image->content);
$type = in_array($image->name, array('image', 'small_image', 'thumbnail')) ? $image->name : null;
$product->addImageToMediaGallery($path . DS . $image->filename, $type, true, false);
}
$product->save();
这个:
$imageTypes = array();
//Sets images on default store for product
foreach($images as $image){
file_put_contents($path . DS . $image->filename, $image->content);
$type = in_array($image->name, array('image', 'small_image', 'thumbnail')) ? $image->name : null;
$product->addImageToMediaGallery($path . DS . $image->filename, $type, true, false);
//Store image, small_image and thumbnail for later use
$imageTypes['image'] = $product->getImage();
$imageTypes['small_image'] = $product->getSmallImage();
$imageTypes['thumbnail'] = $product->getThumbnail();
}
$product->save();
//Go through all stores and set image, small_image and thumbnail
foreach($websiteIds as $websiteId) {
$website = Mage::getModel('core/website')->load($websiteId);
foreach($website->getStoreIds() as $storeId) {
Mage::app()->setCurrentStore($storeId);
Mage::getSingleton('catalog/product_action')->updateAttributes(array($product->getId()), $imageTypes, $storeId);
}
}
$product->save();