Magento 2自定义客户属性未显示
我一直在尝试创建一个自定义客户属性,允许客户将属性保存到他们的配置文件中。当我用代码更新我的Magento站点时,我看不到前端更改,也看不到数据库中有任何更新。我所做的哪些错误导致了这两个问题?我需要添加一些.phtml更改吗 InstallData.phpMagento 2自定义客户属性未显示,magento,magento2,magento-2.0,magento-2.0.7,Magento,Magento2,Magento 2.0,Magento 2.0.7,我一直在尝试创建一个自定义客户属性,允许客户将属性保存到他们的配置文件中。当我用代码更新我的Magento站点时,我看不到前端更改,也看不到数据库中有任何更新。我所做的哪些错误导致了这两个问题?我需要添加一些.phtml更改吗 InstallData.php <?php namespace SR\DeliveryDate\Setup; use Magento\Customer\Setup\CustomerSetupFactory; use Magento\Customer\Model\C
<?php
namespace SR\DeliveryDate\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, 'custom_attribute', [
'type' => 'varchar',
'label' => 'Custom Attributeeee',
'input' => 'text',
'required' => false,
'visible' => true,
'user_defined' => true,
'position' =>999,
'system' => 0,
]);
$attribute = $customerSetup->getEavConfig()->getAttribute(Customer::ENTITY, 'custom_attribute')
->addData([
'attribute_set_id' => $attributeSetId,
'attribute_group_id' => $attributeGroupId,
'used_in_forms' => ['adminhtml_customer', 'customer_address_edit'],//you can use other forms also ['adminhtml_customer_address', 'customer_address_edit', 'customer_register_address']
]);
$attribute->save();
}
}
在对代码进行重大更改后,尝试运行以下命令。示例:如果创建扩展,则可能会运行升级命令
命令
php bin/magento setup:upgrade
php bin/magento setup:static-content:deploy
php bin/magento cache:clean or php bin/magento cache:flush
上述命令应在控制台中运行
试试看,它可能会对你有所帮助。请试试这个:-
\magento2\app\code\Custom\CustomerAttribute\Block\Widget\Passport.php
/**
* @return bool
*/
public function isEnabled() {
$attributeMetadata = $this->_getAttribute('passport');
return $attributeMetadata ? (bool) $attributeMetadata->isVisible() : false;
}