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',
],
];
}
}