Here first question of mine. Now i added custom attribute to my customer following this code
InstallData.php
<?php
namespace Kt\AddBonus\Setup;
use Magento\Customer\Model\Customer;
use Magento\Customer\Model\Customer;
use Magento\Customer\Setup\CustomerSetupFactory;
use Magento\Eav\Model\Entity\Attribute\SetFactory as AttributeSetFactory;
use Magento\Framework\Setup\InstallDataInterface;
use Magento\Customer\Model\Customer;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;
class InstallData implements InstallDataInterface
{
const BONUS_ATTRIBUTE_CODE = 'customer_bonus';
/**
* @var CustomerSetupFactory
*/
private $customerSetupFactory;
/**
* @var AttributeSetFactory
*/
private $attributeSetFactory;
public function __construct(CustomerSetupFactory $customerSetupFactory,
AttributeSetFactory $attributeSetFactory)
{
$this->customerSetupFactory = $customerSetupFactory;
$this->attributeSetFactory = $attributeSetFactory;
}
/**
* Installs data for a module
*
* @param ModuleDataSetupInterface $setup
* @param ModuleContextInterface $context
* @return void
*/
public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
{
$setup->startSetup();
$customerSetup = $this->customerSetupFactory->create(['setup' => $setup]);
$customerEntity = $customerSetup->getEavConfig()->getEntityType('customer');
$attributeSetId = $customerEntity->getDefaultAttributeSetId();
$attributeSet = $this->attributeSetFactory->create();
$attributeGroupId = $attributeSet->getDefaultGroupId($attributeSetId);
$customerSetup->addAttribute( Customer::ENTITY, self::BONUS_ATTRIBUTE_CODE, [
'type' => 'int',
'label' => 'Customer Bonus',
'input' => 'text',
'required' => false,
'visible' => true,
'user_defined' => true,
'system' => false,
]);
$attribute = $customerSetup->getEavConfig()->getAttribute( Customer::ENTITY, self::BONUS_ATTRIBUTE_CODE)
->addData([
'attribute_set_id' => $attributeSetId,
'attribute_group_id' => $attributeGroupId,
'used_in_forms'=>['adminhtml_customer']
]);
$attribute->save();
$setup->endSetup();
}
}
And now i need Plugin which will change custom attribute value to 1000.
this is what i have:
etc\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>
Plugin\BonusPlugin.php
<?php
namespace Kt\AddBonus\Plugin;
use Kt\AddBonus\Setup\InstallData;
use Magento\Customer\Api\CustomerRepositoryInterface;
use Magento\Customer\Api\Data\CustomerInterface;
class BonusPlugin
{
public function beforeSave(CustomerRepositoryInterface $customerRepository,
CustomerInterface $customer)
{
$customer->setCustomAttribute(InstallData::BONUS_ATTRIBUTE_CODE,1000);
}
}
when i press "Create An Account" it just reset password fields in registration form and remains on this page, with the "we cant save Customer" error. How i can create this plugin? And what i did wrong?