Magento 2:自定义属性不起作用
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Magento 2:自定义属性不起作用相关的知识,希望对你有一定的参考价值。
我以编程方式创建了自定义属性。它没有显示在管理面板的产品部分。我在创建InstallData.php和Options.php文件后使用了以下命令:
php bin / magento setup:升级php bin / magento cache:clean
之后,我无法在管理员的产品部分找到自定义属性。
代码是
1 Create InstallData.php
namespace MatrixsoftwareMatrixSetup;
use MagentoEavSetupEavSetup;
use MagentoEavSetupEavSetupFactory /* For Attribute create */;
use MagentoFrameworkSetupInstallDataInterface;
use MagentoFrameworkSetupModuleContextInterface;
use MagentoFrameworkSetupModuleDataSetupInterface;
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(
MagentoCatalogModelProduct::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' => 'MatrixsoftwareMatrixModelConfigSourceOptions',
/* Source of your select type custom attribute options*/
'global' => MagentoEavModelEntityAttributeScopedAttributeInterface::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 MatrixsoftwareMatrixModelConfigSource;
use MagentoEavModelResourceModelEntityAttributeOptionFactory;
use MagentoFrameworkDBDdlTable;
class Options extends MagentoEavModelEntityAttributeSourceAbstractSource
{
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',
],
];
}
}
答案
您也可以使用它,因为我以编程方式创建自定义产品属性。
应用程序/代码/ [供应商] / [模块] /Setup/InstallData.php
namespace [Vendor][Module]Setup;
use MagentoEavSetupEavSetup;
use MagentoEavSetupEavSetupFactory;
use MagentoFrameworkDBDdlTable;
use MagentoFrameworkSetupInstallDataInterface;
use MagentoFrameworkSetupModuleContextInterface;
use MagentoFrameworkSetupModuleDataSetupInterface;
use MagentoSalesSetupSalesSetupFactory;
use MagentoQuoteSetupQuoteSetupFactory;
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(
MagentoCatalogModelProduct::ENTITY,
'dropdown_attribute',
[
'type' => 'int',
'label' => 'Dropdown Attribute',
'input' => 'select',
'global' => MagentoEavModelEntityAttributeScopedAttributeInterface::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
];
}
}
以上是关于Magento 2:自定义属性不起作用的主要内容,如果未能解决你的问题,请参考以下文章
自定义模块中的 Magento Layout xml 不起作用