1

I want override Block Magento\Catalog\Block\Product\View\Gallery this is code in file di.xml

Edited

 <?xml version="1.0"?>
    <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/ObjectManager/etc/config.xsd">
        <preference for="Magento\Catalog\Block\Product\View\Gallery" type="Xyaddons\Productzoom\Block\Product\View\Gallery" />
    </config>

Code file Gallery.php

    namespace Xyaddons\Productzoom\Block\Product\View;
    use Magento\Framework\View\Element\Template;

    class Gallery extends \Magento\Catalog\Block\Product\View\Gallery
    {
        protected function _toHtml()
        {
            $this->setModuleName($this->extractModuleName('Magento\Catalog\Block\Product\View\Gallery'));
            return parent::_toHtml();
        }

       /**
       * @var \Magento\Framework\App\Config\ScopeConfigInterface
       */
       protected $_scopeConfig;

       /**
         * Template image only
         *
         * @var string
         */
        protected $_templateImage = 'Xyaddons_Productzoom::product/image.phtml';

      protected function _prepareLayout()
        {
        $this->getLayout()->createBlock('Xyaddons\Productzoom\Block\Product\View\Gallery', 'xyaddons.productzoom')->setTemplate('Xyaddons_Productzoom::produtc/image.phtml');}
}

when run code don't running image.phtml and not override Block Magento\Catalog\Block\Product\View\Gallery. What wrong?

1

Did you removed cache?

Why your extended class \Magento\Catalog\Block\Product\Gallery but try override \Magento\Catalog\Block\Product\View\Gallery? (I think you have error in class for in preferences)

Why you don't rewrite block <block class="Magento\Catalog\Block\Product\View\Gallery" name="product.info.media.image" template="product/view/gallery.phtml"/> and try override class?

  • Thanks @antonohorodnyk. i removed cache and edit extended class in my question. Why you don't rewrite block....? Because I want create a module. when setup new site i don't need find block and rewrite it. – xanka Nov 4 '15 at 1:40
0

use plugin to override the block or model ex in di.xml (i took ex of block, model and controller of product )

<?xml version="1.0"?>

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/ObjectManager/etc/config.xsd">

    <type name="Magento\Catalog\Block\Product\View">
        <plugin name="inroduct-custom-module" type="Sugarcode\Test\Model\Plugin\Product" sortOrder="1"/>
    </type>

    <type name="Magento\Catalog\Model\Product">
        <plugin name="getname-test-module" type="Sugarcode\Test\Model\Plugin\Product" sortOrder="10"/>
    </type>
    <type name="Magento\Catalog\Controller\Product\View">
        <plugin name="product-cont-test-module" type="Sugarcode\Test\Model\Plugin\Product" sortOrder="10"/>
    </type>
</config>

in plugin that app\code\Sugarcode\Test\Model\Plugin\Product.php

<?php

namespace Sugarcode\Test\Model\Plugin;

class Product
{        
    public function beforeSetName(\Magento\Catalog\Model\Product $subject, $name)
    {
        return array('(' . $name . ')');
    }

     public function afterGetName(\Magento\Catalog\Model\Product $subject, $result)
    {
        return '|' . $result . '|';
    } 

     public function afterGetLabel($subject, $result)
    {
        $result=$result.' Custom';
        return $result;
    }
    public function aroundExecute(\Magento\Catalog\Controller\Product\View $subject, \Closure $proceed)
    {
        echo 'I Am in Local Controller Before <br>';
        $returnValue = $proceed(); // it get you old function return value
        //$name='#'.$returnValue->getName().'#';
        //$returnValue->setName($name);
        echo 'I Am in Local Controller  After <br>';
        return $returnValue;// if its object make sure it return same object which you addition data
    }


}

this the best way of override file if you use preference then you will miss magento core function at the time of upgrade
i hope it will help you

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.