1

Hi i have a custom prodcuct attribute i want to show the same attribute in category also under the same group is it possible below is my code please suggest me how to achieve this

<?php
namespace Vendor\Module\Setup;

use Magento\Eav\Setup\EavSetup;
use Magento\Eav\Setup\EavSetupFactory;
use Magento\Framework\Setup\InstallDataInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface;

class InstallData implements InstallDataInterface
{
    /**
     * EAV setup factory
     *
     * @var EavSetupFactory
     */
    private $eavSetupFactory;

    /**
     * Init
     *
     * @param EavSetupFactory $eavSetupFactory
     */
    public function __construct(EavSetupFactory $eavSetupFactory)
    {
        $this->eavSetupFactory = $eavSetupFactory;
    }

    /**
     * {@inheritdoc}
     * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
     */
    public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
    {
        /** @var EavSetup $eavSetup */
        $eavSetup = $this->eavSetupFactory->create(['setup' => $setup]);

        /**
         * Add attributes to the eav_attribute
         */
        $eavSetup->removeAttribute(\Magento\Catalog\Model\Product::ENTITY, 'miscellaneous');
        $eavSetup->removeAttribute(\Magento\Catalog\Model\Product::ENTITY, 'miscellaneous');

        $eavSetup->addAttribute(
            \Magento\Catalog\Model\Product::ENTITY,
            'miscellaneous_meta',
            [
                'group' => 'Search Engine Optimization',
                'type' => 'text',
                'backend' => '',
                'frontend' => '',
                'label' => 'Meta Miscellaneous',
                'input' => 'text',
                'class' => '',
                'source' => '',
                'global' => ScopedAttributeInterface::SCOPE_GLOBAL,
                'visible' => true,
                'required' => false,
                'user_defined' => false,
                'default' => '',
                'searchable' => false,
                'filterable' => false,
                'comparable' => false,
                'is_used_in_grid' => true,
                'visible_on_front' => false,
                'used_in_product_listing' => false,
                'unique' => false
            ]
        );

        
    }
}

update

<?php
namespace Vendor\Module\Setup;

use Magento\Eav\Setup\EavSetup;
use Magento\Eav\Setup\EavSetupFactory;
use Magento\Framework\Setup\InstallDataInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface;

class InstallData implements InstallDataInterface
{
    /**
     * EAV setup factory
     *
     * @var EavSetupFactory
     */
    private $eavSetupFactory;

    /**
     * Init
     *
     * @param EavSetupFactory $eavSetupFactory
     */
    public function __construct(EavSetupFactory $eavSetupFactory)
    {
        $this->eavSetupFactory = $eavSetupFactory;
    }

    /**
     * {@inheritdoc}
     * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
     */
    public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
    {
        /** @var EavSetup $eavSetup */
        $eavSetup = $this->eavSetupFactory->create(['setup' => $setup]);

        /**
         * Add attributes to the eav_attribute
         */
        $eavSetup->removeAttribute(\Magento\Catalog\Model\Product::ENTITY, 'miscellaneous');
        $eavSetup->removeAttribute(\Magento\Catalog\Model\Product::ENTITY, 'miscellaneous');

        // $statusOptions = 'Rbj\ProductAttribute\Model\Config\Source\StatusOptions';
        $eavSetup->addAttribute(
            \Magento\Catalog\Model\Product::ENTITY,
            'miscellaneous_meta',
            [
                'group' => 'Search Engine Optimization',
                'type' => 'text',
                'backend' => '',
                'frontend' => '',
                'label' => 'Meta Miscellaneous',
                'input' => 'text',
                'class' => '',
                'source' => '',
                'global' => ScopedAttributeInterface::SCOPE_GLOBAL,
                'visible' => true,
                'required' => false,
                'user_defined' => false,
                'default' => '',
                'searchable' => false,
                'filterable' => false,
                'comparable' => false,
                'is_used_in_grid' => true,
                'visible_on_front' => false,
                'used_in_product_listing' => false,
                'unique' => false
            ]
        );

        $eavSetup->addAttribute(
    \Magento\Catalog\Model\Category::ENTITY,
    'miscellaneous_meta',
        [
            'type' => 'text',
            'backend' => '',
            'frontend' => '',
            'input' => 'text',
            'visible'  => true,
            'default'  => '',
            'required' => false,
            'global'   => ScopedAttributeInterface::SCOPE_STORE,
            'group' => 'Search Engine Optimization'
        ]
    );
    }
}

1 Answer 1

1

it's not possible to share same attribute between entities.

But you can create same attribute for different entities.

Here an example on how to create product attribute:

public function execute()
{
    $eavSetup->addAttribute(
        \Magento\Catalog\Model\Product::ENTITY,
        'miscellaneous_meta',
        [
            'type' => 'varchar',
            'group' => 'Search Engine Optimization',
            'backend' => '',
            'frontend' => '',
            'input' => 'text',
            'class' => '',
            'source' => '',
            'global' => ScopedAttributeInterface::SCOPE_GLOBAL,
            'visible' => true,
            'required' => false,
            'user_defined' => true,
            'default' => '',
            'searchable' => false,
            'filterable' => false,
            'comparable' => false,
            'visible_on_front' => true,
            'used_in_product_listing' => true,
            'unique' => false,
            'apply_to' => ''
        ]
    );
}

Here example for categories:

public function execute()
{
    $eavSetup->addAttribute(
    \Magento\Catalog\Model\Category::ENTITY,
    'miscellaneous_meta',
        [
            'type' => 'varchar',
            'backend' => '',
            'frontend' => '',
            'input' => 'text',
            'visible'  => true,
            'default'  => '',
            'required' => false,
            'global'   => ScopedAttributeInterface::SCOPE_STORE,
            'group' => 'Search Engine Optimization'
        ]
    );
}

UPDATE

which version of Magento 2 are you using? because on 2.3 "setup install" it's deprecated, you should use Patch Data, following best practice magento: https://devdocs.magento.com/guides/v2.4/extension-dev-guide/declarative-schema/data-patches.html

Cheers

10
  • so you mean it will be visible under catg also 'global' => ScopedAttributeInterface::SCOPE_STORE,
    – Pramod
    Aug 7, 2020 at 14:28
  • yes, try it. I use this code in my code base and it has always worked so far Aug 7, 2020 at 14:31
  • check my updated code
    – Pramod
    Aug 7, 2020 at 14:32
  • your updated code it's good only for product attribute. If you want the same attribute for category entity follow the guide Aug 7, 2020 at 14:34
  • please update the above code and mention changes
    – Pramod
    Aug 7, 2020 at 14:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.