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











up vote
3
down vote

favorite












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


























    up vote
    3
    down vote

    favorite












    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
























      up vote
      3
      down vote

      favorite









      up vote
      3
      down vote

      favorite











      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 4 hours ago









      Aniket Prajapati

      507




      507






















          1 Answer
          1






          active

          oldest

          votes

















          up vote
          3
          down vote













          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 $image) {
          $imageData = $image->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























            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',
            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








            up vote
            3
            down vote













            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 $image) {
            $imageData = $image->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



























              up vote
              3
              down vote













              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 $image) {
              $imageData = $image->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

























                up vote
                3
                down vote










                up vote
                3
                down vote









                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 $image) {
                $imageData = $image->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 $image) {
                $imageData = $image->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 54 mins ago

























                answered 2 hours ago









                Himmat Paliwal

                1,118417




                1,118417






























                    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.





                    Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                    Please pay close attention to the following guidance:


                    • 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

                    flock() on closed filehandle LOCK_FILE at /usr/bin/apt-mirror

                    Mangá

                    Eduardo VII do Reino Unido