【问题标题】:How To Add Additional Images To A Product In Magento如何在 Magento 中向产品添加其他图像
【发布时间】:2014-11-20 06:56:22
【问题描述】:

我可以在创建过程中使用以下脚本将图像添加到产品中。但是,我无法加载产品并将其他图像添加到产品媒体库。我是否必须一次删除产品媒体库并读取所有图像?它是 csv 文件中每行一张图片。

public function addImage($product, $image)
{
    $imagePath = $this->downloadImage($image);
    $product->setMediaGallery(array('images' => array(), 'values' => array()));
    if (is_file($imagePath)) {
        $product->addImageToMediaGallery($imagePath, array('image', 'small_image', 'thumbnail'), false, false);
    }
}

【问题讨论】:

    标签: php magento csv


    【解决方案1】:

    为什么要使用脚本来添加图像?在 magento 中添加图像可以通过管理界面轻松完成。

    只需登录管理面板,目录 > 管理产品 > 编辑产品 > 图片(在左侧面板)。

    【讨论】:

      【解决方案2】:

      您可以使用以下脚本添加其他图像。

      $importDir = Mage::getBaseDir('media') . DS;
      // additional images
          if ($import_product[29] != '') {
              $addImages = explode(",", trim($import_product[29]));
              foreach ($addImages as $additional_image) {
                  $image_directory = $dir .DS.'data'.DS. trim($additional_image);
                  if (file_exists($image_directory)) {
                      $product->addImageToMediaGallery($image_directory, null, false, false);
                  } else {
                      $image_directory = $dir . 'data' . DS . 'comingsoon.jpg';
                      $product->addImageToMediaGallery($image_directory, null, false, false);
                  }
              }
              echo 'Additional images for product ' . $product->getName() . ' ' . $product->getId() . ' imported successfully' . PHP_EOL;
          }
      

      $import_product[29] 是由逗号分隔的图像数组。请同时参考我的教程,其中解释了如何设置默认图像。

      http://www.pearlbells.co.uk/how-to-add-main-image-and-additional-image-to-the-products-pro-grammatically-magento/

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2023-04-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多