Quantcast
Channel: Journal » Magento
Viewing all articles
Browse latest Browse all 10

Change selected product image programmatically

$
0
0

First, fish out your image using the API:

$api = Mage::getModel('catalog/product_attribute_media_api');
$items = $api->items(PRODUCT_ID);
/*
    Add your logic here, each $item will look like this:
	array
	  'file' => string '/5/5/55asdg234_0_1.png' (length=31)
	  'label' => string '' (length=0)
	  'position' => string '1' (length=1)
	  'exclude' => string '0' (length=1)
	  'url' => string 'http://domain.com/media/catalog/product/5/5/55asdg234_0_1.png' (length=67)
	  'types' => 
		array
		  0 => string 'perspective_2' (length=13)
*/

Then simply update the attribute using the value of

file
:
$file' = '/5/5/55asdg234_0_1.png';

$product = Mage::getModel('catalog/product')->load(PRODUCT_ID);
$product->setData('image', $file);
$product->setData('small_image', $file);
$product->setData('thumbnail', $file);
$product->setData('any_custom_image_attribute', $file);

try {
    $product->save();
} catch (Exception $e){ ... }

Use the value “

no_selection
” to deselect the image. That’s all there is to it.

Viewing all articles
Browse latest Browse all 10

Latest Images

Trending Articles



Latest Images