1

I'm trying to override native block Magento\Catalog\Block\Product\View.php, because i want to render my custom(copied) template addtocart.phtml and use my custom methods on this template.

Here is my DI - Wodev\Guestcheck\etc\di.xml

<?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="Wodev\Guestcheck\Block\Product\View" />
</config>

Here is my block - Wodev\Guestcheck\Block\Product\View.php

    <?php

namespace Wodev\Guestcheck\Block\Product;

use Magento\Catalog\Api\ProductRepositoryInterface;

class View extends \Magento\Catalog\Block\Product\View
{
    private $_helper;

    public function __construct(
        \Wodev\Guestcheck\Helper\Data $helper,
        \Magento\Catalog\Block\Product\Context $context,
        \Magento\Framework\Url\EncoderInterface $urlEncoder,
        \Magento\Framework\Json\EncoderInterface $jsonEncoder,
        \Magento\Framework\Stdlib\StringUtils $string,
        \Magento\Catalog\Helper\Product $productHelper,
        \Magento\Catalog\Model\ProductTypes\ConfigInterface $productTypeConfig,
        \Magento\Framework\Locale\FormatInterface $localeFormat,
        \Magento\Customer\Model\Session $customerSession,
        ProductRepositoryInterface $productRepository,
        \Magento\Framework\Pricing\PriceCurrencyInterface $priceCurrency,
        array $data = []
    ) {
        $this->_helper = $helper;
        parent::__construct($context,$urlEncoder,$jsonEncoder,$string,$productHelper,$productTypeConfig,$localeFormat,$customerSession,$productRepository,$priceCurrency,$data);
    }
}

Here is my layout - Wodev\Guestcheck\view\frontend\layout\catalog_product_view.xml

<page layout="1column" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <body>
        <referenceContainer name="product.info.form.content">
            <referenceBlock name="product.info.addtocart" remove="true" />
            <block class="Wodev\Guestcheck\Block\Product\View" name="custom.addtocart" template="Wodev_Guestcheck::product/view/addtocart.phtml"/>
        </referenceContainer>
    </body>
</page>

My template is not rendering. Please,help to find out why?

1 Answer 1

1

You dont need to override core block.

Simply put this xml format in your catalog_product_view.xml file,

<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="1column" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <body>
        <referenceBlock name="product.info.addtocart">
            <action method="setTemplate">
                <argument name="template" xsi:type="string">Wodev_Guestcheck::product/view/addtocart.phtml</argument>
            </action>
        </referenceBlock>
        <referenceBlock name="product.info.addtocart.additional">
            <action method="setTemplate">
                <argument name="template" xsi:type="string">Wodev_Guestcheck::product/view/addtocart.phtml</argument>
            </action>
        </referenceBlock>
    </body>
</page>
4
  • I'm going to realize custom methods, which i want to use in template
    – Alkos
    Commented Jul 4, 2017 at 8:09
  • 1
    and as i know, action methods in layouts are deprecated
    – Alkos
    Commented Jul 4, 2017 at 8:23
  • As Alkos said - <action> has been deprecated, <arguments> should be used where possible - devdocs.magento.com/guides/v2.0/frontend-dev-guide/layouts/…
    – Ben Crook
    Commented Jul 4, 2017 at 8:55
  • So, how can i render my template using overriding block??? I want to make some methods on it, which i want to use on custom template
    – Alkos
    Commented Jul 4, 2017 at 9:24

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.