Extending Magento Category Edit Page with Product Grid Tab
In this guide, we’ll walk through adding a new tab to the category edit page in Magento that contains a product grid. This is useful when you want to associate specific products with each category for special promotions or layouts.
Current Progress and Challenges
The user has made some progress by setting up the basic module structure and defining the necessary XML configuration files. They have also created the block class Forideas_Promotion_Block_Adminhtml_Category_Edit_Tab_Product that extends the built-in Magento grid block to handle the product listing.
However, they are facing a few challenges:
- The code is incomplete and lacks some necessary methods.
- The observer for adding the tab to the category edit page isn’t correctly implemented.
- There’s no setup script to create the required database table.
Completing the Module
Let’s start by completing the existing classes and creating the missing ones step-by-step.
Step 1: Update the Observer Class
The observer class is responsible for adding our new tab to the category edit page. The user has a good starting point, but we need to update it:
class Forideas_Promotion_Model_Observer{
public function addCategoryTab($observer)
{
$block = $observer->getEvent()->getTabs();
if (!$this->_isAllowedAction('promotion/category_product/products')) {
return;
}
$block->addTab('feature_products', array(
'label' => Mage::helper('catalog')->__('Featured Products'),
'url' => Mage::helper('adminhtml')->getUrl('adminhtml/promotion_category_product/productsgrid', array('_current'=>true)),
'class' => 'ajax',
));
}
protected function _isAllowedAction($action)
{
return Mage::getSingleton('admin/session')->isAllowed('catalog/attributes/' . $action);
}
}
We’ve added a check to ensure the user has the necessary permissions before adding the tab. The _isAllowedAction method is a helper method that uses Magento’s built-in permission system.
Step 2: Add Product Grid Controller
The user needs a controller to handle the AJAX requests for loading the product grid. Here’s the updated ProductController.php:
require_once ("Mage/Adminhtml/controllers/Catalog/ProductController.php");
class Forideas_Promotion_Adminhtml_Promotion_Category_ProductController extends Mage_Adminhtml_Catalog_ProductController
{
public function productsAction(){
$this->_initEntity();
$this->loadLayout();
$block = $this->getLayout()->getBlock('category.edit.tab.product');
$block->setCategoryProducts($this->getRequest()->getPost('category_products', null));
$this->renderLayout();
}
public function productsgridAction(){
$this->_initEntity();
$this->loadLayout();
$this->renderLayout();
}
}
We’ve added the _initEntity method to initialize the current category being edited.
Step 3: Create the Database Table
To store the association between categories and selected products, we need a database table. Here’s the missing setup script:
$this->startSetup();
$table = $this->getConnection()
->newTable($this->getTable('promotion/category_product'))
->addColumn('rel_id', Varien_Db_Ddl_Table::TYPE_INTEGER, null, array(
'unsigned' => true,
'identity' => true,
'nullable' => false,
'primary' => true,
), 'Relation ID')
->addColumn('category_id', Varien_Db_Ddl_Table::TYPE_INTEGER, null, array(
'unsigned' => true,
'nullable' => false,
), 'Category ID')
->addIndex($this->getIdxName('promotion/category_product', array('product_id')), array('product_id'))
->setComment('Category to Product Linkage Table');
$this->getConnection()->createTable($table);
$this->endSetup();
This script creates a table with columns for the relationship ID, category ID, and product ID.
Conclusion
With these updates, you now have a fully functional module that adds a “Featured Products” tab to the category edit page in Magento. This tab contains a grid listing all products associated with the current category.
Remember, after making changes like this, always clear your cache (php bin/magento cache:clean) and check for any compiler errors before proceeding.
For more advanced Magento development tasks or if you’re facing any issues, feel free to reach out to our experienced team at Laravel Company.
Happy coding!