简体   繁体   中英

Magento - auto set base image, small image and thumbnail on import (in admin panel)?

I'm making an import products script from an xml file on magento CE 1.9.1.1
All attributes are imported correctly but in admin panel, images radio buttons are not checked, so my product images don't appear in frontend.
How can i have these radio buttons automatically checked after import ?

Here my code to import images :

$sku = Mage::getModel('catalog/product')->loadByAttribute('sku', $sku);    
$url = $image; //$image, $small_image, $thumbnail are external urls image files
$image_type = substr(strrchr($url,"."),1); //find the image extension
$filename   = $sku.'.'.$image_type;
$filepath   = Mage::getBaseDir('media') . DS . 'import'. DS . $filename;

file_put_contents($filepath, file_get_contents(trim($url)));
if($is_default==true){
    $mediaAttribute = array (
        'image'=>$image,
        'small_image'=>$small_image,
        'thumbnail'=>$thumbnail,
        );
}else{
    $mediaAttribute = null;
}
$prod->addImageToMediaGallery($filepath, $mediaAttribute, false, false);
$prod->save();

I try these sql request where 85,86,87 are my ids for image, small image and thumbnail :

UPDATE catalog_product_entity_media_gallery AS mg,
       catalog_product_entity_media_gallery_value AS mgv,
       catalog_product_entity_varchar AS ev
SET ev.value = mg.value
WHERE  mg.value_id = mgv.value_id
AND mg.entity_id = ev.entity_id
AND ev.attribute_id IN (85,86,87)
AND mgv.position = 1;

but It says 0 rows changed.

Also i follow step on : Magento - Auto set base, small and thumbnail on upload

It works for upload manually but how can make it works for import ?

Thanks,

JayD

I found my mistake, the issue was :

if($is_default==true){
    $mediaAttribute = array (
        'image'=>$image,
        'small_image'=>$small_image,
        'thumbnail'=>$thumbnail,
   );
}else{
    $mediaAttribute = null;
}

Now it works with :

$url = $image;

$image_type = substr(strrchr($url,"."),1); //find the image extension
$filename   = $sku.'.'.$image_type; //give a new name, you can modify as per your requirement
$filepath   = Mage::getBaseDir('media') . DS . 'import'. DS . $filename; //path for temp storage folder: ./media/import/

file_put_contents($filepath, file_get_contents(trim($url))); //store the image from external url to the temp storage folder


    $mediaAttribute = array (   'image',
                                'small_image',
                                'thumbnail',
                            );

$prod->addImageToMediaGallery($filepath, $mediaAttribute, true, false);

$prod->save();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM