0

How can I add an eav attribute for store? I practically need to add a post code but storeview scope. The problem is that the post code for store information has website scope.

I have addedd eav attributes before, but what would be the ENTITY and how can I change to storeview scope?

Something like this:

/** @var EavSetup $eavSetup */
            $eavSetup = $this->eavSetupFactory->create(['setup' => $setup]);
            /**
            * Add attributes to the eav/attribute
            */
            
            $eavSetup->addAttribute(
                \Magento\Store\Model\???::ENTITY,
                'is_featured',
                [
                    'group' => 'General',
                    'type' => 'int',
                    'backend' => '',
                    'frontend' => '',
                    'label' => 'Post Code',
                    'input' => 'text',
                    'class' => '',
                    'source' => 'Magento\Eav\Model\Entity\Attribute\Source\Boolean',
                    'global' => \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_GLOBAL,
                    'visible' => true,
                    'required' => false,
                    'user_defined' => false,
                    'default' => '1',
                    'searchable' => false,
                    'filterable' => false,
                    'comparable' => false,
                    'visible_on_front' => false,
                    'used_in_product_listing' => false,
                    'unique' => false,
                    'apply_to' => ''
                ]
            );

This is the place where I need to add it.

enter image description here

Thanks!

  • Would you add the new filed in store information ,right ? – madani Oct 17 at 5:14
1

Store information is not an EAV attribute so you will have to take a different approach.

It is a system config value stored in the core_config_data table, these values are configured using /etc/adminhtml/system.xml.

Postcode is defined in the Magento_Backend module. To change the scope of this variable you will have to override it and change the scope for postcode that is set in:

/vendor/magento/module-backend/etc/adminhtml/system.xml

I would recommend creating your own module with these files:

app/code/StackExchange/StoreInformation/etc/adminhtml/system.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Config:etc/system_file.xsd">
    <system>
        <section id="general">
            <group id="store_information">
                <field id="postcode" translate="label" type="text" sortOrder="30" showInDefault="1" showInWebsite="1" showInStore="1">
                    <label>ZIP/Postal Code - Override</label>
                </field>
            </group>
        </section>
    </system>
</config>

app/code/StackExchange/StoreInformation/etc/module.xml (the sequence is important here)

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
    <module name="StackExchange_StoreInformation" setup_version="0.1.0">
        <sequence>
            <module name="Magento_Backend"/>
        </sequence>
    </module>
</config>

app/code/StackExchange/StoreInformation/registration.php

<?php
\Magento\Framework\Component\ComponentRegistrar::register(
    \Magento\Framework\Component\ComponentRegistrar::MODULE,
    'StackExchange_StoreInformation',
    __DIR__
);

Enable the module from cli Run setup:upgrade

You will then have the attribute in the correct scope as expected:

enter image description here

| improve this answer | |
New contributor
Kristoffer is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.

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.