【问题标题】:Set display order for product images imported programmatically为以编程方式导入的产品图像设置显示顺序
【发布时间】:2013-04-07 22:37:57
【问题描述】:

在测试 magento 期间,我已经能够通过读取 XML 将产品导入商店。我的 XML 还包含与产品关联的图像 URL 数组。我阅读了每个图像属性中的 URL,下载它并将其移动到 media/import 文件夹中。然后我将每张图片与产品相关联

foreach($mediaArray as $imageType => $fileName)
{
    try {
         $product->addImageToMediaGallery($fileName, $imageType, false, false);
    } catch (Exception $e) {
         echo $e->getMessage();
    }
}

我要解决的一件事是确定图像的排序顺序,以及哪一个是在页面加载时显示的默认图像。有没有办法以编程方式说我希望这个文件成为第一个显示的图像?在页面加载时显示的 magento 并不是最好的。

【问题讨论】:

    标签: magento magento-1.7


    【解决方案1】:

    下面的代码允许您导入图像并设置位置。它将根据数组中图像的顺序设置位置,因此如果这不是必需的,则您需要进行更改,但希望这至少能让您了解如何完成。

    $sku = $product->getSku();
    $media = Mage::getModel('catalog/product_attribute_media_api');
    
    $position = 1;
    foreach($mediaArray as $fileName) {
    
        if (file_exists($fileName)) { // assuming $fileName is full path not just the file name
            $pathInfo = pathinfo($fileName);
    
            switch($pathInfo['extension']){
                case 'png':
                    $mimeType = 'image/png';
                    break;
                case 'jpg':
                    $mimeType = 'image/jpeg';
                    break;
                case 'gif':
                    $mimeType = 'image/gif';
                    break;
            }
    
            $types = ($position == 1) ? array('image', 'small_image', 'thumbnail') : array();
            $newImage = array(
                'file' => array(
                    'content' => base64_encode($fileName),
                    'mime' => $mimeType,
                    'name' => basename($fileName),
                    ),
                'label' => 'whatever', // change this. 
                'position' => $position,
                'types' => $types,
                'exclude' => 0,
            );
    
            $media->create($sku, $newImage);
            // OR (if you would rather use the product entity ID):
            // $media->create($productId, $newImage, null, 'id');
            $position++;
        } else {
            // image not found
        }
    }
    

    【讨论】:

    • 嗯,好的,有一个位置字段。我认为可能有一个参数可以在每个产品的基础上说“默认图像”。这应该可以正常工作,谢谢。
    • 我在 $media->create() 语句中收到“not_created”错误。另外,有没有办法更改管理 UI 中的默认图像?有时订单不是我想要的,所以我只想通过 UI 更改它。
    • 在这种情况下,我会尝试将一些调试代码放入 Mage_Catalog_Model_Product_Attribute_Media_Api 类中。如果您还不知道,可以在 app/code/core/Mage/Catalog/Model/Product/Attribute/Media/Api.php 中找到它。您应该能够以这种方式缩小故障发生的位置。请记住在之后恢复对核心代码所做的任何更改。是的,这可以在产品的图像部分手动更改(目录 -> 管理产品)。
    猜你喜欢
    • 1970-01-01
    • 2013-08-16
    • 1970-01-01
    • 2013-11-23
    • 2018-08-17
    • 2015-08-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多