2

It is possible?

di.xml

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <type name="Magento\SalesRule\Block\Adminhtml\Promo\Quote">
        <plugin name="Vendor_Module::plugin_new" type="Vendor\Module\Plugin\New" sortOrder="1" disabled="true"/>
    </type>
</config>

New.php

namespace Vendor\Module\Plugin;

class New{ public function before_construct(ChildBefore $subject, $interceptedInput) { echo '<pre>'; var_dump($subject); var_dump($interceptedInput); echo '</pre>';

    return [&quot;(before) $interceptedInput (/before)&quot;];
}

}

Gianni Di Falco
  • 1,008
  • 1
  • 16
  • 31

2 Answers2

3

No, unfortunately you can't. To overwrite _construct function, you have to use preference:

<preference for="Magento\SalesRule\Block\Adminhtml\Promo\Quote" type="Vendor\Module\Other\New" />

New.php

namespace Vendor\Module\Other;

class New extends Magento\SalesRule\Block\Adminhtml\Promo\Quote{
    protected function _construct()
    {
        echo '<pre>';
        [Your code here]
        echo '</pre>';

        return parent::_construct();
    }
} 
Bartłomiej Szubert
  • 2,642
  • 18
  • 26
  • So, if a want to add a new button to that block-container Magento\SalesRule\Block\Adminhtml\Promo\Quote, how do i do it? Do i have to use preference by force? – Gianni Di Falco May 06 '16 at 12:12
  • Exactly, I don't think there is better option than preference. – Bartłomiej Szubert May 06 '16 at 12:14
  • 3
    No. For your particular problem, you can still use plugins. You just have to dig a little more: https://gist.github.com/adragus-inviqa/52faeb070b8f95f5f295820ddee2b406. Stop rewriting classes! There's TONS of ways to get your code in there. – nevvermind May 07 '16 at 11:06
2

No, unfortunately plugins can only be used to to extend the behavior of any public method within a Magento class. Thus, they cannot be used to extend protected/private methods such as the _construct() method.

In order to modify those protected/private methods you will have to use preferences.

If you want to find out when and when not to use plugins I suggest you have a look at this great post: Rewriting Magento 2 classes vs Plugins

Raphael at Digital Pianism
  • 70,385
  • 34
  • 188
  • 352