0

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?

0

Try like this,

etc\di.xml

<type name="Magento\Customer\Model\AccountManagement">
   <plugin name="bonusPlugin" sortOrder="10" type="Kt\Addbonus\Plugin\BonusPlugin"/>
</type>

Plugin\BonusPlugin.php

<?php

namespace Kt\Addbonus\Plugin;


use Magento\Customer\Api\Data\CustomerInterface;
use Magento\Customer\Model\AccountManagement;

class BonusPlugin
{
    public function beforeCreateAccountWithPasswordHash(AccountManagement $subject,CustomerInterface $customer, $hash, $redirectUrl)
    {
        $customer->setCustomerBonus("1000");
        return [$customer, $hash, $redirectUrl];
    }
}
  • Thank you for answer, but it dont work, have same trouble. – newakkoff Mar 6 '18 at 11:54
  • Did you cleared the cache after update the new code? Check log files for error if any – saravanavelu Mar 6 '18 at 11:55
  • i checked it, and magento log files and Nginx log files, did not see there something helpful – newakkoff Mar 6 '18 at 12:01
  • yes after update code i changed module version -> setup:upgrade -> cache:clean – newakkoff Mar 6 '18 at 12:06

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.