2017-05-02 22 views
0

Magento 2.1で顧客属性を追加しようとしています

私はこれを行う方法について複数のチュートリアルを試してみましたが、どれも動作していません。

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_moduleテーブルで取得されますが、属性の作成を拒否します。私は管理領域(新規顧客と編集顧客)とデータベース内のeav_attributeテーブルを調べています。属性はどちらにも表示されません。

上記のコードに何か問題がありますか?

Magentoのデバッグ情報を見ると素晴らしいですが、ログには何もありません。

誰か助けてもらえますか?私はちょうど私の髪を引っ張っている!

答えて

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 <[email protected]> 
* @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 <[email protected]> 
* @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 <[email protected]> 
* @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(); 
    } 
} 
関連する問題