Magento 2: cách tốt nhất để thêm / xóa sản phẩm Hình ảnh theo chương trình?


18

Tôi muốn tải hình ảnh lên các sản phẩm hiện có. Những hình ảnh được trong import_dir. Và chúng cần được thêm vào sản phẩm đã tồn tại trong danh mục.

Tôi chỉ có thể tìm thấy 2 cách để làm điều đó.
1. Cách "thực hành xấu" - sử dụng mô hình sản phẩm\Magento\Catalog\Model\Product::addImageToMediaGallery

1. Copy the images from `import_dir` to `pub/media/tmp`
2. Add the images to the product
3. Save product

    /* copy files from import_dir to pub/media/tmp */

    /** @var \Magento\Catalog\Api\Data\ProductInterface $product */
    /* Init media gallery */
    $mediaGalleryEntries = $product->getMediaGalleryEntries();
    if (empty($mediaGalleryEntries) === true){
        $product->setMediaGalleryEntries([]);
    }

    /* Add an image to the product's gallery */
    $product->addImageToMediaGallery(
        $filePathFromTmpDir,
        [
          "image",
          "small_image",
          "thumbnail",
          "swatch_image" 
        ],
        $moveImage,
        $disableImage
    );

    /* Save */
    $this->_productRepository->save($product);

2. Cách "thực hành tốt" - sử dụng API \Magento\Catalog\Api\ProductAttributeMediaGalleryManagementInterface::create

1. Create image content object via **\Magento\Framework\Api\Data\ImageContentInterfaceFactory**
2. Create image object via **\Magento\Catalog\Api\Data\ProductAttributeMediaGalleryEntryInterfaceFactory**
3. Create an image via API

    $imageContent = $this->_imageContentInterfaceFactory->create()
        ->setBase64EncodedData(base64_encode(file_get_contents($filePathImportDir)))
        ->setType($this->_mime->getMimeType($filePathImportDir))
        ->setName($file_name);

    $newImage = $this->_productAttributeMediaGalleryEntryInterfaceFactory->create()
        ->setMediaType(\Magento\Catalog\Model\Product\Attribute\Backend\Media\ImageEntryConverter::MEDIA_TYPE_CODE)
        ->setFile($filePathImportDir)
        ->setDisabled($disableImage)
        ->setContent($imageContent)
        ->setLabel('label');

    $this->_productAttributeMediaGalleryManagement->create($product->getSku(), $newImage);

Mối quan tâm:

  • Trong 1 tôi đang gặp lỗi, đây là sự cố đã biết

    Chỉ mục không xác định: media_type

  • Trong 2 là cách quá phức tạp và nó sẽ là cách dễ dàng hơn

Câu hỏi:

  • Có cách "thực hành tốt nhất" để quản lý (thêm, xóa, thay thế) hình ảnh của sản phẩm không?
  • Có lẽ có một cách với \ Magento \ CatalogImportExport \ Model \ Import \ Product

Câu trả lời:


3

Nó có thể được thực hiện bằng cách sử dụng cách thứ hai của bạn, ít nhiều như thế này:

<?php


namespace [vendor]\[moduleName]\Model\Adminhtml\Product\MediaGallery;

use \Magento\Catalog\Model\Product\Gallery\EntryFactory;
use \Magento\Catalog\Model\Product\Gallery\GalleryManagement;
use \Magento\Framework\Api\ImageContentFactory;

{

/**
 * @var \Magento\Catalog\Model\Product\Gallery\EntryFactory
 */
private $mediaGalleryEntryFactory;


/**
 * @var \Magento\Catalog\Model\Product\Gallery\GalleryManagement
 */
private $mediaGalleryManagement;


/**
 * @var \Magento\Framework\Api\ImageContentFactory
 */
private $imageContentFactory;


/**
 * @param \Magento\Catalog\Model\Product\Gallery\EntryFactory $mediaGalleryEntryFactory
 * @param \Magento\Catalog\Model\Product\Gallery\GalleryManagement $mediaGalleryManagement
 * @param \Magento\Framework\Api\ImageContentFactory $imageContentFactory
 */
public function __construct
(
    EntryFactory $mediaGalleryEntryFactory,
    GalleryManagement $mediaGalleryManagement,
    ImageContentFactory $imageContentFactory
)
{
    $this->mediaGalleryEntryFactory = $mediaGalleryEntryFactory;
    $this->mediaGalleryManagement = $mediaGalleryManagement;
    $this->imageContentFactory = $imageContentFactory;
}


/**
 * @param string $filePath
 * @param string $sku
 */
public function processMediaGalleryEntry($filePath, $sku)
{
    $entry = $this->mediaGalleryEntryFactory->create();

    $entry->setFile($filePath);
    $entry->setMediaType('image');
    $entry->setDisabled(false);
    $entry->setTypes(['thumbnail', 'image', 'small_image']);

    $imageContent = $this->imageContentFactory->create();
    $imageContent
        ->setType(mime_content_type($filePath))
        ->setName('test')
        ->setBase64EncodedData(base64_encode(file_get_contents($filePath)));

    $entry->setContent($imageContent);

    $this->mediaGalleryManagement->create($sku, $entry);
}

}

$filePathnên là một đường dẫn tuyệt đối - có thể bạn có thể sử dụng constans BP.


thật đáng buồn khi không còn cách nào tốt hơn để giải quyết nó, vì như @JakubIlczuk đã nói - nó rất hạn chế. Về $entry->setMediaType('image');dòng này tôi không chắc lắm, vì theo như tôi nhớ thì nó gây ra lỗi cho tôi một cái gì đó giống như nó cần một loại "png" hoặc "jpg" (vì vậy cuối cùng nó phải là "image / png"). Nhưng một lần nữa, tôi không chắc chắn
Olga Zhe

Trong magento 2.1.1, nó không gây ra lỗi như vậy.
Bartosz Kubicki

Không có địa chỉ phương thức loại bỏ hoặc thay thế. Chỉ là một quan sát.
Rooster242

0

sau khi xem xét những điều tương tự như Bạn rõ ràng tôi đang ở cùng một nơi, và không thể tìm thấy cách nào tốt hơn sau đó 2. Và điều này rất hạn chế. Trong các thử nghiệm chức năng, họ đang sử dụng sản phẩm đơn giản-> save () gây ra các vấn đề khác (đối với cá nhân tôi, url_key đã tồn tại lỗi). Có vẻ như chỉ có thể sử dụng phương pháp thứ 2, tuy nhiên có vẻ phức tạp và khó hiểu. Nhưng tôi tự hỏi nếu trong phương pháp thứ 2, bạn đã tìm ra cách đặt hình ảnh được tải lên thành hình thu nhỏ hay hình ảnh nhỏ?

Khi sử dụng trang web của chúng tôi, bạn xác nhận rằng bạn đã đọc và hiểu Chính sách cookieChính sách bảo mật của chúng tôi.
Licensed under cc by-sa 3.0 with attribution required.