3

I have created plugin for add textbox in product attribute form.

enter image description here

di.xml

<?xml version="1.0"?>

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <type name="Magento\Catalog\Block\Adminhtml\Product\Attribute\Edit\Tab\Front">
        <plugin name="extension_attribute_edit_form" type="Vendor\Module\Plugin\Block\Adminhtml\Product\Attribute\Edit\Tab\Front" sortOrder="1"/>
    </type>
</config>

Vendor\Module\Plugin\Block\Adminhtml\Product\Attribute\Edit\Tab\Front.php

<?php

namespace Vendor\Module\Plugin\Block\Adminhtml\Product\Attribute\Edit\Tab;

class Front
{
public function aroundGetFormHtml(
    \Magento\Catalog\Block\Adminhtml\Product\Attribute\Edit\Tab\Front $subject,
    \Closure $proceed
)
{
    $form = $subject->getForm();
    $fieldset = $form->getElement('front_fieldset');
    $fieldset->addField(
        'stn_price',
        'text',
        [
            'name' => 'stn_price',
            'label' => __('Stone Price'),
            'title' => __('Stone Price'),
            'required' => false
        ]
    );
    return $proceed();
}
}

Vendor\Module\etc\db_schema.xml

<?xml version="1.0"?>
<schema xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:noNamespaceSchemaLocation="urn:magento:framework:Setup/Declaration/Schema/etc/schema.xsd">
<table name="catalog_eav_attribute" resource="default" engine="innodb" comment="Catalog EAV Attribute Table">
    <column xsi:type="int" name="stn_price" unsigned="true" nullable="false"
            identity="false" default="0" comment="Stone Weight"/>
</table>
</schema>
2
  • Is it possible to save data already? Commented Sep 21, 2022 at 13:15
  • No, now i have added manually but i will save with observer Commented Sep 21, 2022 at 14:04

1 Answer 1

0

First of all you need to create a data patch to create field in eav_attribute table. It will save data in db and fetch data in product attribute form

<?php

namespace Vendor\Module\Setup\Patch\Data;

use Magento\Catalog\Model\Product;
use Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface;
use Magento\Eav\Setup\EavSetup;
use Magento\Eav\Setup\EavSetupFactory;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Framework\Setup\Patch\DataPatchInterface;

class Data implements DataPatchInterface
{
    /**
     * @var ModuleDataSetupInterface
     */
    private $moduleDataSetup;

    /**
     * @var EavSetupFactory
     */
    private $eavSetupFactory;

    public function __construct(
        ModuleDataSetupInterface $moduleDataSetup,
        EavSetupFactory $categorySetup
    ) {
        $this->moduleDataSetup = $moduleDataSetup;
        $this->eavSetupFactory = $eavSetupFactory;
    }

    public function apply()
    {
        $eavSetup = $this->eavSetupFactory->create(['setup' => $this->moduleDataSetup]);
        $eavSetup->addAttribute(
            Product::ENTITY,
            'stn_price',
            [
                'type' => 'text',
                'label' => 'Stone Price',
                'required' => false,
                'visible' => 1,
                'visible_on_front' => 1,
                'global' => ScopedAttributeInterface::SCOPE_STORE,
            ]
        );
    }

    public static function getDependencies()
    {
        return [];
    }

    public function getAliases()
    {
        return [];
    }
}

After creating this file

Run commands:

php bin/magento setup:upgrade and others as required

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.