1

In the layout file app/code/Magento/Checkout/view/frontend/layout/default.xml

<block class="Magento\Checkout\Block\Cart\Sidebar" name="minicart" as="minicart" after="logo" template="cart/minicart.phtml">

I would like to add a new method to block Sidebar.php.

Then call in the template and the minicart.phtml.

I found in app/code/Magento/Customer/etc/frontend/di.xml:

<type name="Magento\Checkout\Block\Cart\Sidebar">
    <plugin name="customer_cart" type="Magento\Customer\Model\Cart\ConfigPlugin" />
</type>

so I cant't use preference for Magento\Checkout\Block\Cart\Sidebar to add function in Sidebar.php.

Preference didn't work: app/code/Vendor/Modulename/etc/di.xml

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <preference for="Magento\Checkout\Block\Cart\Sidebar" type="Vendor\Modulename\Block\Cart\Sidebar" />
</config>

block file Vendor\Modulename\Block\Cart\Sidebar

class Sidebar extends \Magento\Checkout\Block\Cart\Sidebar
{
    protected $_logger;

    public function __construct(
        \Magento\Framework\View\Element\Template\Context $context,
        \Magento\Customer\Model\Session $customerSession,
        \Magento\Checkout\Model\Session $checkoutSession,
        \Magento\Catalog\Helper\Image $imageHelper,
        \Magento\Customer\CustomerData\JsLayoutDataProviderPoolInterface $jsLayoutDataProvider,
        \Psr\Log\LoggerInterface $logger,
        array $data
    ){
        $this->_logger = $logger;
        parent::__construct($context, $customerSession, $checkoutSession, $imageHelper, $jsLayoutDataProvider, $data);
    }

    public function getCheckoutUrl()
    {
        $this->_logger->addDebug("AAAAAAAAAAA");
        return parent::getCheckoutUrl(); // TODO: Change the autogenerated stub
    }
}

In var/log/debug.log, there is nothing to print

2 Answers 2

1

You have to first override block for add new function in sidebar.php file.

You cant use plugin method for add new function in block file. You can use plugin method to overrider already defined public function in file.

You must have to overrider sidebar.php file and add new function inside sidebar.php file after adding function you can call those function inside your template file.

under

Vendor/Modulename/etc/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\Checkout\Block\Cart\Sidebar" type="Vendor/Modulename/Block/Cart/Sidebar" />

</config>

Inside sidebar.php file,

Vendor/Modulename/Block/Cart/Sidebar.php

you can define your new function.

3
  • I edited the question, preference didn't work for me
    – year
    Commented Jul 25, 2016 at 8:23
  • you have to remove var folder and remove plugin declaration from xml Commented Jul 25, 2016 at 8:38
  • I remove the var folder and plugin declaration is core module,like app/code/Magento/Customer/etc/frontend/di.xml . <plugin name="customer_cart" type="\Magento\Customer\Model\Cart\ConfigPlugin" />. You can't remove from core module
    – year
    Commented Jul 25, 2016 at 8:44
0

Magento2.1 fix plugin and preference problem

1
  • Which problem, and how is it related to this question? Commented Oct 27, 2016 at 12:34

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.