3

I have a script to import a list of product from xls. It works fine except for the image; I use the following bits of code:

...

if(file_exists(Mage::getBaseDir('media') . DS . 'import' . DS . $gallery_img)){
    $product->addImageToMediaGallery(Mage::getBaseDir('media') . DS . 'import' . DS . $gallery_img, null, false, false);
}

...

The image is actually added to the product but it's not selected, so it does not show in the frontend:

enter image description here

How can I "select" it programmatically?

Thanks

4

If you check Magento's addImageToMediaGallery function located at app\code\core\Mage\Catalog\Model\Product.php file,

public function addImageToMediaGallery($file, $mediaAttribute=null, $move=false, $exclude=true)

You will see that second attribute is code of attribute with type 'media_image'. This attribute will define which image will be set as which media image.
By default there are three.

  1. image
  2. small_image
  3. thumbnail

So you need to pass value to your second argument as below

$product->addImageToMediaGallery(Mage::getBaseDir('media') . DS . 'import' . DS . $gallery_img, array('image', 'small_image', 'thumbnail'), false, false);

to set them as main image, small image and thumbnail image.

4

Look at Mage_Catalog_Model_Product to become familiar with addImageToMediaGallery() and other methods you'll undoubtedly need to be aware of in the future.

// Add three image sizes to media gallery
$mediaArray = array(
    'thumbnail'   => $putPathHere,
    'small_image' => $putPathHere,
    'image'       => $putPathHere,
);

// Remove unset images, add image to gallery if exists
$importDir = Mage::getBaseDir('media') . DS . 'import/';

foreach($mediaArray as $imageType => $fileName) {
    $filePath = $importDir.$fileName;
    if ( file_exists($filePath) ) {
        try {
            $product->addImageToMediaGallery($filePath, $imageType, false);
        } catch (Exception $e) {
            echo $e->getMessage();
        }
    } else {
        echo "Product does not have an image or the path is incorrect. Path was: {$filePath}<br/>";
    }
}

Or

<?php
require_once 'app/Mage.php';
Mage::app();
Mage::app()->getStore()->setId(Mage_Core_Model_App::ADMIN_STORE_ID);
$importDir = Mage::getBaseDir('media') . DS . 'bulkimages/101/';

$productsData = array('Purple-Crown-Ring-MFAS.jpg','Purple-Crown-Ring-MFC.jpg','Purple-Crown-Ring-MSAS.jpg','Purple-Crown-Ring-MSF.jpg');

$productSKU = '111';
$ourProduct = Mage::getModel('catalog/product')->loadByAttribute('sku',$productSKU);

foreach($productsData as $fileName){
    $filePath = $importDir.$fileName;
    if (file_exists($filePath)) {
        $ourProduct->addImageToMediaGallery($filePath, array('image', 'small_image', 'thumbnail'), false, false);
        $ourProduct->save();
        echo "done ";
    } else {
        echo $productSKU . " not done";
        echo "<br>";
    }   
}
?>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.