1

I had created a custom API for adding new customer in Magento 2.3. I had injected dependency injection and passed the method to create the customer account. But it gives me below error:

main.CRITICAL: Type Error occurred when creating object: MyModule\Customapi\Model\Addcustomer, Too few arguments to function MyModule\Customapi\Model\Addcustomer::__construct(), 7 passed in /var/www/html/vendor/magento/framework/ObjectManager/Factory/AbstractFactory.php on line 116 and exactly 8 expected [] []

I had injected CustomerRepositoryInterface to add new customer like this:

<?php
namespace MyModule\Customapi\Model;
use MyModule\Customapi\Api\AddCustomerInterface;
use Magento\Store\Model\StoreManagerInterface;
use Magento\Customer\Api\AccountManagementInterface;

class AddCustomer implements AddCustomerInterface
{
    protected $request;
    protected $storeManager;
    protected $dataHelper;
    protected $accountManagementInterface;
    protected $authentication;
    private $accountConfirmation;
    protected $objectManager;
    protected $customerRepository;
    protected $_customerFactory;
    protected $_customerInterfaceFactory;

    public function __construct(
        StoreManagerInterface $storeManager,
        Data $dataHelper,
        RequestInterface $request,
        AccountConfirmation $accountConfirmation = null,
        AccountManagementInterface $accountManagementInterface,
        CustomerRepositoryInterface $customerRepository,
        \Magento\Customer\Model\Customer $customerFactory,
        \Magento\Customer\Api\Data\CustomerInterfaceFactory $customerInterfaceFactory
    ) {
        $objectManager = ObjectManager::getInstance();
        $this->storeManager = $storeManager;
        $this->dataHelper =$dataHelper;
        $this->request = $request;
        $this->accountManagementInterface = $accountManagementInterface;
        $this->accountConfirmation = $accountConfirmation ?: $objectManager
            ->get(AccountConfirmation::class);
        $this->customerRepository = $customerRepository;
        $this->_customerFactory = $customerFactory;
        $this->_customerInterfaceFactory = $customerInterfaceFactory;
    }

    public function addcustomer()
    {
        ...
        $customer = $this->_customerInterfaceFactory->create();
        ...
    }
}
  • add more code please – Ashar Riaz Jan 29 at 11:30
  • Hey @AsharRiaz, I had updated the code. Can you please check and suggest me the proper solution? – MagentoUser Jan 29 at 13:31
3

You need to compile your dependency injection that is di.xml Try this

php bin/magento setup:di:compile
php bin/magento cache:clean
| improve this answer | |
2

Try this

rm -rf var/generation/*
bin/magento setup:di:compile
bin/magento cache:clean
| improve this answer | |
2

You will need to remove all generated files. The problem is the scope has changed but Magento is not updated about this yet.

Running bin/magento setup:upgrade will clear the generated folder for you. Depending on what version of Magento you are on this will be in var/generated or generated/

If you have Magento in a "compiled" mode you will have to run bin/magento setup:di:compile as well.

| improve this answer | |
0

You forgot , after \Magento\Customer\Model\Customer $customerFactory it should be like below.

public function __construct(
        StoreManagerInterface $storeManager,
        Data $dataHelper,
        RequestInterface $request,
        AccountConfirmation $accountConfirmation = null,
        AccountManagementInterface $accountManagementInterface,
        CustomerRepositoryInterface $customerRepository,
        \Magento\Customer\Model\Customer $customerFactory,
        \Magento\Customer\Api\Data\CustomerInterfaceFactory $customerInterfaceFactory
    )

Hope it'll solve your issue. Let me know if you still face any problem on this.

| improve this answer | |
  • No, It was my mistake just c&p issue. i had already used the , in my code. Thanks for pointing @MayankZalavadia – MagentoUser Jan 29 at 13:40

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.