3

How can I make custom category attributes being saved in to different store views, in Magento 2.3.3, and not only global?

Change 'global' => 1 to 'global' => 0 doesn't seem to make any difference.

use Magento\Framework\Setup\InstallDataInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Eav\Setup\EavSetupFactory;

class InstallData implements InstallDataInterface
{

    private $eavSetupFactory;

    /**
     * Constructor
     *
     * @param \Magento\Eav\Setup\EavSetupFactory $eavSetupFactory
     */
    public function __construct(EavSetupFactory $eavSetupFactory)
    {
        $this->eavSetupFactory = $eavSetupFactory;
    }

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

        $eavSetup->addAttribute(
            \Magento\Catalog\Model\Category::ENTITY,
            'custom_category_field',
            [
                'type' => 'varchar',
                'label' => 'Category Custom field',
                'input' => 'text',
                'sort_order' => 100,
                'source' => '',
                'global' => 1,
                'visible' => true,
                'required' => false,
                'user_defined' => false,
                'default' => null,
                'group' => 'General Information',
                'backend' => ''
            ]
        );
    }
}

EDIT

Working example efter help from @Haijerome

$eavSetup->addAttribute(
            \Magento\Catalog\Model\Category::ENTITY,
            'category_heading',
            [
                'type' => 'varchar',
                'label' => 'Category Heading',
                'input' => 'text',
                'sort_order' => 100,
                'source' => '',
                'global' => \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_STORE,
                'visible' => true,
                'required' => false,
                'user_defined' => false,
                'default' => null,
                'group' => 'General Information',
                'backend' => ''
            ]
        );
2
  • 1
    Did you saved value for your custom attribute store wise ? and check that ? Because I created one attribute and saved that store wise and that is working but "Use Default Value" checkbox only not coming in my case. Nov 16, 2019 at 16:37
  • Yes I did, but it didn't work. I installed the custom module with scope set to 0. But when that didn't work I changed to 1 and ran setup upgrade again, still no success.
    – Aknot
    Nov 16, 2019 at 20:06

1 Answer 1

3

Category entity attributes can be set to the following scopes. Source

const SCOPE_STORE = 0;
const SCOPE_GLOBAL = 1;
const SCOPE_WEBSITE = 2;

You can use set the 'global' value to Store scope by using the code below (but I can see you have used 0 already. Try to upgrade your data and set the global value of the attribute as given below )

Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_STORE

3
  • Thanks for your input. I'm not sure exactly what to do. Where should I put that line of code @Haijerome? Sorry for not understanding you :-)
    – Aknot
    Nov 16, 2019 at 20:09
  • I figured it out and it's working, thank you!
    – Aknot
    Nov 17, 2019 at 7:04
  • 1
    @Alldo glad that you get it done. Sorry that i was not on my machine at that time to give you the exact piece of code.
    – Haijerome
    Nov 21, 2019 at 0:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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