ฉันต้องการอัปโหลดภาพไปยังผลิตภัณฑ์ที่มีอยู่ import_dirภาพที่อยู่ใน และพวกเขาจำเป็นต้องเพิ่มไปยังผลิตภัณฑ์ที่มีอยู่แล้วในแคตตาล็อก
ฉันสามารถหาวิธีทำได้ 2 วิธีเท่านั้น 
1. "วิธีปฏิบัติที่ไม่เหมาะสม" - ใช้รุ่นผลิตภัณฑ์\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. "แนวปฏิบัติที่ดี" - ใช้ 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);
ความกังวลเกี่ยวกับ:
- ใน1ฉันได้รับข้อผิดพลาดซึ่งเป็นปัญหาที่ทราบแล้ว
ดัชนีที่ไม่ได้กำหนด: media_type 
- ใน2เป็นวิธีที่ซับซ้อนเกินไปและควรเป็นวิธีที่ง่ายกว่า
คำถาม:
- มีวิธีปฏิบัติที่ดีที่สุดในการจัดการ (เพิ่มลบเปลี่ยน) ภาพผลิตภัณฑ์หรือไม่
- อาจมีวิธีกับ\ Magento \ CatalogImportExport \ Model \ Import \ Product
$entry->setMediaType('image');บรรทัดนี้ฉันไม่แน่ใจ cuz เท่าที่ฉันจำได้ว่ามันทำให้ฉันเกิดข้อผิดพลาดบางอย่างเช่นที่มันต้องการประเภท "png" หรือ "jpg" (ดังนั้นในที่สุดมันควรเป็น "image / png") แต่อีกครั้งฉันไม่แน่ใจ