Let’s initiate a discussion!!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
<?php /** * file location: * app/code/VendorName/ExtensionName/Service/ImportImageService.php */ namespace VendorName\ExtensionName\Service; use Magento\Catalog\Model\Product; use Magento\Framework\App\Filesystem\DirectoryList; use Magento\Framework\Filesystem\Io\File; /** * Class ImportImageService * assign images to products by image URL */ class ImportImageService { /** * Directory List * * @var DirectoryList */ protected $directoryList; /** * File interface * * @var File */ protected $file; /** * ImportImageService constructor * * @param DirectoryList $directoryList * @param File $file */ public function __construct( DirectoryList $directoryList, File $file ) { $this->directoryList = $directoryList; $this->file = $file; } /** * Main service executor * * @param Product $product * @param string $imageUrl * @param array $imageType * @param bool $visible * * @return bool */ public function execute($product, $imageUrl, $visible = false, $imageType = []) { /** @var string $tmpDir */ $tmpDir = $this->getMediaDirTmpDir(); /** create folder if it is not exists */ $this->file->checkAndCreateFolder($tmpDir); /** @var string $newFileName */ $newFileName = $tmpDir . baseName($imageUrl); /** read file from URL and copy it to the new destination */ $result = $this->file->read($imageUrl, $newFileName); if ($result) { /** add saved file to the $product gallery */ $product->addImageToMediaGallery($newFileName, $imageType, true, $visible); } return $result; } /** * Media directory name for the temporary file storage * pub/media/tmp * * @return string */ protected function getMediaDirTmpDir() { return $this->directoryList->getPath(DirectoryList::MEDIA) . DIRECTORY_SEPARATOR . 'tmp' . DIRECTORY_SEPARATOR; } } |
Using above fucntion Images can be imported directly from external url.
Override view block using di.xml and add the below code...
You can check a list of called layout XML for...
Follow the below steps to install and set up PWA...
If you want to remove all leading zero's from order,...
We will create a new category page layout and use...
Let our Magento expert connect to discuss your requirement.
We offer Magento
certified developers.
Our Magento clientele
is 500+.
We sign NDA for the
security of your projects.
We’ve performed 100+
Magento migration projects.
Free quotation
on your project.
Three months warranty on
code developed by us.