Attributes 正在尝试在Magento 2.1中添加客户属性

Attributes 正在尝试在Magento 2.1中添加客户属性,attributes,magento2,Attributes,Magento2,这不起作用让我很沮丧 我试着按照多个教程介绍如何做到这一点,但没有一个是有效的 运行Magento 2.1.5 我只是想创建一个客户属性。我的Setup/UpgradeData.php脚本如下: 名称空间通配符\CustomerMods\Setup use Magento\Customer\Model\Customer; use Magento\Customer\Setup\CustomerSetupFactory; use Magento\Framework\Setup\ModuleConte

这不起作用让我很沮丧

我试着按照多个教程介绍如何做到这一点,但没有一个是有效的

运行Magento 2.1.5

我只是想创建一个客户属性。我的
Setup/UpgradeData.php
脚本如下:

名称空间通配符\CustomerMods\Setup

use Magento\Customer\Model\Customer;
use Magento\Customer\Setup\CustomerSetupFactory;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Framework\Setup\UpgradeDataInterface;

class UpgradeData implements UpgradeDataInterface
{

    /**
     * Customer setup factory
     *
     * @var CustomerSetupFactory
     */
    private $customerSetupFactory;

    /**
     * Init
     *
     * @param CustomerSetupFactory $customerSetupFactory
     */
    public function __construct(CustomerSetupFactory $customerSetupFactory)
    {
        $this->customerSetupFactory = $customerSetupFactory;
    }

    /**
     * Installs DB schema for a module
     *
     * @param ModuleDataSetupInterface $setup
     * @param ModuleContextInterface   $context
     *
     * @return void
     */
    public function upgrade(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
    {

        $installer = $setup;
        $installer->startSetup();

        $customerSetup = $this->customerSetupFactory->create(['setup' => $setup]);

        $customerSetup->removeAttribute(Customer::ENTITY, "customer_payment_type");

        $customerSetup->addAttribute(Customer::ENTITY, "customer_payment_type", array(
            "type"     => "varchar",
            "backend"  => "",
            "label"    => "Payment Type",
            "input"    => "select",
            "source"   => 'Wildcard\CustomerMods\Model\Config\Source\Customer\PaymentTypeOptions',
            "visible"  => true,
            "required" => true,
            "default"  => "",
            "frontend" => "",
            "unique"   => false,
            "note"     => ""

        ));

        $my_attribute    = $customerSetup->getEavConfig()->getAttribute(Customer::ENTITY, ' customer_payment_type');
        $used_in_forms[] = "adminhtml_customer";
        $used_in_forms[] = "customer_account_create";
        $used_in_forms[] = "customer_account_edit";
        $my_attribute->setData("used_in_forms", $used_in_forms)
                     ->setData("is_used_for_customer_segment", true)
                     ->setData("is_system", 0)
                     ->setData("is_user_defined", 0)
                     ->setData("is_visible", 1)
                     ->setData("sort_order", 100);
        $my_attribute->save();
        $installer->endSetup();
    }
}
我已尝试增加
module.xml
中的版本号。这是在
setup\u模块
表中获取的,但它只是拒绝创建属性!我正在查看管理区域(新客户和编辑客户)以及数据库中的
eav\u属性表。该属性未出现在任何一个中

上面的代码有问题吗

如果能从Magento那里看到一些关于这方面的调试信息就好了,但是日志中根本没有任何内容


有人能帮忙吗?我只是想把头发拔出来

以下代码将创建客户属性,您可以将其与代码进行比较。(Magento 2.1.5上的工作代码)

app/code/Jworks/CustomerSetup/etc/module.xml

<?xml version="1.0" encoding="UTF-8"?>
<!--
/**
 *
 * @category    Jworks
 * @package     Jworks_CustomerSetup
 * @author Jitheesh V O <jitheeshvo@gmail.com>
 * @copyright Copyright (c) 2017 Jworks Digital (/)
 */
-->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
    <module name="Jworks_CustomerSetup" setup_version="0.1.0">
        <sequence>
            <module name="Magento_Theme"/>
        </sequence>
    </module>
</config>

app/code/Jworks/CustomerSetup/registration.php

<?php
/**
 *
 * @category    Jworks
 * @package     Jworks_CustomerSetup
 * @author Jitheesh V O <jitheeshvo@gmail.com>
 * @copyright Copyright (c) 2017 Jworks Digital (/)
 */
\Magento\Framework\Component\ComponentRegistrar::register(
    \Magento\Framework\Component\ComponentRegistrar::MODULE,
    'Jworks_CustomerSetup',
    __DIR__
);
<?php
/**
 * Upgrade Data setup Script
 * @category    Jworks
 * @package     Jworks_CustomerSetup
 * @author Jitheesh V O <jitheeshvo@gmail.com>
 * @copyright Copyright (c) 2017 Jworks Digital (/)
 */
namespace Jworks\CustomerSetup\Setup;


use Magento\Customer\Model\Customer;
use Magento\Customer\Setup\CustomerSetupFactory;
use Magento\Framework\App\ObjectManager;
use Magento\Framework\Indexer\IndexerRegistry;
use Magento\Framework\Setup\SetupInterface;
use Magento\Framework\Setup\UpgradeDataInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Store\Model\ScopeInterface;
use Magento\Store\Model\StoreManagerInterface;

class UpgradeData implements UpgradeDataInterface
{
    /**
     * Customer setup factory
     *
     * @var CustomerSetupFactory
     */
    protected $customerSetupFactory;

