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 asp.textbox后台赋值前端不能显示什么原因
  • ¥15 宇视监控服务器无法登录
  • ¥15 PADS Logic 原理图
  • ¥15 PADS Logic 图标
  • ¥15 电脑和power bi环境都是英文如何将日期层次结构转换成英文
  • ¥15 DruidDataSource一直closing
  • ¥20 气象站点数据求取中~
  • ¥15 如何获取APP内弹出的网址链接
  • ¥15 wifi 图标不见了 不知道怎么办 上不了网 变成小地球了
  • ¥50 STM32单片机传感器读取错误