Magento 1.9 – Adding Product Backend URLs on the Custom Grid

customgridmagento-1.9moduleproduct

I have a custom grid, where I need to add links to Product Edit pages on back-end.
I was able to get product front-end URLs using a renderer, but can't get the product links on back-end.

I have tried this code, but it redirects to the "Manage products" page instead of that particular product and I'm getting an error "This product no longer exists".

$this->addColumn('action', array(
    'header'    => Mage::helper('catalog')->__('Action'),
    'width'     => '50px',
    'type'      => 'action',
    'getter'    => 'getId',
    'actions'   => array(
        array(
            'caption' => Mage::helper('catalog')->__('Product Backend Url'),
            'url'     => array(
                'base'    => 'adminhtml/catalog_product/edit/productcomment_increment_id/',
                'params'  => array('store'=>$this->getRequest()->getParam('entity_id'))
            ),
            'field'   => 'id'
        )
    ),
    'filter'    => false,
    'sortable'  => false,
    'index'     => 'stores',
));

How can I get the product edit page URLs on the back-end?
Thanks for help in advance.

Best Answer

In your code you're using single quotes for the url. Within single quotes, it's not possible to parse variables, that might be part of your problem.

Also, this functionality is also in the core Magento file app/code/core/Mage/Adminhtml/Block/Catalog/Product/Grid.php, which might be the perfect example to get your solution. When you check that file and look for the line where the "Edit" action is added to the grid, you will find this code:

$this->addColumn('action',
    array(
        'header'    => Mage::helper('catalog')->__('Action'),
        'width'     => '50px',
        'type'      => 'action',
        'getter'     => 'getId',
        'actions'   => array(
            array(
                'caption' => Mage::helper('catalog')->__('Edit'),
                'url'     => array(
                    'base'=>'*/*/edit',
                    'params'=>array('store'=>$this->getRequest()->getParam('store'))
                ),
                'field'   => 'id'
            )
        ),
        'filter'    => false,
        'sortable'  => false,
        'index'     => 'stores',
));

Now, in your case you can't use the */*/edit part, because it will try to initialise the edit action of your custom extension, which probably doesn't exist. So you need to replace the */*/ part with a working URL path. If you go to the file app/code/core/Mage/Catalog/etc/adminhtml.xml, you can find the URLs that are used for the links in the menu. Since */* just "tells" Magento to use the same module and controller as the current page, you can get this info from that file. Here you can see that the link to the "Manage Products" page is adminhtml/catalog_product. There is no action defined here, which means it will use the index action.

So if you replace */*/edit with adminhtml/catalog_product/edit, your links should work properly, except for the fact that the field needs to be changed to the correct field you're using in your extension.

Related Topic