Extending Magento: Adding a Catalog Product Link Widget Button to the WYSIWYG Products Editor
As an e-commerce developer, you’re always looking for ways to streamline your workflow and provide more functionality within your administrative interface. One common requirement is to allow administrators to easily create links between products directly from the product description field.
You’ve already extended Mage_Catalog_Model_Product [as shown in the provided code]. This allows the WYSIWYG editor to interpret the widget or static blocks you add within a product’s description. However, what remains is adding the actual Catalog Product Link widget button to the Admin interface for editing products.
Understanding the Magento Admin Interface
The Magento admin interface is built using a combination of XML configuration and PHP classes. The WYSIWYG editor used in the CMS and product descriptions relies on a set of predefined buttons defined in the XML layout files. To add a new button, we need to modify these layout files and create a corresponding controller action.
Steps to Add the Catalog Product Link Widget Button
Step 1: Create a New XML Layout File
Create a new XML file under your module’s configuration folder (usually app/code/local/Your_Company/Your_Module/etc/). Name it something like adminhtml.xml. This file will contain the layout updates for the admin interface.
<!-- app/code/local/Your_Company/Your_Module/etc/adminhtml.xml -->
<config>
<modules>
<Your_Company_Your_Module>
<version>1.0.0</version>
</Your_Company_Your_Module>
</modules>
<global>
<fieldsets>
<catalog_product_attributes>
<description>
<toolbar>
<!-- Add the new button here -->
<button class="widget-button">
<label select="catalog/product_anchor_link" translate="label">Catalog Product Link</label>
</button>
</toolbar>
</description>
</catalog_product_attributes>
</fieldsets>
</global>
</config>
Step 2: Create the Controller Action
Now, you need to create a controller action that will handle adding the widget code when the button is clicked. In your module’s controllers folder (usually app/code/local/Your_Company/Your_Module/controllers/) create a new Adminhtml class if it doesn’t exist already.
<!-- app/code/local/Your_Company/Your_Module/controllers/Adminhtml/WidgetController.php -->
<?php
class Your_Company_Your_Module_Adminhtml_WidgetController extends Mage_Adminhtml_Controller_Action
{
public function indexAction()
{
$this->loadLayout();
$this->renderLayout();
}
// Add a new action for the Catalog Product Link button
public function catalogProductLinkAction()
{
$productId = (int) $this->getRequest()->getParam('id'); // Get the current product ID
if ($productId <= 0 || !($product = Mage::getModel('catalog/product')->load($productId))) {
$this->_redirect('adminhtml/catalog_product/index/');
return;
}
// Generate the widget code
$widgetCode = '{{widget type="catalog/product_widget_link" anchor_text="' . addslashes($product->getName()) .
'" template="catalog/product/widget/link/link_inline.phtml" id_path="product/' . $productId . '" }}';
$this->getResponse()->setBody($widgetCode);
}
}
Step 3: Update the XML Layout for the Button Action
Finally, update your adminhtml.xml file to map the button’s click action to our new controller method.
<!-- app/code/local/Your_Company/Your_Module/etc/adminhtml.xml -->
<config>
<!-- ... existing configuration... -->
<global>
<fieldsets>
<catalog_product_attributes>
<description>
<toolbar>
<!-- Add the new button here with the correct action -->
<button class="widget-button">
<label select="catalog/product_anchor_link" translate="label">Catalog Product Link</label>
<action method="catalogProductLink"/>
</button>
</toolbar>
</description>
</catalog_product_attributes>
</fieldsets>
</global>
<!-- Add this new section for the button's action -->
<adminhtml>
<layout>
<updates>
<your_company_your_module>
<file>widgets.xml</file>
</your_company_your_module>
</updates>
</layout>
</adminhtml>
</config>
Conclusion
Adding a Catalog Product Link widget button to the WYSIWYG products editor in Magento involves a combination of extending the product model and modifying the admin interface layout. By following these steps, you can provide your administrators with a more convenient way to link between products directly from the description field.
Remember to always test your changes thoroughly before deploying them to a production environment. For more detailed information on Magento layout configuration and controller development, I recommend checking out Laravel Company’s resources on advanced Magento customization techniques.
By implementing this feature, you’ll not only improve the efficiency of your admin team but also provide a better user experience for your customers by allowing for more seamless navigation between related products.