0

I want to edit the frontend label attribute. I know how to do this through the database, but I haven’t found anything on the Internet how to change the attribute name

enter image description here

How do I change the attribute name programmatically ?

1

You need a data patch or an upgrade script. I'm leaving an example here. You can run them with bin/magento setup:upgrade or bin/magento setup:db-data:upgrade

declare(strict_types=1);

namespace Vulpea\InternshipEav\Setup\Patch\Data;

use Magento\Eav\Setup\EavSetup;
use Magento\Framework\Setup\Patch\DataPatchInterface;

/**
 * Class UpdateOne
 * @package Vulpea\InternshipEav\Setup\Patch\Data
 * @author Victor Todoran <victor.todoran@yahoo.com>
 */
class UpdateOne implements DataPatchInterface
{
    const ATTRIBUTE_CODE = 'test';
    const FIELD = 'frontend_label';
    const VALUE = 'This value was set via update';

    /**
     * @var EavSetup
     */
    private $eavSetup;

    public function __construct(
        EavSetup $eavSetup
    ) {
        $this->eavSetup = $eavSetup;
    }
    /**
     * Get array of patches that have to be executed prior to this.
     *
     * example of implementation:
     *
     * [
     *      \Vendor_Name\Module_Name\Setup\Patch\Patch1::class,
     *      \Vendor_Name\Module_Name\Setup\Patch\Patch2::class
     * ]
     *
     * @return string[]
     */
    public static function getDependencies()
    {
        return [];
    }

    /**
     * Get aliases (previous names) for the patch.
     *
     * @return string[]
     */
    public function getAliases()
    {
        return [];
    }

    /**
     * Run code inside patch
     * If code fails, patch must be reverted, in case when we are speaking about schema - than under revert
     * means run PatchInterface::revert()
     *
     * If we speak about data, under revert means: $transaction->rollback()
     *
     * @return $this
     */
    public function apply()
    {
        $this->eavSetup->updateAttribute(
              \Magento\Catalog\Model\Product::ENTITY,
              self::ATTRIBUTE_CODE,
              self::FIELD,
                self::VALUE
        );
        return $this;
    }
}
|improve this answer|||||
1
// attributeId: you want update attribute id
$attributeModel = \Magento\Framework\App\ObjectManager::getInstance()
          ->get(\Magento\Eav\Model\Attribute::class)->load($attributeId);
$customAttributeValue = "you need custom attribute value";
$attributeModel->setData('frontend_label',$customAttributeValue);
$attributeModel->save();
|improve this answer|||||

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.