Reputation: 6266
I have started using magento as my ecommerce cms and I know that is an extremely powerful platform. Recently I came across its functionality that helps the developer extending the core and I have managed to add custom category options. Is there any chance to achieve the same results on an attribute? I would like to add a text description on the properties' tab and display it on the front end?
Upvotes: 3
Views: 5183
Reputation: 61
For Magento 2 (Adding simple Yes/No property for product attributes).
It could be used ui_component "product_attribute_add_form.xml" (example could be find in vendor modules)
OR if it is not working:
<?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">
<column xsi:type="smallint" name="new_column" padding="6" unsigned="false" nullable="false"
identity="false" default="0" comment="Comment for new_column"/>
</table>
</schema>
<?xml version="1.0" ?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
<event name="product_attribute_form_build">
<observer
name="vendor_modulename_observer_backend_product_attributeformbuild"
instance="Vendor\ModuleName\Observer\Backend\Product\AttributeFormBuild"/>
</event>
</config>
<?php
declare(strict_types=1);
namespace Vendor\ModuleName\Observer\Backend\Product;
use Magento\Framework\Module\Manager;
use Magento\Config\Model\Config\Source\Yesno;
class AttributeFormBuild implements \Magento\Framework\Event\ObserverInterface
{
protected Yesno $yesNo;
protected $moduleManager;
/**
* @param Manager $moduleManager
* @param Yesno $yesNo
*/
public function __construct(
Manager $moduleManager,
Yesno $yesNo
)
{
$this->moduleManager = $moduleManager;
$this->yesNo = $yesNo;
}
/**
* @param \Magento\Framework\Event\Observer $observer
* @return void
*/
public function execute(
\Magento\Framework\Event\Observer $observer
) {
if (!$this->moduleManager->isEnabled('Vendor_ModuleName')) {
return;
}
$form = $observer->getForm();
// $fieldset = $form->getElement('base_fieldset');
$fieldset = $form->getElement('advanced_fieldset');
$yesnoSource = $this->yesNo->toOptionArray();
$fieldset->addField(
'new_column',
'select',
[
'name' => 'new_column',
'label' => __('New column label'),
'title' => __('New column title'),
'note' => __('Use this attribute for something'),
'values' => $yesnoSource,
'required' => false,
],
'is_filterable'
);
}
}
Upvotes: 0
Reputation: 2960
This is possible with writing your own custom module. I did it to add a tooltip-option to an attribute.
You can use the adminhtml_catalog_product_attribute_edit_prepare_form
-event to add a field with your observer:
$fieldset = $observer->getForm()->getElement('base_fieldset');
$fieldset->addField('tooltip', 'text', array(
'name' => 'tooltip',
'label' => Mage::helper('catalog')->__('Tooltip'),
'title' => Mage::helper('catalog')->__('Tooltip')
));
This adds the extra field to the attribute-edit screen. Next up is to make sure the property gets saved when editing your attribute. This can be done in your installer script with something like:
$installer->getConnection()->addColumn(
$installer->getTable('catalog/eav_attribute'),
'tooltip',
array(
'type' => Varien_Db_Ddl_Table::TYPE_TEXT,
'nullable' => true,
'comment' => 'Tooltip'
)
);
You also have to make sure your Model/Resource/Setup
-class extends Mage_Eav_Model_Entity_Setup
instead of Mage_Core_Model_Resource_Setup
.
Now, at this point, you can save your custom attribute property. Next step is to display it on the frontend. This can be done fairly easy, just by simple Magento templating 101:
For example, in catalog/product/view/type/options/configurable.phtml
in the foreach()
-loop, place something like this, to display the tooltip:
echo $_attribute->getProductAttribute()->getTooltip();
That's nice...
Update: Since I got some questions about this subject by e-mail, I decided to write a more detailed blog post about it. You can read it here: http://gielberkers.com/add-custom-properties-magento-attributes/
Upvotes: 11