Php Magento在商店视图上设置图像

Php Magento在商店视图上设置图像,php,magento,Php,Magento,问题 我正在与Magento进行集成,首先从产品中删除所有图像,然后添加新图像。问题是,当我添加它们时,“缩略图”、“小图像”和“图像”只在默认存储上设置,而不是在单个存储上设置 代码 public function setProductImages($entityId, $images, $websiteIds, $removeFirst = true){ $product = Mage::getModel('catalog/product')->load($entityId);

问题

我正在与Magento进行集成,首先从产品中删除所有图像,然后添加新图像。问题是,当我添加它们时,“缩略图”、“小图像”和“图像”只在默认存储上设置,而不是在单个存储上设置

代码

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();
}
是否有某种方法可以告诉AddImageToDiagallery在存储阵列上设置?还是我遗漏了什么?

通过替换

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();