magento2 создать атрибут клиента
Любой может мне помочь, я хочу добавить атрибут клиента, и я разместил свой код, как показано ниже.Я установил его, не получив никакой ошибки, но отметив, что это произошло.
Customer/Attrib/Setup/InstallData.php
<?php
namespace Customer\Attrib\Setup;
use Magento\Customer\Setup\CustomerSetupFactory;
use Magento\Customer\Model\Customer;
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;
}
public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
{
/** @var CustomerSetup $customerSetup */
$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, 'my_measurements', [
'type' => 'varchar',
'label' => 'Mymeasurements',
'input' => 'text',
'required' => false,
'visible' => true,
'user_defined' => true,
'sort_order' => 1000,
'position' => 1000,
'system' => 0,
]);
$attribute = $customerSetup->getEavConfig()->getAttribute(Customer::ENTITY, 'my_measurements')
->addData([
'attribute_set_id' => $attributeSetId,
'attribute_group_id' => $attributeGroupId,
'used_in_forms' => ['customer_address_edit'],
]);
$attribute->save();
}
}
Customer/Attrib/registration.php
<?php
\Magento\Framework\Component\ComponentRegistrar::register(
\Magento\Framework\Component\ComponentRegistrar::MODULE,
'Customer_Attrib',
__DIR__
);
Customer/Attrib/etc/module.xml
<?xml version="1.0" encoding="UTF-8"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/Module/etc/module.xsd">
<module name="Customer_Attrib" setup_version="1.0.0">
<sequence>
<module name="Customer_Attrib"/>
</sequence>
</module>
</config>
Заранее благодарю.
3 answers
Пожалуйста, внесите следующие изменения в module.xml,
<?xml version="1.0" encoding="UTF-8"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/Module/etc/module.xsd">
<module name="Customer_Attrib" setup_version="1.0.0">
<sequence>
<!--<module name="Customer_Attrib"/>-->
<module name="Magento_Customer"/>
</sequence>
</module>
</config>
InstallData.php
<?php
namespace Customer\Attrib\Setup;
use Magento\Customer\Setup\CustomerSetupFactory;
use Magento\Customer\Model\Customer;
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;
}
public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
{
$setup->startSetup();
/** @var CustomerSetup $customerSetup */
$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, 'my_measurements', [
'type' => 'varchar',
'label' => 'Mymeasurements',
'input' => 'text',
'required' => false,
'visible' => true,
'user_defined' => true,
'sort_order' => 1000,
'position' => 1000,
'system' => 0,
]);
$attribute = $customerSetup->getEavConfig()->getAttribute(Customer::ENTITY, 'my_measurements')
->addData([
'attribute_set_id' => $attributeSetId,
'attribute_group_id' => $attributeGroupId,
'used_in_forms' => ['customer_address_edit',customer_form],
]);
$attribute->save();
$setup->endSetup();
}
}
Теперь выполните команды,
php bin/magento setup:upgrade
rm -rf var/view_preprocessed
rm -rf pub/static/frontend/
rm -rf var/cache/rm -rf var/page_cache/
rm -rf var/generation/
php bin/magento setup:static-content:deploy
php bin/magento cache:flush
php bin/magento cache:clean
Теперь войдите в систему amdin, ваш атрибут будет отображаться в разделе "Клиент" в виде снимка экрана.
[ОБНОВЛЕНИЕ]
Если вы все еще не поняли, пожалуйста, не забудьте добавить приведенный ниже код в app/code/<vendor>/<module>/view/base/ui_component/customer_form.xml
<?xml version="1.0" encoding="UTF-8"?>
<form xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Ui:etc/ui_configuration.xsd">
<fieldset name="customer">
<field name="my_measurements" formElement="input">
<settings>
<visible>true</visible>
</settings>
</field>
</fieldset>
</form>
После этого выполните команды, как указано выше.
Ваш XML-модуль должен быть:
Customer/Attrib/etc/module.xml
<?xml version="1.0" encoding="UTF-8"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/Module/etc/module.xsd">
<module name="Customer_Attrib" setup_version="1.0.0">
<sequence>
<!--<module name="Customer_Attrib"/>-->
<module name="Magento_Customer"/>
</sequence>
</module>
</config>
Customer/Attrib/Setup/InstallData.php
public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
{
$setup->startSetup();
//Your add attribute code here
$setup->endSetup();
}
Не забудьте выполнить команду обновления установки.
У нас есть возможность создать атрибут/атрибуты клиента с помощью программного инструмента magento2 silk, что очень просто. пожалуйста, выполните следующие действия, которые могут оказаться полезными.
Пожалуйста, найдите скриншот:
Шаг 1: Создайте модуль с выбором атрибута клиента с помощью инструмента создания модуля.
Шаг 2: Загрузите zip-файл и установите его в свой magento2, используя шаги установки модуля
Шаг 3: Очистить кэш-память и кэш страниц и войдите в свой аккаунт администратора, проверьте это.
Выход: