How to get product video in product listing page in magento 2?












3















How to get product video as base and thumbnail in product listing page. Currently, Only Video preview image is showing in listing page. But I want to display video in listing page instead of the preview image.










share|improve this question



























    3















    How to get product video as base and thumbnail in product listing page. Currently, Only Video preview image is showing in listing page. But I want to display video in listing page instead of the preview image.










    share|improve this question

























      3












      3








      3








      How to get product video as base and thumbnail in product listing page. Currently, Only Video preview image is showing in listing page. But I want to display video in listing page instead of the preview image.










      share|improve this question














      How to get product video as base and thumbnail in product listing page. Currently, Only Video preview image is showing in listing page. But I want to display video in listing page instead of the preview image.







      magento2 product-list product-video






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Dec 17 '18 at 9:23









      Aniket PrajapatiAniket Prajapati

      627




      627






















          1 Answer
          1






          active

          oldest

          votes


















          3














          You can get Media collection of particular product by loading it in custom helper as below:



          <?php
          namespace YourModuleHelper;

          use MagentoCatalogApiProductRepositoryInterface;

          class Data extends MagentoFrameworkUrlHelperData
          {
          public function __construct(
          ...
          ProductRepositoryInterface $productRepository,
          ...
          ) {
          ...
          $this->productRepository = $productRepository;
          ...
          }

          public function getMediaGalleryImages($product)
          {
          $_product = $this->productRepository->get($product->getSku(), false, null, true);

          return $_product->getMediaGalleryImages();
          }
          }


          Please copy




          vendor/magento/module-catalog/view/frontend/templates/product/list.phtml




          to your custom theme.




          app/design/frontend/package/theme/Magento_Catalog/templates/product/list.phtml




          Now in list.phtml file, you can call this function to load the video url if it's available otherwise show the regular image:



          <?php
          $video = false;
          $galleryImages = $this->helper('YourModuleHelperData')->getMediaGalleryImages($_product);
          if ($galleryImages) {
          foreach ($galleryImages as $primage) {
          $imageData = $primage->getData();
          // Check media type
          if (isset($imageData['media_type']) && $imageData['media_type'] == 'external-video') {
          $video = true;
          if (strpos($imageData['video_url'], 'youtube.com') !== false) {
          echo preg_replace("/s*[a-zA-Z//:.]*youtube.com/watch?v=([a-zA-Z0-9-_]+)([a-zA-Z0-9/*-_?&;%=.]*)/i","<iframe width="240" height="300" src="//www.youtube.com/embed/$1" frameborder="0" allowfullscreen></iframe>", $imageData['video_url']);
          }elseif(strpos($imageData['video_url'], 'vimeo.com') !== false){
          echo "<iframe width="240" height="300" src=".$imageData['video_url']." frameborder="0" allowfullscreen></iframe>";
          }
          }
          }
          }
          ?>

          <?php if(!$video){ ?>
          <?= $productImage->toHtml() ?>
          <?php } ?>


          For vimeo, add embedded urls for product video to make them playable at list page. Please note that it would take some extra time to load page first time as we are loading product to get media collection.






          share|improve this answer


























          • Thank you himmat, Its working well for my case.

            – Aniket Prajapati
            Dec 18 '18 at 9:33











          • Great! Happy to help :)

            – Himmat Paliwal
            Dec 18 '18 at 9:42











          Your Answer








          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "479"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: false,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f254855%2fhow-to-get-product-video-in-product-listing-page-in-magento-2%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          3














          You can get Media collection of particular product by loading it in custom helper as below:



          <?php
          namespace YourModuleHelper;

          use MagentoCatalogApiProductRepositoryInterface;

          class Data extends MagentoFrameworkUrlHelperData
          {
          public function __construct(
          ...
          ProductRepositoryInterface $productRepository,
          ...
          ) {
          ...
          $this->productRepository = $productRepository;
          ...
          }

          public function getMediaGalleryImages($product)
          {
          $_product = $this->productRepository->get($product->getSku(), false, null, true);

          return $_product->getMediaGalleryImages();
          }
          }


          Please copy




          vendor/magento/module-catalog/view/frontend/templates/product/list.phtml




          to your custom theme.




          app/design/frontend/package/theme/Magento_Catalog/templates/product/list.phtml




          Now in list.phtml file, you can call this function to load the video url if it's available otherwise show the regular image:



          <?php
          $video = false;
          $galleryImages = $this->helper('YourModuleHelperData')->getMediaGalleryImages($_product);
          if ($galleryImages) {
          foreach ($galleryImages as $primage) {
          $imageData = $primage->getData();
          // Check media type
          if (isset($imageData['media_type']) && $imageData['media_type'] == 'external-video') {
          $video = true;
          if (strpos($imageData['video_url'], 'youtube.com') !== false) {
          echo preg_replace("/s*[a-zA-Z//:.]*youtube.com/watch?v=([a-zA-Z0-9-_]+)([a-zA-Z0-9/*-_?&;%=.]*)/i","<iframe width="240" height="300" src="//www.youtube.com/embed/$1" frameborder="0" allowfullscreen></iframe>", $imageData['video_url']);
          }elseif(strpos($imageData['video_url'], 'vimeo.com') !== false){
          echo "<iframe width="240" height="300" src=".$imageData['video_url']." frameborder="0" allowfullscreen></iframe>";
          }
          }
          }
          }
          ?>

          <?php if(!$video){ ?>
          <?= $productImage->toHtml() ?>
          <?php } ?>


          For vimeo, add embedded urls for product video to make them playable at list page. Please note that it would take some extra time to load page first time as we are loading product to get media collection.






          share|improve this answer


























          • Thank you himmat, Its working well for my case.

            – Aniket Prajapati
            Dec 18 '18 at 9:33











          • Great! Happy to help :)

            – Himmat Paliwal
            Dec 18 '18 at 9:42
















          3














          You can get Media collection of particular product by loading it in custom helper as below:



          <?php
          namespace YourModuleHelper;

          use MagentoCatalogApiProductRepositoryInterface;

          class Data extends MagentoFrameworkUrlHelperData
          {
          public function __construct(
          ...
          ProductRepositoryInterface $productRepository,
          ...
          ) {
          ...
          $this->productRepository = $productRepository;
          ...
          }

          public function getMediaGalleryImages($product)
          {
          $_product = $this->productRepository->get($product->getSku(), false, null, true);

          return $_product->getMediaGalleryImages();
          }
          }


          Please copy




          vendor/magento/module-catalog/view/frontend/templates/product/list.phtml




          to your custom theme.




          app/design/frontend/package/theme/Magento_Catalog/templates/product/list.phtml




          Now in list.phtml file, you can call this function to load the video url if it's available otherwise show the regular image:



          <?php
          $video = false;
          $galleryImages = $this->helper('YourModuleHelperData')->getMediaGalleryImages($_product);
          if ($galleryImages) {
          foreach ($galleryImages as $primage) {
          $imageData = $primage->getData();
          // Check media type
          if (isset($imageData['media_type']) && $imageData['media_type'] == 'external-video') {
          $video = true;
          if (strpos($imageData['video_url'], 'youtube.com') !== false) {
          echo preg_replace("/s*[a-zA-Z//:.]*youtube.com/watch?v=([a-zA-Z0-9-_]+)([a-zA-Z0-9/*-_?&;%=.]*)/i","<iframe width="240" height="300" src="//www.youtube.com/embed/$1" frameborder="0" allowfullscreen></iframe>", $imageData['video_url']);
          }elseif(strpos($imageData['video_url'], 'vimeo.com') !== false){
          echo "<iframe width="240" height="300" src=".$imageData['video_url']." frameborder="0" allowfullscreen></iframe>";
          }
          }
          }
          }
          ?>

          <?php if(!$video){ ?>
          <?= $productImage->toHtml() ?>
          <?php } ?>


          For vimeo, add embedded urls for product video to make them playable at list page. Please note that it would take some extra time to load page first time as we are loading product to get media collection.






          share|improve this answer


























          • Thank you himmat, Its working well for my case.

            – Aniket Prajapati
            Dec 18 '18 at 9:33











          • Great! Happy to help :)

            – Himmat Paliwal
            Dec 18 '18 at 9:42














          3












          3








          3







          You can get Media collection of particular product by loading it in custom helper as below:



          <?php
          namespace YourModuleHelper;

          use MagentoCatalogApiProductRepositoryInterface;

          class Data extends MagentoFrameworkUrlHelperData
          {
          public function __construct(
          ...
          ProductRepositoryInterface $productRepository,
          ...
          ) {
          ...
          $this->productRepository = $productRepository;
          ...
          }

          public function getMediaGalleryImages($product)
          {
          $_product = $this->productRepository->get($product->getSku(), false, null, true);

          return $_product->getMediaGalleryImages();
          }
          }


          Please copy




          vendor/magento/module-catalog/view/frontend/templates/product/list.phtml




          to your custom theme.




          app/design/frontend/package/theme/Magento_Catalog/templates/product/list.phtml




          Now in list.phtml file, you can call this function to load the video url if it's available otherwise show the regular image:



          <?php
          $video = false;
          $galleryImages = $this->helper('YourModuleHelperData')->getMediaGalleryImages($_product);
          if ($galleryImages) {
          foreach ($galleryImages as $primage) {
          $imageData = $primage->getData();
          // Check media type
          if (isset($imageData['media_type']) && $imageData['media_type'] == 'external-video') {
          $video = true;
          if (strpos($imageData['video_url'], 'youtube.com') !== false) {
          echo preg_replace("/s*[a-zA-Z//:.]*youtube.com/watch?v=([a-zA-Z0-9-_]+)([a-zA-Z0-9/*-_?&;%=.]*)/i","<iframe width="240" height="300" src="//www.youtube.com/embed/$1" frameborder="0" allowfullscreen></iframe>", $imageData['video_url']);
          }elseif(strpos($imageData['video_url'], 'vimeo.com') !== false){
          echo "<iframe width="240" height="300" src=".$imageData['video_url']." frameborder="0" allowfullscreen></iframe>";
          }
          }
          }
          }
          ?>

          <?php if(!$video){ ?>
          <?= $productImage->toHtml() ?>
          <?php } ?>


          For vimeo, add embedded urls for product video to make them playable at list page. Please note that it would take some extra time to load page first time as we are loading product to get media collection.






          share|improve this answer















          You can get Media collection of particular product by loading it in custom helper as below:



          <?php
          namespace YourModuleHelper;

          use MagentoCatalogApiProductRepositoryInterface;

          class Data extends MagentoFrameworkUrlHelperData
          {
          public function __construct(
          ...
          ProductRepositoryInterface $productRepository,
          ...
          ) {
          ...
          $this->productRepository = $productRepository;
          ...
          }

          public function getMediaGalleryImages($product)
          {
          $_product = $this->productRepository->get($product->getSku(), false, null, true);

          return $_product->getMediaGalleryImages();
          }
          }


          Please copy




          vendor/magento/module-catalog/view/frontend/templates/product/list.phtml




          to your custom theme.




          app/design/frontend/package/theme/Magento_Catalog/templates/product/list.phtml




          Now in list.phtml file, you can call this function to load the video url if it's available otherwise show the regular image:



          <?php
          $video = false;
          $galleryImages = $this->helper('YourModuleHelperData')->getMediaGalleryImages($_product);
          if ($galleryImages) {
          foreach ($galleryImages as $primage) {
          $imageData = $primage->getData();
          // Check media type
          if (isset($imageData['media_type']) && $imageData['media_type'] == 'external-video') {
          $video = true;
          if (strpos($imageData['video_url'], 'youtube.com') !== false) {
          echo preg_replace("/s*[a-zA-Z//:.]*youtube.com/watch?v=([a-zA-Z0-9-_]+)([a-zA-Z0-9/*-_?&;%=.]*)/i","<iframe width="240" height="300" src="//www.youtube.com/embed/$1" frameborder="0" allowfullscreen></iframe>", $imageData['video_url']);
          }elseif(strpos($imageData['video_url'], 'vimeo.com') !== false){
          echo "<iframe width="240" height="300" src=".$imageData['video_url']." frameborder="0" allowfullscreen></iframe>";
          }
          }
          }
          }
          ?>

          <?php if(!$video){ ?>
          <?= $productImage->toHtml() ?>
          <?php } ?>


          For vimeo, add embedded urls for product video to make them playable at list page. Please note that it would take some extra time to load page first time as we are loading product to get media collection.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Dec 18 '18 at 9:26

























          answered Dec 17 '18 at 11:47









          Himmat PaliwalHimmat Paliwal

          1,145418




          1,145418













          • Thank you himmat, Its working well for my case.

            – Aniket Prajapati
            Dec 18 '18 at 9:33











          • Great! Happy to help :)

            – Himmat Paliwal
            Dec 18 '18 at 9:42



















          • Thank you himmat, Its working well for my case.

            – Aniket Prajapati
            Dec 18 '18 at 9:33











          • Great! Happy to help :)

            – Himmat Paliwal
            Dec 18 '18 at 9:42

















          Thank you himmat, Its working well for my case.

          – Aniket Prajapati
          Dec 18 '18 at 9:33





          Thank you himmat, Its working well for my case.

          – Aniket Prajapati
          Dec 18 '18 at 9:33













          Great! Happy to help :)

          – Himmat Paliwal
          Dec 18 '18 at 9:42





          Great! Happy to help :)

          – Himmat Paliwal
          Dec 18 '18 at 9:42


















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Magento Stack Exchange!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f254855%2fhow-to-get-product-video-in-product-listing-page-in-magento-2%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Bressuire

          Cabo Verde

          Gyllenstierna