以编程方式为Store View(Magento)设置默认媒体库图像

时间:2011-08-01 02:48:58

标签: php magento

我有一个脚本可以将图像添加到我的产品中。它用于设置图像,small_image和缩略图。该代码适用于默认视图,但当我切换到商店视图时,媒体库设置为“no_image”。导致我的产品在前端完全没有图像。

我尝试重置商店视图属性但没有成功。

$product->addImageToMediaGallery($fileName, array('image', 'small_image', 'thumbnail'), false, false);
$attributes = $product->setStoreId(1)->getTypeInstance(true)->getSetAttributes($product);
if (isset($attributes['media_gallery'])) {
    $attributes['media_gallery']->getBackend()->clearMediaAttribute($product, array('image', 'small_image', 'thumbnail'));
}
$product->save(); 

如何修改特定商店属性,并将其重置为使用父属性?

谢谢。

2 个答案:

答案 0 :(得分:4)

您的'简单解决方案'可以更简单;

foreach($product->getStoreIds() as $storeId) {
    $product->setStoreId($storeId)
        ->setImage(false)
        ->setSmallImage(false)
        ->setThumbnail(false)
        ->save();
}

答案 1 :(得分:2)

对于那些仍然有问题的人我找到了sql解决方案:

DELETE FROM `catalog_product_entity_varchar` 
WHERE store_id IN (1,2,3,4)
      and entity_type_id=4 
      and (attribute_id=77 or attribute_id=78 or attribute_id=79);

在我的情况下,我想恢复所有商店视图的默认图片视图(ID为1,2,3,4)

编辑:电子耶稣建议的修改过的SQL