0

I was trying to create a plugin for existing Magento 2 extension, but it is always getting Uncaught TypeError:

 Fatal Error: 'Uncaught TypeError: Argument 1 passed to
 Vinc\discount4customer\Plugin\CheckoutCouponApply::__construct() must
 be an instance of Vinc\discount4customer\Model\AccountFactory,
 instance of Magento\Framework\ObjectManager\ObjectManager given,
 called in
 /var/www/m2site/vendor/magento/framework/ObjectManager/Factory/AbstractFactory.php
 on line 93 and defined in
 /var/www/m2site/app/code/Vinc/discount4customer/Plugin/CheckoutCouponApply.php:30
 Stack trace:

  #0 /var/www/m2site/vendor/magento/framework/ObjectManager/Factory/AbstractFactory.php(93):
 Vinc\discount4customer\Plugin\CheckoutCouponApply-__construct(Object(Magento\Framework\ObjectManager\ObjectManager))



  #1 /var/www/m2site/vendor/magento/framework/ObjectManager/Factory/Compiled.php(88):
 Magento\Framework\ObjectManager\Factory\AbstractFactory-createObject('Vinc\\discou...',
 Array)

  #2 /var/www/m2site/vendor/magento/framework/ObjectManager/ObjectManager.php(71):
 Magento\Framework\ObjectManager\Factory\Compiled-create('Vinc\\discou...')'
 in
 '/var/www/m2site/app/code/Vinc/discount4customer/Plugin/CheckoutCouponApply.php'
 on line 30";root@vs:/home/uuii/websites/m2site/var/report/api#**

My plugin page is very simple as following:

It came from There is an event or method to “plug-in” when the coupon is removed from the cart/quote in the checkout flow?

<?php 

namespace Vinc\discount4customer\Plugin;
use Magento\Quote\Model\CouponManagement as CouponManagement;
use Vinc\discount4customer\Model\AccountFactory;

//use Magento\Framework\Event\ObserverInterface;

class CheckoutCouponApply {

protected $accountFactory;


public function __construct(
        AccountFactory $accountFactory
    )
    {
        $this->accountFactory        = $accountFactory;

    }


    public function beforeSet(CouponManagement $subject, $cartId, $couponCode)
    {
        $account = $this->accountFactory->create()->loadByCode($couponCode);
         // Do what you need to do
         file_put_contents("/var/www/m2site/var/log/c".time().".txt",$couponCode ."--" .date("Y-m-d H:i:s").$account);
    }
}
?>

it seems I couldn't use the functions under another namespace? thanks advance for helping.

3
  • delete the folder var/generation and try.
    – Manish Joy
    Commented May 22, 2017 at 6:04
  • Just remove var/generation folder from root of your project. Commented May 22, 2017 at 6:09
  • I did rm the var/generation folder already, but it won't help.it seems I couldn't use the functions under other namespace(except namespace Magento). I could use CouponManagement to get coupon, but can't use accountFactory to get other data.
    – Vin.F
    Commented May 23, 2017 at 1:11

1 Answer 1

0

For factory to be created Vinc\discount4customer\Model\Account must exist. This error can also occur if you have done argument replacement using in di.xml. can you make sure that there is nothing like this in your di.xml and the class exists. You should also be in able to see your factory generated in var/generation

1
  • finally, I get this plugin working by removing var/generation and run bin/magento setup:di:compile bin/magento setup:static-content:deploy , also I create a new di.xml(although I can't see there is any wrong in the old one). thanks for everyone who been helping me.
    – Vin.F
    Commented May 24, 2017 at 13:19

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.