0

Good evening. I need to create Module which will add Custom Attribute for Customer and Plugin which will set this attribute to 1000 after registration, but during registration of a new Customer it displays error "We can't save the customer" This is what I got:

di.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
   <type name="Magento\Customer\Api\CustomerRepositoryInterface">
<plugin name="bonusPlugin" type="\Kt\Addbonus\BonusPlugin"/>
</type>
</config>

InstallData

<?php


use Magento\Customer\Model\Customer;
use Magento\Customer\Setup\CustomerSetupFactory;
use Magento\Eav\Model\Config;
use Magento\Eav\Setup\EavSetup;
use Magento\Framework\Setup\InstallDataInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;



class InstallData implements InstallDataInterface
{

const ATTRIBUTE_CODE = 'bonus_attribute';
/**
 * @var EavSetup
 */
private $eavSetup;
/**
 * @var Config
 */
private $config;

public function __construct(EavSetup $eavSetup,
                            Config $config)
{

    $this->eavSetup = $eavSetup;
    $this->config = $config;
}


/**
 * Installs data for a module
 *
 * @param ModuleDataSetupInterface $setup
 * @param ModuleContextInterface $context
 * @return void
 */
public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
{

    $setup->startSetup();


    $this->eavSetup->addAttribute(Customer::ENTITY, ATTRIBUTE_CODE, [
        'type' => 'int',
        'label' => 'Custom Attribute',
        'input' => 'number',
        'required' => false,
        'visible' => true,
        'user_defined' => true,
        'position' =>999,
        'system' => 0,
    ]);

    $attribute = $this->config->getAttribute(
        Customer::ENTITY,
        self::ATTRIBUTE_CODE
    );

    $attribute->setData('used_in_forms',['adminhtml']);

    $attribute->save();

    $setup->endSetup();

}
}

BonusPlugin

<?php


use Magento\Customer\Api\Data\CustomerInterface;
use Magento\Customer\Api\Data\CustomerInterfaceFactory;

class BonusPlugin
{
  public function afterSave(CustomerInterfaceFactory $factory, CustomerInterface $customer){
    $customer->setCustomAttribute('bonus_attribute',1000);
 }
}

But when I try to create or register a new customer, it shows "We can't save the customer".

  • If you disabled your extension and delete attribute then same is happeing? – Amit Bera Mar 5 '18 at 14:54
  • @AmitBera no. When I disable my module it works ok. – newakkoff Mar 5 '18 at 15:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.