0

I have setup a working store in English (default store view), but now i wish to add a new language which will be a new store view; 'Italian'. We have alot of attributes that have been added pragmatically and that all works fine.

However, I now wish to add a translated label to all of my product attributes. I have seen the various posts on here but they do not offer a code solution to adding a new label to an existing attribute for a new store view. The original import code is as follows;

namespace CustSoft\Multiselect\Setup;

use Magento\Eav\Setup\EavSetup; 
use Magento\Eav\Setup\EavSetupFactory;
use Magento\Framework\Setup\UpgradeDataInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Catalog\Setup\CategorySetupFactory; 
use Magento\Eav\Model\Entity\Attribute\SetFactory as AttributeSetFactory;

class UpgradeData implements UpgradeDataInterface
{
    /**
     * EAV setup factory
     *
     * @var EavSetupFactory
     */
    private $eavSetupFactory;
    private $attributeSetFactory;
    private $attributeSet;
    private $categorySetupFactory;

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

    /**
     * {@inheritdoc}
     */
    public function upgrade(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
    {

        $setup->startSetup();   

            // CREATE ATTRIBUTE SET 
            $categorySetup = $this->categorySetupFactory->create(['setup' => $setup]);

            $attributeSet = $this->attributeSetFactory->create();
            $entityTypeId = $categorySetup->getEntityTypeId(\Magento\Catalog\Model\Product::ENTITY);
            $attributeSetId = $categorySetup->getDefaultAttributeSetId($entityTypeId);
            $data = [
                'attribute_set_name' => 'Motor Mounts', 
                'entity_type_id' => $entityTypeId,
                'sort_order' => 200,
            ];
            $attributeSet->setData($data);
            $attributeSet->validate();
            $attributeSet->save();
            $attributeSet->initFromSkeleton($attributeSetId);
            $attributeSet->save();      

        $setup->endSetup();



        $setup->startSetup();

        /** @var \Magento\Eav\Setup\EavSetup $eavSetup */
        $eavSetup = $this->eavSetupFactory->create(['setup' => $setup]);

        $c_attr_label = 'Mount - Type';
        $c_attr_name = 'mount_type';

        $eavSetup->addAttribute(
            \Magento\Catalog\Model\Product::ENTITY,
            $c_attr_name,
        [

                'label' => $c_attr_label,
                'type' => 'varchar',
                'input' => 'multiselect',

                'user_defined' => true,                 

                'option' => ['values' => [
                    '210',
                    '270',
                    '340',
                    '430',
                    '490',
                    ],
                ],
                'backend' => 'Magento\Eav\Model\Entity\Attribute\Backend\ArrayBackend',
                'required' => false,
                'visible' => true,
                'visible_on_front' => true,
                'global' => \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_STORE,
                'searchable' => true,
                'filterable' => true,
                'used_for_promo_rule' => true,
                'comparable' => true,
                'wysiwyg_enabled' => false,
                'used_in_product_listing' => true,
                'used_for_sort_by' => false, 
                'filterable' => '1',
                'filterable_in_search' => true,

                'is_filterable_in_grid' => '1',
                'is_used_in_grid' => '1',               
                'wp_show_quantity' => '1'

        ]
        );

        $attributeSetName = 'Motor Mounts';
        $groupName = 'Product Details';
        $attributeCode = $c_attr_name;
        $attributeSetId = $eavSetup->getAttributeSetId($entityTypeId, $attributeSetName);
        $attributeId = $eavSetup->getAttributeId($entityTypeId, $attributeCode);
        $attributeGroupId = $eavSetup->getAttributeGroupId($entityTypeId, $attributeSetId, $groupName);
        $eavSetup->addAttributeToSet($entityTypeId,$attributeSetId, $attributeGroupId, $attributeId);

            $setup->endSetup();
   }
}

I would also note that i set their scope to Store View, so I am wondering if the scope will need changing to work over multiple store views?

Any help with this would be appreciated

2
  • please check this link magento.stackexchange.com/questions/204734/…. It could be helpful in your case. Jan 7 '20 at 13:45
  • @HiteshBalpande Hi thanks, I saw that. Isn't that discussing attributes option labels rather than the label for the attribute itself? In the above example it would be 'Mount - Type' that i would want to change, where i think that discusses changing (in the example above) 210, 270, 340, 430 and 490? - Sorry if i was not clear in my original post
    – Anthony
    Jan 8 '20 at 0:59

Your Answer

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

Browse other questions tagged or ask your own question.