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

时间:2017-07-24 01:00:09

标签: 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\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();
        }
    }

强文

2 个答案:

答案 0 :(得分:0)

尝试在代码中进行重大更改后运行以下命令。例如,如果创建扩展而不是运行upgrade命令。

<强>命令

php bin/magento setup:upgrade

php bin/magento setup:static-content:deploy

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

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

尝试一下,它可能对你有帮助。

答案 1 :(得分:0)

请试试这个: -

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

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