magento - Set display order for product images imported programmatically -
during testing of magento, have been able import products store reading xml. xml contains array of image urls associated product. read url in each image property, download , move media/import folder. associate each image product
foreach($mediaarray $imagetype => $filename) { try { $product->addimagetomediagallery($filename, $imagetype, false, false); } catch (exception $e) { echo $e->getmessage(); } }
the 1 thing fix decided sort order of images, , 1 default image displayed on page load. there way programmatically want file first image display? 1 magento showing on page load isn't best.
the code below allows import images , set position. set position based on order of images in array need change if not required @ least give idea of how can done.
$sku = $product->getsku(); $media = mage::getmodel('catalog/product_attribute_media_api'); $position = 1; foreach($mediaarray $filename) { if (file_exists($filename)) { // assuming $filename full path not file name $pathinfo = pathinfo($filename); switch($pathinfo['extension']){ case 'png': $mimetype = 'image/png'; break; case 'jpg': $mimetype = 'image/jpeg'; break; case 'gif': $mimetype = 'image/gif'; break; } $types = ($position == 1) ? array('image', 'small_image', 'thumbnail') : array(); $newimage = array( 'file' => array( 'content' => base64_encode($filename), 'mime' => $mimetype, 'name' => basename($filename), ), 'label' => 'whatever', // change this. 'position' => $position, 'types' => $types, 'exclude' => 0, ); $media->create($sku, $newimage); // or (if rather use product entity id): // $media->create($productid, $newimage, null, 'id'); $position++; } else { // image not found } }
Comments
Post a Comment