Creating Custom Fields in Magento Admin: A Step-by-Step Guide
When working with Magento admin panels, there often comes a time when you need to add custom fields to the product creation process. In this guide, we’ll walk through how to create a new field in the “Add Products” page, specifically for a subtitle or alternative name.
Understanding Magento’s Field Creation Process
Before diving into code, it’s crucial to understand that Magento uses an XML file (catalog_product_view.xml) to define the layout of the admin panel. We’ll be making changes here and also modifying the product model to handle our new field.
Step 1: Create a Custom Module
First, create a custom module directory structure:
/app/code/Vendor/CustomModule/etc
/app/code/Vendor/CustomModule/etc/module.xml
/app/code/Vendor/CustomModule/Setup
/app/code/Vendor/CustomModule/Setup/InstallData.php
/app/code/Vendor/CustomModule/Model
/app/code/Vendor/CustomModule/Model/Product.php
Step 2: Define the XML Layout
In module.xml, define your module:
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<module name="Vendor_CustomModule" setup_version="1.0.0">
<sequence>
<module name="Magento_Catalog"/>
</sequence>
</module>
</config>
Step 3: Create the Custom Field
We’ll add a new field to our product model.
In Model/Product.php, add this code:
namespace Vendor\CustomModule\Model;
use Magento\Framework\App\ResourceConnection;
use Magento\Catalog\Model\ProductFactory as ProductFactory;
class Product extends \Magento\Catalog\Model\Product
{
const SUBTITLE = 'vendor_custom_module_product_subtitle';
/**
* @var string
*/
protected $_eventPrefix = 'catalog_product_save';
public function __construct(
ResourceConnection $resource,
ProductFactory $productFactory,
array $data = []
) {
parent::__construct($resource, $productFactory, $data);
}
public function getSubtitle()
{
return $this->getData(self::SUBTITLE);
}
public function setSubtitle($subtitle)
{
return $this->setData(self::SUBTITLE, $subtitle);
}
}
Step 4: Updating the Layout
In your catalog_product_view.xml file located in app/code/Vendor/CustomModule/view/adminhtml/layout, add this XML:
<layout xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/layout.xsd">
<adminhtml_catalog_product_edit>
<referenceBlock name="product_form_data" >
<block class="Magento\Catalog\Block\Adminhtml\Form\Field" as="subtitle_field"
template="Vendor_CustomModule::product/subtitle.phtml"/>
</referenceBlock>
</adminhtml_catalog_product_edit>
</layout>
Step 5: Create the Template
Create a subtitle.phtml file in the following directory:
app/code/Vendor/CustomModule/view/adminhtml/templates/product/
In this file, add your HTML and JavaScript:
<div class="admin__field field">
<label for="vendor_custom_module_product_subtitle" class="admin__field-label required"><?php echo __('Subtitle') ?>:</label>
<div class="admin__field-control">
<input type="text"
name="vendor_custom_module_product_subtitle"
id="vendor_custom_module_product_subtitle"
value="<?php echo $block->getProduct()->getData('vendor_custom_module_product_subtitle'); ?>"
title="<?php echo __('Subtitle') ?>"
class="admin__field-control input-text"
data-ui-id="product-form-product-subtitle">
</div>
</div>
Step 6: Save Your Changes
Clear the Magento cache, and your new field should now appear in the “Add Products” page.
Conclusion
Creating custom fields in Magento admin is a straightforward process once you understand how to modify the XML layouts and update the product model. This guide demonstrates how to add a subtitle or alternative name field to your products using a simple yet effective approach.
Remember, when working with Magento, always ensure you have a backup of your data before making any changes. Additionally, keep your Magento version up-to-date for optimal performance and security.
For more advanced customizations and best practices in Magento development, be sure to check out the resources available at Laravel Company.