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

时间:2016-03-31 09:01:24

标签: php image magento import

我正在使用magento CE 1.9.1.1上的xml文件制作导入产品脚本 所有属性都已正确导入,但在管理面板中,图像单选按钮未被选中,因此我的产品图像不会出现在前端。
如何在导入后自动检查这些单选按钮?

这是我导入图片的代码:

$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();

我尝试这些sql请求,其中85,86,87是我的图片,小图片和缩略图的ID:

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;

但它说0行改变了。

我也按照步骤操作:Magento - Auto set base, small and thumbnail on upload

它适用于手动上传,但如何使其适用于导入?

谢谢,

JayD

1 个答案:

答案 0 :(得分:0)

我发现了我的错误,问题是:

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

现在可以使用:

$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();