1

I would like to override /vendor/magento/module-catalog/Block/Product/View/Gallery.php file getGalleryImagesJson function

In result I would like to get -

public function getGalleryImagesJson()
{
    $imagesItems = [];
    foreach ($this->getGalleryImages() as $image) {
        $imagesItems[] = [
            'thumb' => $image->getData('small_image_url'),
            'img' => $image->getData('medium_image_url'),
            'full' => $image->getData('large_image_url'),
            'caption' => ($image->getLabel() ?: $this->getProduct()->getName()),
            'position' => $image->getPosition(),
            'isMain' => $this->isMainImage($image),
            'type' => str_replace('external-', '', $image->getMediaType()),
            'videoUrl' => $image->getVideoUrl(),
            'video' => $image->getVideoUrl(), //This is new
        ];
    }
    if (empty($imagesItems)) {
        $imagesItems[] = [
            'thumb' => $this->_imageHelper->getDefaultPlaceholderUrl('thumbnail'),
            'img' => $this->_imageHelper->getDefaultPlaceholderUrl('image'),
            'full' => $this->_imageHelper->getDefaultPlaceholderUrl('image'),
            'caption' => '',
            'position' => '0',
            'isMain' => true,
            'type' => 'image',
            'videoUrl' => null,
            'video' => null, //This is new
        ];
    }
    return json_encode($imagesItems);
}

How can I do that? I've tried using preference but then thumbnails doesn't show anymore and I've also tried using plugins but can't really understand how to override it using plugin...

Thank you for any help!

  • your overwritten class has to extend the class you want to overwrite – Philipp Sander Jan 21 at 13:03
  • @PhilippSander yes and if I'm using preference I extend the class I want to overwrite, clean cache but my image thumnbails doesn't show up anymore and no errors are thrown – help Jan 21 at 13:05
  • Override not working because the function you overriding has implemented some private function "getGalleryImages()" as well. – Amit Naraniwal Jan 22 at 10:41
0

I have strongly recommended to the plugin.The plugin is the best way to modify the output of a public function.

In this case, we can use after and around plugin.

Create di.xml at app/code/{Vendorname}/{Modulename}/etc/frontend/ and code will be like:

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <type name="Magento\Catalog\Block\Product\View\Gallery">
        <plugin name="add_new_column_to_placeholder" 
type="{VendorName}{ModuleName}\Plugin\Magento\Catalog\Block\Product\View\GalleryPlugin" sortOrder="1" />
    </type>
</config>

For your requirement, you can use after plugin:

Create GalleryPlugin.php as per as di.xml AT {VendorName}{ModuleName}\Plugin\Magento\Catalog\Block\Product\View

And Code:

<?php
namespace StackExchanges\OnlyMagento\Plugin\Magento\Catalog\Block\Product\View;

class GalleryPlugin {

    /**
     * @var \Magento\Catalog\Helper\Image
     */
    private $_imageHelper;

    public function __construct(
    \Magento\Catalog\Helper\Image $_imageHelper
    ) {

        $this->_imageHelper = $_imageHelper;
    } 
   public function afterGetGalleryImagesJson(
    \Magento\Catalog\Block\Product\View\Gallery $subject,// origin class
    $output //Result of orional function         
   )
   {
       $result = json_decode($output);
       // if $result size  > 1 means$output does not has place holder
       // then return original put


       if(count($result) > 1 || (count($result) < 1 ))
       {
           return $output;
       }

       // If $result == 1 means it has one image or place holder image
       $placeHolderThub = $this->_imageHelper-getDefaultPlaceholderUrl('thumbnail');
       //match image with place holder     
       if(isset($result[0]['thumb']) && ($result[0]['thumb'] == $placeHolderThub))
       {
           $result[0]['thumb']['video'] =   null ; //This is new
       }

       return json_encode($result);
   }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.