magento设置基本图像,如果我有图像的路径

时间:2014-03-27 18:14:28

标签: magento

$product1 = Mage::getModel('catalog/product')->loadByAttribute('sku', 'FN244403');
$product1ID = $product1->getId();
$connection = Mage::getSingleton('core/resource')->getConnection('core_read');
$sql = "Select * from `catalog_product_entity_media_gallery` WHERE `entity_id` = '$product1ID' 

LIMIT 1";
$rows       = $connection->fetchAll($sql);
echo $path = $rows[0]['value'];

那么,我现在如何设置基础,小图像等? 我试着这样做,但这没有帮助

try {
    $product1->setSmallImage($path)
        ->setThumbnail($path)
        ->setImage($path)
        ->save();
} catch (Exception $e) {
    echo $e->getMessage();
}

1 个答案:

答案 0 :(得分:1)

您可以使用以下代码设置图像。

$mediaAttribute = array (
            'thumbnail',
            'small_image',
            'image'
    );

$product1->addImageToMediaGallery($filepath_to_image, $mediaAttribute, true, false);