cancel
Showing results for 
Search instead for 
Did you mean: 

How can i override or extend a block in Magento 2.1

How can i override or extend a block in Magento 2.1

I will extend View.php in htdocs\vendor\magento\module-catalog\Block\Product 

 

My Solution was to make a new Folder Structure in the Folder app, like: htdocs\app\code\Webkul\Hello

 

1. Step was to  make a Folder etc with the di.xml File. 

 

<?xml version="1.0"?>

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <preference for="Magento\Catalog\Block\Product\View" type="Webkul\Hello\Block\Rewrite\Product\View" />
</config>

 

2. Step was the Folder htdocs\app\code\Webkul\Hello\Block\Rewrite\Product there i put the new View.php File. 

<?php
/**
 * Copyright © 2016 Magento. All rights reserved.
 * See COPYING.txt for license details.
 */
namespace Webkul\Hello\Block\Rewrite\Product;


class View extends \Magento\Catalog\Block\Product\View
{
    
    
	public function _getQuantityValidators2()
    {
        $validators = [];
        $validators['required-number'] = true;
		$validators['validate-pack'] = true;
        return $validators;
    }

}

This solution doesn't work and I don't know why. If someone has a idea, pleas let me know.

Tags (3)
1 REPLY

Re: How can i override or extend a block in Magento 2.1

Here is a complete tutorial on overriding Magento 2 Block, helper, Model and controller.

https://www.fmeextensions.com/blog/override-magento-2-helper-block-model-and-controller/