4

I'm trying to set labels for attributes for each language. For example I want to translate Price (in Stores > Attributes > Product > Price > Manage labels) for all the store views. enter image description here

However, I can't seem to find a way to do this via UpgradeData.php. I don't want to directly inject with an SQL query. Looking for something like this, but it doesn't work:

if (version_compare($context->getVersion(), '0.0.xx') < 0) {
        $eavSetup = $this->eavSetupFactory->create(['setup' => $setup]);
        $attributeId = $eavSetup->getAttributeId('catalog_product', 'price');
        $options = [
            'values' => [
                'Dutch(NL)' => 'Prijs',
            ],
            'attribute_id' => $attributeId,
        ];
        $eavSetup->addAttributeOption($options);
    }

1 Answer 1

9

You can set the frontend labels with the following code in your UpgradeData or InstallData script:

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

    $attribute = $this->productAttributeRepository->get($attributeCode);
    $sourceModel = $attribute->getSourceModel();
    $frontendLabels = [
        $this->attributeFrontendLabelFactory->create()
            ->setStoreId(1)
            ->setLabel('Label 1'),
        $this->attributeFrontendLabelFactory->create()
            ->setStoreId(2)
            ->setLabel('Label 2'),
    ];
    $attribute->setFrontendLabels($frontendLabels);
    $this->productAttributeRepository->save($attribute);

    if ($sourceModel !== null) {
        $eavSetup->updateAttribute(
            \Magento\Catalog\Model\Product::ENTITY,
            $attributeCode,
            \Magento\Catalog\Api\Data\EavAttributeInterface::SOURCE_MODEL,
            $sourceModel
        ); // this is needed as the attribute repository throws away the source model while saving in Magento 2.2.5
    }

$this->eavSetupFactory is of type \Magento\Eav\Setup\EavSetupFactory.

$this->productAttributeRepository is of type \Magento\Catalog\Api\ProductAttributeRepositoryInterface.

$this->attributeFrontendLabelFactory is of type \Magento\Eav\Api\Data\AttributeFrontendLabelInterfaceFactory.

You should inject them via Dependency Injection in the constructor.

2
  • Thank you! I've been also trying to keep the already set labels by getting labels by getFrontendLabels() and appending my store label, and since this function return type is clearly an array * @return \Magento\Eav\Api\Data\AttributeFrontendLabelInterface[] I expected an array merge would suffice but kept getting this warning that "array_merge(): Argument #1 is not an array", I'm gonna see it through ...
    – medmek
    Commented Sep 9, 2018 at 20:06
  • $eavSetup->updateAttribute(...) not needed anymore, at least from Magento 2.3.5 or higher. Commented Nov 3, 2022 at 11:28

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.