2017-12-26 15 views
0

プログラムでカスタム属性を作成しました。Magetno 2:カスタム属性が機能しません

PHPのbin/Magentoのセットアップを:: PHPビン/ Magentoのキャッシュをアップグレード:クリーン

をした後、それはpanel.Iは後InstallData.php & Options.phpファイルを作成するには、次のコマンドを使用し、管理して、製品のセクションに表示されていません私は管理者の製品セクションでカスタム属性を見つけることができません。

コードは、私は製品がプログラム的にカスタム属性を作成するために使用されるようにあなたは、同様にこれを使用

1 Create InstallData.php 

namespace Matrixsoftware\Matrix\Setup; 

use Magento\Eav\Setup\EavSetup; 
use Magento\Eav\Setup\EavSetupFactory /* For Attribute create */; 
use Magento\Framework\Setup\InstallDataInterface; 
use Magento\Framework\Setup\ModuleContextInterface; 
use Magento\Framework\Setup\ModuleDataSetupInterface; 


class InstallData implements InstallDataInterface 
{ 

    private $eavSetupFactory; 

    public function __construct(EavSetupFactory $eavSetupFactory) 
    { 
     $this->eavSetupFactory = $eavSetupFactory; 

    } 


    public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context) 
    { 
     $setup->startSetup(); 

     /** @var EavSetup $eavSetup */ 
     $eavSetup = $this->eavSetupFactory->create(['setup' => $setup]); 

     $eavSetup->addAttribute(
      \Magento\Catalog\Model\Product::ENTITY, 
      'thickness',/* Custom Attribute Code */ 
      [ 
       'group' => 'Product Details',/* Group name in which you want 
               to display your custom attribute */ 
       'type' => 'int',/* Data type in which formate your value save in database*/ 
       'backend' => '', 
       'frontend' => '', 
       'label' => 'Choose Thickness', /* lablel of your attribute*/ 
       'input' => 'select', 
       'class' => '', 
       'source' => 'Matrixsoftware\Matrix\Model\Config\Source\Options', 
           /* Source of your select type custom attribute options*/ 
       'global' => \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_GLOBAL, 
            /*Scope of your attribute */ 
       'visible' => true, 
       'required' => true, 
       'user_defined' => false, 
       'default' => '', 
       'searchable' => false, 
       'filterable' => true, 
       'comparable' => false, 
       'visible_on_front' => true, 
       'used_in_product_listing' => true, 
       'unique' => false 
      ] 
     ); 
     $setup->endSetup(); 
    } 
} 

2.Create Options.php 

namespace Matrixsoftware\Matrix\Model\Config\Source; 

use Magento\Eav\Model\ResourceModel\Entity\Attribute\OptionFactory; 
use Magento\Framework\DB\Ddl\Table; 

class Options extends \Magento\Eav\Model\Entity\Attribute\Source\AbstractSource 
{ 

    protected $optionFactory; 

    /*public function __construct(OptionFactory $optionFactory) 
    { 
     $this->optionFactory = $optionFactory; 
     //you can use this if you want to prepare options dynamically 
    }*/ 

    public function getAllOptions() 
    { 
     /* your Attribute options list*/ 
     $this->_options=[ ['label'=>'Select Options', 'value'=>''], 
          ['label'=>'Option1', 'value'=>'1'] 
          ['label'=>'Option2', 'value'=>'2'] 
          ['label'=>'Option3', 'value'=>'3'] 
         ]; 
     return $this->_options; 
    } 

    public function getOptionText($value) 
    { 
     foreach ($this->getAllOptions() as $option) { 
      if ($option['value'] == $value) { 
       return $option['label']; 
      } 
     } 
     return false; 
    } 

    public function getFlatColumns() 
    { 
     $attributeCode = $this->getAttribute()->getAttributeCode(); 
     return [ 
      $attributeCode => [ 
       'unsigned' => false, 
       'default' => null, 
       'extra' => null, 
       'type' => Table::TYPE_INTEGER, 
       'nullable' => true, 
       'comment' => 'Custom Attribute Options ' . $attributeCode . ' column', 
      ], 
     ]; 
    } 
} 

答えて

0

です。

アプリケーション/コード/ [ベンダー]/[モジュール] /Setup/InstallData.php

namespace [Vendor]\[Module]\Setup; 
use Magento\Eav\Setup\EavSetup; 
use Magento\Eav\Setup\EavSetupFactory; 
use Magento\Framework\DB\Ddl\Table; 
use Magento\Framework\Setup\InstallDataInterface; 
use Magento\Framework\Setup\ModuleContextInterface; 
use Magento\Framework\Setup\ModuleDataSetupInterface; 
use Magento\Sales\Setup\SalesSetupFactory; 
use Magento\Quote\Setup\QuoteSetupFactory; 

class InstallData implements InstallDataInterface 
{ 

private $eavSetupFactory; 
private $quoteSetupFactory; 
private $salesSetupFactory; 

/** 
* InstallData constructor. 
* @param EavSetupFactory $eavSetupFactory 
* @param QuoteSetupFactory $quoteSetupFactory 
*/ 
public function __construct(
    EavSetupFactory $eavSetupFactory, 
    QuoteSetupFactory $quoteSetupFactory, 
    SalesSetupFactory $salesSetupFactory 
) 
{ 
    $this->eavSetupFactory = $eavSetupFactory; 
    $this->quoteSetupFactory = $quoteSetupFactory; 
    $this->salesSetupFactory = $salesSetupFactory; 
} 

public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context) 
{ 
    $eavSetup = $this->eavSetupFactory->create(['setup' => $setup]); 
    $quoteSetup = $this->quoteSetupFactory->create(['setup' => $setup]); 
    $salesSetup = $this->salesSetupFactory->create(['setup' => $setup]); 

    /** 
    * Add attributes to the eav/attribute 
    */ 
    $eavSetup->addAttribute(
     \Magento\Catalog\Model\Product::ENTITY, 
     'dropdown_attribute', 
     [ 
      'type'     => 'int', 
      'label'     => 'Dropdown Attribute', 
      'input'     => 'select', 
      'global'     => \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_GLOBAL, 
      'visible'     => true, 
      'required'    => false, 
      'user_defined'   => true, 
      'default'     => '', 
      'searchable'    => false, 
      'filterable'    => false, 
      'comparable'    => false, 
      'visible_on_front'  => false, 
      'used_in_product_listing' => false, 
      'unique'     => false, 
      'option'     => [ 
       'values' => [ 
        'Option 1', 
        'Option 2', 
        'Option 3' 
       ], 
      ] 
     ] 
    ); 

    $attributeSetId = $eavSetup->getDefaultAttributeSetId('catalog_product'); 
    $eavSetup->addAttributeToSet(
     'catalog_product', 
     $attributeSetId, 
     'General', 
     'dropdown_attribute' 
    ); 

    $attributeOptions = [ 
     'type'  => Table::TYPE_TEXT, 
     'visible' => true, 
     'required' => false 
    ]; 
} 
} 
関連する問題