I Need to display product image gallery to my custom .phtml file so I can display it anywhere. I tried to get block using below code

$block = $objectManager->create('\Magento\Catalog\Block\Product\View\Gallery');

But then some jquery error comes. like "setoptions" not defined.

Please give me a perfect solution for this.


1 Answer 1


Please check with below url -

first you need create you own module in magento 2 (you can search in google) , in ibnab we created extension with name Ibnab_CloudZoomy , Magento 2 system use the Block Magento\Catalog\Block\Product\View\Gallery inside Magento_Catalog Module , and if you go to vendor/magento/module-catalog/view/frontend/layout/catalog_product_view.xml you can find the related template of this block :

<container name="product.info.media" htmlTag="div" htmlClass="product media" after="product.info.main">
            <block class="Magento\Catalog\Block\Product\View\Gallery" name="product.info.media.image" template="product/view/gallery.phtml"/>

Yes we rewrite this block and push our custom template , the easy technique is to use (app/code/Ibnab/CloudZoomy/etc/frontend/di.xml) :

Yeah now we can create our own block class and push own template , you can copy past Magento\Catalog\Block\Product\View\Gallery inside Ibnab\CloudZoomy\Block\Product\View\Gallery and add attribute :

protected $_template = 'Ibnab_CloudZoomy::product/view/gallery.phtml';

Now the block system is rewrited and related to our template , we can customise function of block or content of template , of create template inside :


you can get content of this template from original template in vendor/magento/module-catalog/view/frontend/templates/product/view/gallery.phtml . I cloud zoom extesnion we use the file cloud-zoom.1.0.2.js we added to path app/code/Ibnab/CloudZoomy/view/frontend/web/js/cloud-zoom.1.0.2.js and we pushed inside requirejs-config.js in path app/code/Ibnab/CloudZoomy/view/frontend/requirejs-config.js

var config = {
    map: {
        '*': {
          ibnabzoom:       'Ibnab_CloudZoomy/js/cloud-zoom.1.0.2'

Yes for using in gallery.phtml :

$typeGallery = $this->getConfigValue('ibnab_cloud_config/general/enabledisable');
$jsonGallery =  $block->getGalleryImagesJson();
$arrayJsonGallery = json_decode($jsonGallery, true);
<div class="gallery-placeholder">
              if(isset($arrayJsonGallery[0]) && isset($arrayJsonGallery[0]['img']) && isset($arrayJsonGallery[0]['full'])):
        <a href="<?php echo $arrayJsonGallery[0]['full']; ?>"  class = 'cloud-zoom' id='zoom1'rel=""  >
            <img src="<?php echo $arrayJsonGallery[0]['img'];?>"  title="<?php echo $arrayJsonGallery[0]['caption']?>"  />
<div class="more-views">
    <?php $i=0 ?>
   <?php foreach ($arrayJsonGallery as $_image): ?>
         <a href='<?php echo $_image['full']; ?>' class='cloud-zoom-gallery' title='Thumbnail <?php echo $i=$i+1; ?>'
         rel="useZoom: 'zoom1', smallImage: '<?php echo $_image['img']; ?>' ">
        <img src="<?php echo $_image['thumb']; ?>" alt = "<?php echo $_image['caption'] ?>"/></a>
    <?php endforeach; ?>
            ], function ($, ibnabzoom, mage, gallery) {    


you can add configuration or anything you want to more customise your image zoom inside product page view .


i hope its very helpful to you.

  • 2
    This is for a custom module. Thanks anyway. Jul 8, 2019 at 6:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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