Magento provide a feature to manage product attributes from the admin. There we can manage how to use that attributes in frontend and backend. And sometimes based on demand, we need additional information for an individual attribute. For example, include an attribute in the product feed.
So let's see how we can add a new field to capture the additional value for each attribute. Please follow the below steps:
STEP 1: Create a plugin
<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" sortorder="1" type="MageInsight\ProductFeed\Plugin\Block\Adminhtml\Product\Attribute\Edit\Tab\Front">
</plugin></type>
</config>
STEP 2: Next, we will create the “Front” class inside the Plugin folder which will contain the logic of adding a field in the form.
<?php
namespace MageInsight\ProductFeed\Plugin\Block\Adminhtml\Product\Attribute\Edit\Tab;
class Front
{
protected $booleanSource;
protected $registry;
public function __construct(
\Magento\Config\Model\Config\Source\Yesno $booleanSource,
\Magento\Framework\Registry $registry
) {
$this->booleanSource = $booleanSource;
$this->registry = $registry;
}
public function aroundGetFormHtml(
\Magento\Catalog\Block\Adminhtml\Product\Attribute\Edit\Tab\Front $subject,
\Closure $proceed
)
{
$booleanSource = $this->booleanSource->toOptionArray();
$attributeObject = $this->getAttributeObject();
$form = $subject->getForm();
$fieldset = $form->getElement('front_fieldset');
$fieldset->addField(
'used_in_feed',
'select',
[
'name' => 'used_in_feed',
'label' => __('Used in Feed'),
'title' => __('Used in Feed'),
'value' => $attributeObject->getData('used_in_feed'),
'values' => $booleanSource,
]
);
return $proceed();
}
private function getAttributeObject()
{
return $this->registry->registry('entity_attribute');
}
}
STEP 3: Once this plugin is created, we can see the new filed in the Product attribute form. Now we need to add the new field in database so we need to add a new column in catalog_eav_attribute table as below:
<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="smallint" name="used_in_feed" unsigned="true" nullable="false" identity="false" default="0" comment="Used in Feed"/>
</table>
</schema>
Now just execute upgrade command from the command line and your new field is ready to use.
Comments
Post a Comment