Magento 2自定义客户属性未显示

xjreopfe  于 2022-11-12  发布在  其他
关注(0)|答案(2)|浏览(164)

我一直在尝试创建一个自定义客户属性,以允许客户将属性保存到他们的配置文件中。当我用代码更新我的Magento站点时,我没有看到前端更改,也没有看到我的数据库中有任何更新。我做错了什么,导致了这两个问题?我需要添加一些.phtml更改吗?

安装数据.php

<?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();
        }
    }
f4t66c6m

f4t66c6m1#

请尝试以下操作:

\magento2\app\code\Custom\CustomerAttribute\Block\Widget\Passport.php
/**
 * @return bool
 */

public function isEnabled() {
    $attributeMetadata = $this->_getAttribute('passport');
    return $attributeMetadata ? (bool) $attributeMetadata->isVisible() : false;
}
1aaf6o9v

1aaf6o9v2#

在对代码进行任何重大更改后,请尝试运行以下命令。例如,如果创建了扩展,则可以运行升级命令。

php bin/magento setup:upgrade

php bin/magento setup:static-content:deploy

php bin/magento cache:clean or php bin/magento cache:flush

以上命令应在控制台中运行。

相关问题