dongying195959 2016-03-31 09:01
浏览 41
已采纳

Magento - 导入时自动设置基本图像,小图像和缩略图(在管理面板中)?

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

  • 写回答

1条回答 默认 最新

  • dte66654 2016-04-06 08:40
    关注

    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();
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 这种微信登录授权 谁可以做啊
  • ¥15 请问我该如何添加自己的数据去运行蚁群算法代码
  • ¥20 用HslCommunication 连接欧姆龙 plc有时会连接失败。报异常为“未知错误”
  • ¥15 网络设备配置与管理这个该怎么弄
  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来