0

I'm using Magento2.3.4 and there is an API response that sends me JSON to add a new product image:

{
  "base_image"  : [{"type":"url","value":"http://imgur.com/base_image.png"}],
  "small_image" : [{"type":"url","value":"http://imgur.com/small_image.png"}],
  "thumbnail"   : [{"type":"url","value":"http://imgur.com/thumbnail.png"}],
  "gallery"     : [
                   {"type":"url","value":"http://imgur.com/gallery_1.jpg"},
                   {"type":"url","value":"http://imgur.com/gallery_2.jpg"}, 
                  ]
}

as you can see from the json above , there is an external url that I need to save as product images, how can I do this?

0

I am not exactly sure about the requirement but maybe you can convert this JSON response to CSV file and then use Magento\ImportExport\Model\Import class to import this csv. This way, you will be using Magento import functionality and that will handle the external image url itself.

$import_file = pathinfo($import_path);
    /** @var \Magento\ImportExport\Model\Import $import */
    $import = $this->importFactory->create();
    $import->setData(
        [
            'entity' => \Magento\Catalog\Api\Data\ProductAttributeInterface::ENTITY_TYPE_CODE,
            'behavior' => \Magento\ImportExport\Model\Import::BEHAVIOR_ADD_UPDATE,
            'validation_strategy' => \Magento\ImportExport\Model\Import\ErrorProcessing\ProcessingErrorAggregatorInterface::VALIDATION_STRATEGY_SKIP_ERRORS,
        ]
    );

    $read_file = $this->readFactory->create($import_file['dirname']);
    $csvSource = $this->csvFactory->create(
        [
            'file' => $import_file['basename'],
            'directory' => $read_file,
        ]
    );

    $validate = $import->validateSource($csvSource);
    if (!$validate) {
        $this->logger->error('Unable to validate the CSV.');
    }

    $result = $import->importSource();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.