2

I'm trying to make a plugin to extend the \Magento\SalesRule\Model\Validator

Which works like a charm, but the problem is I need to make use of Magento\SalesRule\Model\RuleFactory

I've tried making a __constructor in the plugin class and adding the RuleFactory there through dependency injection as described in het Magento 2 Developer Guide which looked like this:

The entire "Validator" plugin class now looks like this:

class Validator{
    /**
     * @var \Magento\SalesRule\Model\RuleFactory
     */
    private $ruleFactory;

    /**
    * @param \Magento\SalesRule\Model\RuleFactory $ruleFactory
     */
    public function __constructor(\Magento\SalesRule\Model\RuleFactory $ruleFactory){
        $this->ruleFactory = $ruleFactory;
        error_log('validator constructed');
    }

    public function aroundInit(\Magento\SalesRule\Model\Validator $subject, $procede, $websiteId, $customerGroupId, $couponCode){
        //custom before code
        $return = $proceed($websiteId, $customerGroupId, $couponCode);
        //custom after code
        return $return;
    }
}

but this would just return null for $this->ruleFactory at all times.

the di.xml document contains the following:

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
    <type name="\Magento\SalesRule\Model\Validator">
        <plugin name="ValidatorPlugin" type="\MAS\CouponAttempt\Model\SalesRule\Validator" sortOrder="1" disabled="false"/>
    </type>
    <type name="\MAS\CouponAttempt\Model\SalesRule\Validator">
        <arguments>
            <argument name="ruleFactoryInjection" xsi:type="object">Magento\SalesRule\Model\RuleFactory</argument>
        </arguments>
    </type>
</config>

Interestingly enough I found that in the generated Interceptor.php file the constructor is actually made. But it is never called, can anybody tell me why this could be?

  • Clear contents of var/generation – Marius Nov 30 '15 at 16:50
  • I did, and after your comment i checked the interceptor file and interestingly enough I did find the following: public function __constructor(\Magento\SalesRule\Model\RuleFactory $ruleFactory) { $pluginInfo = $this->pluginList->getNext($this->subjectType, '__constructor'); if (!$pluginInfo) { return parent::__constructor($ruleFactory); } else { return $this->___callPlugins('__constructor', func_get_args(), $pluginInfo); } } – marijnm Nov 30 '15 at 17:40
  • So it's actually being generated but the constructor is not being called (still not getting an error_log entry and the $ruleFactory is still not defined), any thoughts? – marijnm Nov 30 '15 at 17:41
9

Late answer but you need to change __constructor to __construct and it will work perfectly well. Don't use static ObjectManager if possible. You should always use DI instead.

2

Yes, it is supported. Looks like in your sample function keyword is missing, try:

public function __constructor(\Magento\SalesRule\Model\RuleFactory $ruleFactory){
    $this->ruleFactory = $ruleFactory;
}
  • Thanks for your response, this was however merely an oversight while typing up this post, I'll edit my question. – marijnm Nov 30 '15 at 15:19
  • Try to post the whole plugin content here. – Alex Paliarush Nov 30 '15 at 15:22
  • Done, quick note the >error_log('validator constructed'); never happens either – marijnm Nov 30 '15 at 15:26
  • Do you need my di.xml content aswell? – marijnm Nov 30 '15 at 15:27
  • yes, would be nice, so it is possible to reproduce issue. – Alex Paliarush Nov 30 '15 at 15:28
-3

I decided to go with the less clean solution and just used a static instance of the objectmanager and got the rule factory through that

  • Not recommended by magento – Sebastien Nov 13 '18 at 8:18

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.