19

I have to create a simple Magento 1.6.x import agent that suppose to create/update products and their images. Could someone advise me how to add product image without having to use the magento API?

The api performance turned out to be very poor and I am starting to be a little frustrated.. :-(

I have found some other questions regarding this problem, but none of them concerns with adding images to the product.

This is what I came with:

$product->setIsMassupdate(true)
    ->setExcludeUrlRewrite(true)
    ->setManufacturer($this->addManufacturers(utf8_encode($record[4])))
    ->setSku($record[3])
    ->setAttributeSetId($this->attribute_set)# 9 is for default
    ->setTypeId(Mage_Catalog_Model_Product_Type::TYPE_SIMPLE)
    ->setName(utf8_encode($record[5]))
    ->setCategoryIds($this->getCategories(array($record[0], $record[1], $record[2]))) # some cat id's,
    ->setWebsiteIDs(array(1)) # Website id, 1 is default
    ->setDescription(utf8_encode($record[6]))
    ->setShortDescription($this->shortText(utf8_encode($record[6]), 150))
    ->setPrice($price) # Set some price
    ->setSpecialPrice($special_price)
    ->setWeight($record[12])
    ->setStatus( Mage_Catalog_Model_Product_Status::STATUS_ENABLED )
    ->setVisibility(Mage_Catalog_Model_Product_Visibility::VISIBILITY_BOTH)
    ->setTaxClassId(2)     // default tax class
    ->setPixmaniaimg($record[10])
    ->setStockData(array('is_in_stock' => $inStock, 'qty' => $qty))
    ->setCreatedAt(strtotime('now'));

Can someone help me with adding images directly without the API?

Thanks

Lukas

Bery
  • 1,094
  • 1
  • 10
  • 23
  • Magento 1.6 - sorry for ommitting that in my original comment.. – Bery Dec 12 '11 at 00:57
  • For Magento 2: http://magento.stackexchange.com/questions/140612/magento-2-save-all-product-data-outside-magento-with-images – Jackson Oct 21 '16 at 10:33

2 Answers2

39

I did this in Magento 1.6.1. Just put your image URL paths in the first array and you are good to go.

Also 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/>";
    }
}
Zachary Schuessler
  • 3,644
  • 2
  • 28
  • 43
  • Is there any way to create the images with magento? I have been provided with only one size of the image. – Bery Dec 12 '11 at 01:00
  • First, do you need separate images? Magento lets you set a single image as the thumbnail, small_image, and image size if you need. – Zachary Schuessler Dec 12 '11 at 01:10
  • Ok. I need to use one or more images (maximum of hree images per product) and then use - first one as base image, then small_image and thumbnail. The image is always the same (e.g. image1.jpg is base, small_image and thumbnail). – Bery Dec 12 '11 at 16:58
  • Worked perfectly. Thanks for you help. – Bery Dec 15 '11 at 15:25
  • For Magento 2: http://magento.stackexchange.com/questions/140612/magento-2-save-all-product-data-outside-magento-with-images – Jackson Oct 21 '16 at 10:34
1
set_time_limit(0);

ini_set('memory_limit', '4095M');

error_reporting(E_ALL);

ini_set('display_errors', 1);

require_once '../app/Mage.php';

umask(0);

Mage::setIsDeveloperMode(true);

$storeID = Mage_Core_Model_App::ADMIN_STORE_ID;

Mage::app()->setCurrentStore($storeID);



$destination = Mage::getBaseDir() . '/import/images/' . $image;

$product->addImageToMediaGallery($destination, array('image', 'thumbnail', 'small_image'), false, false);

}

This will set the base image.

Amit Singh
  • 266
  • 3
  • 13