2

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

1
  • I think it's a different case. It doesn't duplicate. Dec 20, 2017 at 8:44

3 Answers 3

1

Use beforeSave Plugin. Using afterSave caused the infinite loop.

6
  • I need to use afterSave as I need to customer id for newly inserted customers to pass to a 3rd party system
    – Goose84
    Dec 18, 2017 at 15:22
  • Why do you need to save the customer itself again? Dec 18, 2017 at 15:25
  • When a customer is created I need to submit their data to a 3rd party. The data I submit is the newly created customer id and then when the 3rd party system responses I then need to update the custom customer attribute
    – Goose84
    Dec 18, 2017 at 15:55
  • So, from the 3rd party system, is there existing customer info or not? I have an idea for you. Before saving customer info on the Magento side, we need to get the info from the 3rd system, and then compare with current customer info on the Magento side. Finally, save the customer info. Dec 18, 2017 at 16:02
  • Unfortunately not, I need the customer to be saved in Magento first then in the 3rd party system
    – Goose84
    Dec 18, 2017 at 18:01
0

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();
        }
    }
}
0

Try below DataPatch file

<?php
namespace Vendor\Module\Setup\Patch\Data;

use Magento\Eav\Setup\EavSetup;
use Magento\Customer\Model\Customer;
use Magento\Customer\Model\Indexer\Address\AttributeProvider As customer_address;
use Magento\Eav\Setup\EavSetupFactory;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Framework\Setup\Patch\DataPatchInterface;

class UpdateCustomerAddressAttribute implements DataPatchInterface
{
     /** @var ModuleDataSetupInterface */
    private $moduleDataSetup;

    /** @var EavSetupFactory */
    private $eavSetupFactory;

    /**
     * @param ModuleDataSetupInterface $moduleDataSetup
     * @param EavSetupFactory $eavSetupFactory
     */
    public function __construct(
        ModuleDataSetupInterface $moduleDataSetup,
        EavSetupFactory $eavSetupFactory
    ) {
        $this->moduleDataSetup = $moduleDataSetup;
        $this->eavSetupFactory = $eavSetupFactory;
    }

    /**
     * {@inheritdoc}
     */
    public function apply()
    {
        /** @var EavSetup $eavSetup */
        $eavSetup = $this->eavSetupFactory->create(['setup' => $this->moduleDataSetup]);

        // $attribute = $eavSetup->getAttribute('customer_address', 'region');
        // $eavSetup->updateAttribute('customer_address', 'region', 'frontend_label', 'State');

        $eavSetup->updateAttribute(Customer::ENTITY, 'prefix', 'is_required', 0, null);    

        $eavSetup->updateAttribute(customer_address::ENTITY, 'region', 'frontend_label', 'State');
        $eavSetup->updateAttribute(customer_address::ENTITY, 'postcode', 'frontend_label', 'Zip Code');
    }

    /**
     * {@inheritdoc}
     */
    public static function getDependencies()
    {
        return [];
    }

    /**
     * {@inheritdoc}
     */
    public function getAliases()
    {
        return [];
    }
}

then run bin/magento setup:upgrade command...

DataPatch File is works same as UpgradeData. data patch will call once.

see patch_list table in Db

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.