I am working on custom extension and need to override Magento\Catalog\Block\Product\View\Gallery block.
I have followed this method.
In di.xml,
<preference for="Magento\Catalog\Block\Product\View\Gallery" type="[Company]\[Vendor]\Block\Catalog\Product\View\Gallery"/>
In [Company][Vendor]\Block\Catalog\Product\View\Gallery.php
namespace [Company]\[Vendor]\Block\Catalog\Product\View;
class Gallery extends \Magento\Catalog\Block\Product\View\Gallery
{
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(),
'position' => $image->getPosition(),
'isMain' => $this->isMainImage($image),
];
}
if (empty($imagesItems)) {
$imagesItems[] = [
'thumb' => $this->_imageHelper->getDefaultPlaceholderUrl('thumbnail'),
'img' => $this->_imageHelper->getDefaultPlaceholderUrl('image'),
'full' => $this->_imageHelper->getDefaultPlaceholderUrl('image'),
'caption' => '',
'position' => '0',
'isMain' => true,
];
}
return json_encode($imagesItems);
}
}
When I do this, More Views Images of the product is don't show any more.
Can you help me into this?
Thanks in advance.
-
follow this link webkul.com/blog/overriding-block-model-controller-magento2Rajkumar Elumalai– Rajkumar Elumalai2016年09月09日 04:03:37 +00:00Commented Sep 9, 2016 at 4:03
-
Already try that, but when I override this block, more views images on product view page not show any more.Dhiren Vasoya– Dhiren Vasoya2016年09月09日 04:25:53 +00:00Commented Sep 9, 2016 at 4:25
-
update you question what you triedRajkumar Elumalai– Rajkumar Elumalai2016年09月09日 04:57:37 +00:00Commented Sep 9, 2016 at 4:57
-
#Rajkumar .E my questions updated, can you please help me out into this.Dhiren Vasoya– Dhiren Vasoya2016年09月09日 05:13:19 +00:00Commented Sep 9, 2016 at 5:13
-
any error are generated or not?Rakesh Jesadiya– Rakesh Jesadiya2016年09月09日 05:22:34 +00:00Commented Sep 9, 2016 at 5:22
1 Answer 1
I found the solution. I am doing the same thing by using the plugin functionality of magento2.
For that in di.xml file specify plugin.
<type name="Magento\Catalog\Block\Product\View\Gallery">
<plugin name="plugin_block_catalog_product_view_gallery"
type="[Company]\[Vendor]\Block\Catalog\Product\View\Gallery"
sortOrder="10"
disabled="false"/>
</type>
It is better to used.
-
Hi Dhiren, I am trying the plugin method but on my plugin function afterGetGalleryImagesJson(), I want the product data, how can i do that. $this->getProduct(); didn't work. can you help pleaseNavin Bista– Navin Bista2017年02月02日 05:37:05 +00:00Commented Feb 2, 2017 at 5:37
-
@aton1004 Can you tell me what you have try? And what you try to archive?Dhiren Vasoya– Dhiren Vasoya2017年02月02日 05:49:57 +00:00Commented Feb 2, 2017 at 5:49
-
thanks for the reply, actually i wanted product data to check some attribute value before making some changes to the result of gallery , I got it from registryNavin Bista– Navin Bista2017年02月02日 08:33:19 +00:00Commented Feb 2, 2017 at 8:33