Question

I am trying to update a custom customer attribute, I have the following code

<?php
namespace Vendor\Module\Plugin;

use Magento\Customer\Model\ResourceModel\CustomerRepository;
use Magento\Customer\Api\CustomerRepositoryInterface;
use Magento\Customer\Api\Data\CustomerInterfaceFactory;
use Magento\Customer\Model\CustomerFactory;


class CustomerAfterSave
{
    protected $_customerDataFactory;
    protected $_customerRepositoryInterface;
    protected $_customerRepository;

    public function __construct(
        \Magento\Customer\Api\CustomerRepositoryInterface $customerRepositoryInterface,
        \Magento\Customer\Model\CustomerFactory $customerFactory,
    ) {

        $this->_customerRepositoryInterface = $customerRepositoryInterface;
        $this->_customerFactory = $customerFactory;
    }

    public function afterSave(CustomerRepository $subject,$savedCustomer)
    {
        //Get Customer
        $customerId = $savedCustomer->getId();

        $customer = $this->_customerRepositoryInterface->getById($customerId);
        $customer->setCustomAttribute('my_custom_attribute', 'test');
        $this->_customerRepositoryInterface->save($customer);
        exit;

But then I get the following error

( ! ) Fatal error: Maximum function nesting level of '256' reached, aborting! in /vendor/magento/module-customer/Model/ResourceModel/Address.php on line 58

Could anyone advise and see where I am going wrong

Edit: I don't believe this to be a duplicate question as I have disabled xdebug and the code just times out

Further edit: I think my problem is the same as Magento 2 : How to save data in database

Was it helpful?

Solution

Use beforeSave Plugin. Using afterSave caused the infinite loop.

OTHER TIPS

Try below UpgradeData.php script.

<?php

namespace Vendor\CustomModule\Setup;

use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Framework\Setup\UpgradeDataInterface;
use Magento\Eav\Setup\EavSetup;

class UpgradeData implements UpgradeDataInterface
{
    /**
     * @var \Magento\Eav\Model\ResourceModel\Entity\Attribute
     */
    protected $_eavAttribute;


    public function __construct(
        EavSetup $eavSetupFactory,
        \Magento\Eav\Model\ResourceModel\Entity\Attribute $eavAttribute
    ) {
        $this->eavSetupFactory = $eavSetupFactory;
        $this->_eavAttribute = $eavAttribute;
    }

    /**
     * @param ModuleDataSetupInterface $setup
     * @param ModuleContextInterface $context
     */

    public function upgrade(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
    {
        if (version_compare($context->getVersion(), '1.0.1', '<')) {
            $setup->startSetup();
            $entityType = $this->eavSetupFactory->getEntityType(\Magento\Customer\Model\Customer::ENTITY);
            $entityTypeId = $entityType['entity_type_id'];
            $attributeId = $this->_eavAttribute->getIdByCode(\Magento\Customer\Model\Customer::ENTITY, 'prefix');
            $this->eavSetupFactory->updateAttribute($entityTypeId, $attributeId, 'is_required', 0, null);
            $setup->endSetup();
        }
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top