Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- namespace CompanyNameModuleNameControllerIndex;
- use MagentoFrameworkAppActionAction;
- class Test extends Action
- {
- protected $videoGalleryProcessor;
- protected $_product;
- public function __construct(
- MagentoFrameworkAppActionContext $context,
- MagentoCatalogModelProduct $product,
- CompanyNameModuleNameModelProductGalleryVideoProcessor $videoGalleryProcessor
- ){
- parent::__construct($context);
- $this->_product = $product;
- $this->videoGalleryProcessor = $videoGalleryProcessor;
- }
- public function execute()
- {
- $productId = 1; // product id
- $product = $this->_product->load($productId);
- $product->setStoreId(0); //set store vise data
- // sample video data
- $videoData = [
- 'video_id' => "abc", //set your video id
- 'video_title' => "title", //set your video title
- 'video_description' => "description", //set your video description
- 'thumbnail' => "image path", //set your video thumbnail path.
- 'video_provider' => "youtube",
- 'video_metadata' => null,
- 'video_url' => "https://www.youtube.com/watch?v=abc", //set your youtube video url
- 'media_type' => MagentoProductVideoModelProductAttributeMediaExternalVideoEntryConverter::MEDIA_TYPE_CODE,
- ];
- //download thumbnail image and save locally under pub/media
- $videoData['file'] = $videoData['video_id'] . 'filename.jpg';
- // Add video to the product
- if ($product->hasGalleryAttribute()) {
- $this->videoGalleryProcessor->addVideo(
- $product,
- $videoData,
- ['image', 'small_image', 'thumbnail'],
- false,
- true
- );
- }
- $product->save();
- }
- }
- <?php
- namespace CompanyNameModuleNameModelProductGalleryVideo;
- use MagentoFrameworkExceptionLocalizedException;
- class Processor extends MagentoCatalogModelProductGalleryProcessor
- {
- /**
- * @var MagentoCatalogModelProductGalleryCreateHandler
- */
- protected $createHandler;
- /**
- * Processor constructor.
- * @param MagentoCatalogApiProductAttributeRepositoryInterface $attributeRepository
- * @param MagentoMediaStorageHelperFileStorageDatabase $fileStorageDb
- * @param MagentoCatalogModelProductMediaConfig $mediaConfig
- * @param MagentoFrameworkFilesystem $filesystem
- * @param MagentoCatalogModelResourceModelProductGallery $resourceModel
- * @param MagentoCatalogModelProductGalleryCreateHandler $createHandler
- */
- public function __construct(
- MagentoCatalogApiProductAttributeRepositoryInterface $attributeRepository,
- MagentoMediaStorageHelperFileStorageDatabase $fileStorageDb,
- MagentoCatalogModelProductMediaConfig $mediaConfig,
- MagentoFrameworkFilesystem $filesystem,
- MagentoCatalogModelResourceModelProductGallery $resourceModel,
- MagentoCatalogModelProductGalleryCreateHandler $createHandler
- )
- {
- parent::__construct($attributeRepository, $fileStorageDb, $mediaConfig, $filesystem, $resourceModel);
- $this->createHandler = $createHandler;
- }
- /**
- * @param MagentoCatalogModelProduct $product
- * @param array $videoData
- * @param array $mediaAttribute
- * @param bool $move
- * @param bool $exclude
- * @return string
- * @throws LocalizedException
- */
- public function addVideo(
- MagentoCatalogModelProduct $product,
- array $videoData,
- $mediaAttribute = null,
- $move = false,
- $exclude = true
- ) {
- $file = $this->mediaDirectory->getRelativePath($videoData['file']);
- if (!$this->mediaDirectory->isFile($file)) {
- throw new LocalizedException(__('The image does not exist.'));
- }
- $pathinfo = pathinfo($file);
- $imgExtensions = ['jpg', 'jpeg', 'gif', 'png'];
- if (!isset($pathinfo['extension']) || !in_array(strtolower($pathinfo['extension']), $imgExtensions)) {
- throw new LocalizedException(__('Please correct the image file type.'));
- }
- $fileName = MagentoMediaStorageModelFileUploader::getCorrectFileName($pathinfo['basename']);
- $dispretionPath = MagentoMediaStorageModelFileUploader::getDispretionPath($fileName);
- $fileName = $dispretionPath . '/' . $fileName;
- $fileName = $this->getNotDuplicatedFilename($fileName, $dispretionPath);
- $destinationFile = $this->mediaConfig->getTmpMediaPath($fileName);
- try {
- /** @var $storageHelper MagentoMediaStorageHelperFileStorageDatabase */
- $storageHelper = $this->fileStorageDb;
- if ($move) {
- $this->mediaDirectory->renameFile($file, $destinationFile);
- //Here, filesystem should be configured properly
- $storageHelper->saveFile($this->mediaConfig->getTmpMediaShortUrl($fileName));
- } else {
- $this->mediaDirectory->copyFile($file, $destinationFile);
- $storageHelper->saveFile($this->mediaConfig->getTmpMediaShortUrl($fileName));
- }
- } catch (Exception $e) {
- throw new LocalizedException(__('We couldn't move this file: %1.', $e->getMessage()));
- }
- $fileName = str_replace('\', '/', $fileName);
- $attrCode = $this->getAttribute()->getAttributeCode();
- $mediaGalleryData = $product->getData($attrCode);
- $position = 0;
- if (!is_array($mediaGalleryData)) {
- $mediaGalleryData = ['images' => []];
- }
- foreach ($mediaGalleryData['images'] as &$image) {
- if (isset($image['position']) && $image['position'] > $position) {
- $position = $image['position'];
- }
- }
- $position++;
- unset($videoData['file']);
- $mediaGalleryData['images'][] = array_merge([
- 'file' => $fileName,
- 'label' => $videoData['video_title'],
- 'position' => $position,
- 'disabled' => (int)$exclude
- ], $videoData);
- $product->setData($attrCode, $mediaGalleryData);
- if ($mediaAttribute !== null) {
- $product->setMediaAttribute($product, $mediaAttribute, $fileName);
- }
- $this->createHandler->execute($product);
- return $fileName;
- }
- }
- <?php
- $_helper = $this->helper('MagentoCatalogHelperOutput');
- $objectManager = MagentoFrameworkAppObjectManager::getInstance();
- $_product = $objectManager->get('MagentoFrameworkRegistry')->registry('current_product');//get current product
- ?>
- <?php if($proVideo = $_helper->productAttribute($_product,$_product->getVideoName(), 'video_name')):?>
- <?php $videoUrl = $this->getUrl('pub/media/catalog/product/videos/').$proVideo; ?>
- <div class="prod_video_bg">
- <video width="400" controls>
- <source src="<?php echo $videoUrl; ?>" type="video/mp4">
- </video>
- </div>
- <?php endif; ?>
Add Comment
Please, Sign In to add comment