I have created custom attribute "CustomerAccount".Customer does not have access to this field .When new customer account is created CustomerAccount field should get random unique value and should get save in customer module.
Magento – how to generate and save unique value for customer attribute in magento
customereav
Related Solutions
I have got the solution
protected $customer;
protected $customerFactory;
public function __construct(
\Magento\Customer\Model\Customer $customer,
\Magento\Customer\Model\ResourceModel\CustomerFactory $customerFactory
)
{
$this->customer = $customer;
$this->customerFactory = $customerFactory;
}
...
...
$customerId = "1";
$customer = $this->customer->load($customerId);
$data = "customer attribute value";
$customerData = $customer->getDataModel();
$customerData->setCustomAttribute('customer_attribute_code',$data);
$customer->updateData($customerData);
$customerResource = $this->customerFactory->create();
$customerResource->saveAttribute($customer, 'customer_attribute_code');
I have created it with below code in InstallData.php file.
namespace Module\Name\Setup; use Magento\Customer\Setup\CustomerSetupFactory; use Magento\Eav\Model\Entity\Attribute\Set as AttributeSet; use Magento\Eav\Model\Entity\Attribute\SetFactory as AttributeSetFactory; use Magento\Framework\Setup\InstallDataInterface; use Magento\Framework\Setup\ModuleContextInterface; use Magento\Framework\Setup\ModuleDataSetupInterface; /** * @codeCoverageIgnore */ class InstallData implements InstallDataInterface { /** * @var CustomerSetupFactory */ protected $customerSetupFactory; /** * @var AttributeSetFactory */ private $attributeSetFactory; /** * @param CustomerSetupFactory $customerSetupFactory * @param AttributeSetFactory $attributeSetFactory */ public function __construct( CustomerSetupFactory $customerSetupFactory, AttributeSetFactory $attributeSetFactory ) { $this->customerSetupFactory = $customerSetupFactory; $this->attributeSetFactory = $attributeSetFactory; } /** * {@inheritdoc} */ public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context) { $customerSetup = $this->customerSetupFactory->create(['setup' => $setup]); $customerAddressEntity = $customerSetup->getEavConfig()->getEntityType('customer_address'); $attributeSetId = $customerAddressEntity->getDefaultAttributeSetId(); /** @var $attributeSet AttributeSet */ $attributeSet = $this->attributeSetFactory->create(); $attributeGroupId = $attributeSet->getDefaultGroupId($attributeSetId); $customerSetup->addAttribute('customer_address', 'location_id', [ 'type' => 'int', 'label' => 'Location Id', 'input' => 'text', 'required' => false, 'visible' => true, 'user_defined' => true, 'sort_order' => 13, 'position' => 13, 'system' => 0, ]); $attribute = $customerSetup->getEavConfig()->getAttribute('customer_address', 'location_id') ->addData([ 'attribute_set_id' => $attributeSetId, 'attribute_group_id' => $attributeGroupId ]); $attribute->save(); } }
then run the below commands:
php bin/magento setup:upgrade
php bin/magento setup:static-content:deploy -f (optional in `develop` mode)
php bin/magento cache:flush
Best Answer
You can use this extension for creating customer attributes. http://www.magentocommerce.com/magento-connect/customer-attributes-manager-5092.html
and if you don't want to show them on front end you can modify saveAction() function of its indexcontroller.php where it decided where to show attributes. It can work with magento 1.7 CE