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 name="\MAS\CouponAttempt\Model\SalesRule\Validator">
            <argument name="ruleFactoryInjection" xsi:type="object">Magento\SalesRule\Model\RuleFactory</argument>

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, 2015 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, 2015 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, 2015 at 17:41

3 Answers 3


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.


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, 2015 at 15:19
  • Try to post the whole plugin content here. Nov 30, 2015 at 15:22
  • Done, quick note the >error_log('validator constructed'); never happens either
    – marijnm
    Nov 30, 2015 at 15:26
  • Do you need my di.xml content aswell?
    – marijnm
    Nov 30, 2015 at 15:27
  • yes, would be nice, so it is possible to reproduce issue. Nov 30, 2015 at 15:28

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, 2018 at 8:18

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.