Media gallery for product collectionUnit Test for overwrite collection class in magento2Why Getting...

What is the meaning of option 'by' in TikZ Intersections

Convert an array of objects to array of the objects' values

Paper published similar to PhD thesis

The need of reserving one's ability in job interviews

Quitting employee has privileged access to critical information

Why is there an extra space when I type "ls" on the Desktop?

Is there a way to find out the age of climbing ropes?

How to make sure I'm assertive enough in contact with subordinates?

“I had a flat in the centre of town, but I didn’t like living there, so …”

ESPP--any reason not to go all in?

Linear Combination of Atomic Orbitals

Deal the cards to the players

Create chunks from an array

Is every open circuit a capacitor?

Can inspiration allow the Rogue to make a Sneak Attack?

Should I use HTTPS on a domain that will only be used for redirection?

When to use the term transposed instead of modulation?

The past tense for the quoting particle って

What is better: yes / no radio, or simple checkbox?

How to write a chaotic neutral protagonist and prevent my readers from thinking they are evil?

How to chmod files that have a specific set of permissions

A bug in Excel? Conditional formatting for marking duplicates also highlights unique value

I can't die. Who am I?

Are angels creatures (Mark 16:15) and can they repent (Rev 2:5 and Rom 8:21)



Media gallery for product collection


Unit Test for overwrite collection class in magento2Why Getting categories and names on product view page Magento 2 fails?Magento 2.1 Create a filter in the product grid by new attributeMagento 2 Add new field to Magento_User admin formMagento 2: Add a product to the cart programmaticallyMagento offline custom Payment method with drop down listAdding gallery images to category listMagento 2 Create dynamic array From different Model Collection to use in multi select in gridI have created one field using product form field for my price i want save my field value at product creation time from backend magento2Magento 2.3 Can't view module's front end page output?













0















I'm trying to add an alternative image for all config product images. To do so I extended MagentoCatalogBlockProductImage. For some reason $product->getMediagalleryImages() always returns the same gallery for all products in the collection though. Is this the correct way of loading a media gallery for a product in M2?



<?php                                                                               
namespace AcCatalogBlockProduct;

use MagentoFrameworkViewElementTemplateContext;
use MagentoCatalogModelProduct;
use MagentoCatalogModelProductGalleryReadHandler;


class Image extends MagentoCatalogBlockProductImage
{
protected $_productModel;
protected $_galleryReadHandler;

public function __construct(
Context $context,
Product $productModel,
ReadHandler $galleryReadHandler,
array $data = []
) {
if (isset($data['template'])) {
$this->setTemplate($data['template']);
unset($data['template']);
}

parent::__construct($context, $data);

$this->_productModel = $productModel;
$this->_galleryReadHandler = $galleryReadHandler;
}

public function getSrcAlt(): string
{
$product = $this->_productModel->load($this->getProductId());
if (strpos($product->getTypeId(), 'configurable') === false) {
return '';
}

$i = 0;
$this->_galleryReadHandler->execute($product);
foreach ($product->getMediaGalleryImages() as $image) {
if (++$i < 2) continue;
return sprintf('data-src-alt="%s"', $image['url']);
}

return '';
}
}








