我如何为产品设置图像

时间:2014-03-27 16:18:45

标签: magento

我选择没有图像的magento产品。但产品包含图片,只有他们不会在管理员中选择ThumbnailSmall ImageBase Image。我怎么能做图像Base Image

$products123321 = Mage::getModel('catalog/product')
    ->getCollection()
    ->addAttributeToSelect('*')
    ->addAttributeToFilter(array(
        array (
            'attribute' => 'image',
            'like' => 'no_selection'
        ),
        array (
            'attribute' => 'image', // null fields
            'null' => true
        ),
        array (
            'attribute' => 'image', // empty, but not null
            'eq' => ''
        ),
        array (
            'attribute' => 'image', // check for information that doesn't conform to Magento's 

formatting
            'nlike' => '%/%/%'
        ),
    ));

1 个答案:

答案 0 :(得分:0)

$mediaGallery = $product123321->getMediaGallery();
//if there are images
if (isset($mediaGallery['images'])){
    //loop through the images
    foreach ($mediaGallery['images'] as $image){
        //set the first image as the base image
        Mage::getSingleton('catalog/product_action')->updateAttributes(array($product123321-

>getId()), array('image'=>$image['file']), 0);
        //stop
        break;
    }
}
   echo "Edited sku is ".$product123321->getSku();

同时

$product1 = Mage::getModel('catalog/product')->loadByAttribute('sku', 'FN244403');
$product2 = Mage::getModel('catalog/product')->loadByAttribute('sku', 'FN229437');
$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'];
   echo "Edited sku is ".$product1->getSku()." ".$product1->getImage()." ".$product1-

>getThumbnail()." ".$product2->getImage();
echo " asdasd ".Mage::getBaseDir('media') . DS . 'catalog/product' .trim($rows[0]['value']);  
Mage::app()->getStore()->setId(Mage_Core_Model_App::ADMIN_STORE_ID);
$product1->addImageToMediaGallery(Mage::getBaseDir('media') . DS . 'catalog/product' .trim

($rows[0]['value']), array('image', 'small_image', 'thumbnail'), false, false); 
$product1->save();