1

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 ["(before) $interceptedInput (/before)"];
    }
} 
2

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();
    }
} 
|improve this answer|||||
  • 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 6 '16 at 12:12
  • Exactly, I don't think there is better option than preference. – Bartłomiej Szubert May 6 '16 at 12:14
  • 1
    No. For your particular problem, you can still use plugins. You just have to dig a little more: gist.github.com/adragus-inviqa/52faeb070b8f95f5f295820ddee2b406. Stop rewriting classes! There's TONS of ways to get your code in there. – nevvermind May 7 '16 at 11:06
1

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

|improve this answer|||||

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.