share



























    0















    I'm trying to add an alternative image for all config product images. To do so I extended MagentoCatalogBlockProductImage. For some reason $product->getMediagalleryImages() always returns the same gallery for all products in the collection though. Is this the correct way of loading a media gallery for a product in M2?



    <?php                                                                               
    namespace AcCatalogBlockProduct;

    use MagentoFrameworkViewElementTemplateContext;
    use MagentoCatalogModelProduct;
    use MagentoCatalogModelProductGalleryReadHandler;


    class Image extends MagentoCatalogBlockProductImage
    {
    protected $_productModel;
    protected $_galleryReadHandler;

    public function __construct(
    Context $context,
    Product $productModel,
    ReadHandler $galleryReadHandler,
    array $data = []
    ) {
    if (isset($data['template'])) {
    $this->setTemplate($data['template']);
    unset($data['template']);
    }

    parent::__construct($context, $data);

    $this->_productModel = $productModel;
    $this->_galleryReadHandler = $galleryReadHandler;
    }

    public function getSrcAlt(): string
    {
    $product = $this->_productModel->load($this->getProductId());
    if (strpos($product->getTypeId(), 'configurable') === false) {
    return '';
    }

    $i = 0;
    $this->_galleryReadHandler->execute($product);
    foreach ($product->getMediaGalleryImages() as $image) {
    if (++$i < 2) continue;
    return sprintf('data-src-alt="%s"', $image['url']);
    }

    return '';
    }
    }








    share

























      0












      0








      0








      I'm trying to add an alternative image for all config product images. To do so I extended MagentoCatalogBlockProductImage. For some reason $product->getMediagalleryImages() always returns the same gallery for all products in the collection though. Is this the correct way of loading a media gallery for a product in M2?



      <?php                                                                               
      namespace AcCatalogBlockProduct;

      use MagentoFrameworkViewElementTemplateContext;
      use MagentoCatalogModelProduct;
      use MagentoCatalogModelProductGalleryReadHandler;


      class Image extends MagentoCatalogBlockProductImage
      {
      protected $_productModel;
      protected $_galleryReadHandler;

      public function __construct(
      Context $context,
      Product $productModel,
      ReadHandler $galleryReadHandler,
      array $data = []
      ) {
      if (isset($data['template'])) {
      $this->setTemplate($data['template']);
      unset($data['template']);
      }

      parent::__construct($context, $data);

      $this->_productModel = $productModel;
      $this->_galleryReadHandler = $galleryReadHandler;
      }

      public function getSrcAlt(): string
      {
      $product = $this->_productModel->load($this->getProductId());
      if (strpos($product->getTypeId(), 'configurable') === false) {
      return '';
      }

      $i = 0;
      $this->_galleryReadHandler->execute($product);
      foreach ($product->getMediaGalleryImages() as $image) {
      if (++$i < 2) continue;
      return sprintf('data-src-alt="%s"', $image['url']);
      }

      return '';
      }
      }








      share














      I'm trying to add an alternative image for all config product images. To do so I extended MagentoCatalogBlockProductImage. For some reason $product->getMediagalleryImages() always returns the same gallery for all products in the collection though. Is this the correct way of loading a media gallery for a product in M2?



      <?php                                                                               
      namespace AcCatalogBlockProduct;

      use MagentoFrameworkViewElementTemplateContext;
      use MagentoCatalogModelProduct;
      use MagentoCatalogModelProductGalleryReadHandler;


      class Image extends MagentoCatalogBlockProductImage
      {
      protected $_productModel;
      protected $_galleryReadHandler;

      public function __construct(
      Context $context,
      Product $productModel,
      ReadHandler $galleryReadHandler,
      array $data = []
      ) {
      if (isset($data['template'])) {
      $this->setTemplate($data['template']);
      unset($data['template']);
      }

      parent::__construct($context, $data);

      $this->_productModel = $productModel;
      $this->_galleryReadHandler = $galleryReadHandler;
      }

      public function getSrcAlt(): string
      {
      $product = $this->_productModel->load($this->getProductId());
      if (strpos($product->getTypeId(), 'configurable') === false) {
      return '';
      }

      $i = 0;
      $this->_galleryReadHandler->execute($product);
      foreach ($product->getMediaGalleryImages() as $image) {
      if (++$i < 2) continue;
      return sprintf('data-src-alt="%s"', $image['url']);
      }

      return '';
      }
      }






      magento2 gallery





      share












      share










      share



      share










      asked 8 mins ago









      Michael ThesselMichael Thessel

      1337




      1337






















          0






          active

          oldest

          votes











          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%2f264931%2fmedia-gallery-for-product-collection%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          0






          active

          oldest

          votes








          0






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes
















          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%2f264931%2fmedia-gallery-for-product-collection%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

          迭戈·戈丁...

          A phrase ”follow into" in a context The 2019 Stack Overflow Developer Survey Results Are...

          1960s short story making fun of James Bond-style spy fiction The 2019 Stack Overflow Developer...