text 创建和保存客户属性
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了text 创建和保存客户属性相关的知识,希望对你有一定的参考价值。
## Create attribute
```php
public function upgrade(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
{
$setup->startSetup();
if (version_compare($context->getVersion(), '0.1.1') < 0)
{
$customerSetup = $this->_customerSetupFactory->create(['setup' => $setup]);
$customerEntity = $customerSetup->getEavConfig()->getEntityType('customer');
$attributeSetId = $customerEntity->getDefaultAttributeSetId();
/**@var $attributeSet AttributeSet */
$attributeSet = $this->_attributeSetFactory->create();
$attributeGroupId = $attributeSet->getDefaultGroupId($attributeSetId);
$customerSetup->addAttribute(Customer::ENTITY, 'sp_id', [
'type' => 'varchar',
'label' => 'SiemprePago Id',
'input' => 'text',
'required' => 0,
'visible' => 1,
'user_defined' => 0,
'sort_order' => 200,
'position' => 150,
'system' => 0,
'is_used_in_grid' => false,
'is_visible_in_grid' => false,
'is_filterable_in_grid' => false,
'is_searchable_in_grid' => false
]);
$attributeSpId = $customerSetup->getEavConfig()->getAttribute(Customer::ENTITY, 'sp_id');
$attributeSpId->addData([
'attribute_set_id' => $attributeSetId,
'attribute_group_id' => $attributeGroupId,
'used_in_forms' => [
'adminhtml_customer'
]
])->save();
}
$setup->endSetup();
}
```
## Save attribute by script
```php
/** @var \Magento\Customer\Model\ResourceModel\Customer $customerResource */
$customerResource = $this->_objectManager->create('Magento\Customer\Model\ResourceModel\Customer');
$customerFactory = $this->_objectManager->create('Magento\Customer\Model\CustomerFactory');
/** @var \Magento\Customer\Model\Customer $customer */
$customer = $customerFactory->create();
$customerResource->load($customer,4);
$customerData = $customer->getDataModel();
$customerData->setCustomAttribute('sp_id', '1462');
$customerData->setCustomAttribute('cellphone', '091919190');
$customer->updateData($customerData);
$customerResource->save($customer);
```
以上是关于text 创建和保存客户属性的主要内容,如果未能解决你的问题,请参考以下文章
magento麦进斗客户地址属性不保存在sales_flat_order_address
我啥时候使用保存!,创建!和更新属性!在 Rails 中?
前端开发第6篇:JavaScript客户端(浏览器)
PowerShell 创建,查看和保存嵌套的对象属性
[JavaWeb]CookieSession 和 Filter
ElasticSearch客户端注解使用介绍