Magento 2:自定义属性不起作用

问题描述 投票:0回答:1

我以编程方式创建了自定义属性。它没有显示在管理面板的产品部分。我在创建InstallData.php和Options.php文件后使用了以下命令:

php bin / magento setup:升级php bin / magento cache:clean

之后,我无法在管理员的产品部分找到自定义属性。

代码是

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',
            ],
        ];
    }
}
magento magento2
1个回答
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
    ];
}
}
© www.soinside.com 2019 - 2024. All rights reserved.