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

时间:2017-05-02 03:42:24

标签: attributes magento2

对此感到非常沮丧。

我已经尝试过关于如何执行此操作的多个教程,但它们都没有工作。

运行Magento 2.1.5。

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

命名空间Wildcard \ 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_module表中选取的,但它只是拒绝创建属性!我正在查看管理区域(新客户和编辑客户)以及数据库中的eav_attribute表。该属性未出现在任何一个中。

上面的代码有问题吗?

很高兴看到Magento的一些调试信息,但日志中根本没有任何内容。

有人可以帮帮忙吗?我只是想把头发拉出来!

1 个答案:

答案 0 :(得分:0)

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

应用/代码/ 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>

应用/代码/ 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__
);

应用/代码/ Jworks / CustomerSetup /设置/ UpgradeData.php

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