0
프로그래밍 방식으로 사용자 지정 특성을 만들었습니다.Magetno 2 : 사용자 지정 특성이 작동하지 않습니다.
PHP 빈/젠토 설정 : PHP 빈/젠토 캐시를 업그레이드 : 깨끗한
후에는 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',
],
];
}
}