    /**
     * @var IndexerRegistry
     */
    protected $indexerRegistry;

    /**
     * @var \Magento\Eav\Model\Config
     */
    protected $eavConfig;


    /**
     * @param CustomerSetupFactory $customerSetupFactory
     * @param IndexerRegistry $indexerRegistry
     * @param \Magento\Eav\Model\Config $eavConfig
     */
    public function __construct(
        CustomerSetupFactory $customerSetupFactory,
        IndexerRegistry $indexerRegistry,
        \Magento\Eav\Model\Config $eavConfig
    )
    {
        $this->customerSetupFactory = $customerSetupFactory;
        $this->indexerRegistry = $indexerRegistry;
        $this->eavConfig = $eavConfig;
    }

    /**
     * @param ModuleDataSetupInterface $setup
     * @param ModuleContextInterface $context
     */
    public function upgrade(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
    {
        $setup->startSetup();
        /** @var CustomerSetup $customerSetup */
        $customerSetup = $this->customerSetupFactory->create(['setup' => $setup]);

        if (version_compare($context->getVersion(), '0.1.0', '<')) {

            $this->upgradeVersionZeroOneZero($customerSetup);
        }
        $indexer = $this->indexerRegistry->get(Customer::CUSTOMER_GRID_INDEXER_ID);
        $indexer->reindexAll();
        $this->eavConfig->clear();
        $setup->endSetup();
    }

    /**
     * @param $customerSetup
     */
    private function upgradeVersionZeroOneZero($customerSetup)
    {
        $customerSetup->addAttribute(
            Customer::ENTITY,
            'customer_payment_type',
            [
                "type" => "varchar",
                "backend" => "",
                "label" => "Payment Type",
                "input" => "text",
                "visible" => true,
                "required" => true,
                "default" => "",
                "frontend" => "",
                "unique" => false,
                "note" => ""
            ]
        );
        $attribute = $this->eavConfig->getAttribute(\Magento\Customer\Model\Customer::ENTITY, 'customer_payment_type');
        $attribute->setData(
            'used_in_forms',
            ['adminhtml_customer_address', 'customer_address_edit', 'customer_register_address']

        )
            ->setData("is_used_for_customer_segment", true)
            ->setData("is_system", 0)
            ->setData("is_user_defined", 0)
            ->setData("is_visible", 1)
            ->setData("sort_order", 100);
        $attribute->save();
    }
}