2

I have created a grid and in one of the columns I want to show the full path of the product url that we can go to the product currently i did this in grid.php:

protected function _prepareCollection()
    {
        $store = $this->_getStore();

       $collection = Mage::getModel('catalog/product')->getCollection()

            ->addAttributeToSelect('url_key');
//some code
}

and for the index I used it :

   protected function _prepareColumns()
    {


     $this->addColumn('link',
            array(
                'header'=> Mage::helper('catalog')->__('link'),
                'index'=>'url_key'
        )); 
//somecode
}

is there any clean way so that instead of just the url key i will have the whole url in there? I want it to be www.domain.com/urlkey rather than just the url key

3

Try this in your Grid.php

$this->addColumn('link', array(
                'header'        => $this->__('Product Url'),
                'index'         => 'url_key',
                'type'          => 'text',
                'sortable'  => false,
                'filter'    => false,
                'width' => "20px",
                'renderer'  => 'your_block/adminhtml_catalog_product_grid_renderer_link',
            ));

Now create a file Link.php in Block/Adminhtml/Catalog/Product/Grid/Renderer/

<?php
class  Namespace_Module_Block_Adminhtml_Catalog_Product_Grid_Renderer_Link
extends Mage_Adminhtml_Block_Widget_Grid_Column_Renderer_Abstract
{


    public function render(Varien_Object $row)
    {

        $storeId = (int) Mage::app()->getRequest()->getParam('store', 0);
        $url = Mage::getModel('catalog/product')->setStoreId($storeId)->load($row["entity_id"])->getProductUrl();
       return "<a href='$url' target='blank'>URL</a>";
    }

}

I haven't tested, but should work.

| improve this answer | |
  • Should work, but you'll have a full product load for every single row, which will be very slow. Better prepare the collection with all attributes you need and use $row directly, which is the product model – Fabian Schmengler Jul 3 '15 at 7:06
  • I don't think that's a good idea because I already have my collectoin in prepareCollection function, this will cause heavy load – Nickool Jul 6 '15 at 15:02
2

regarding to David Manners

renderer would be like this:

class Ailin_Orders_Block_Adminhtml_Sales_LinkRender
extends Mage_Adminhtml_Block_Widget_Grid_Column_Renderer_Abstract
{
    public function render(Varien_Object $row)
    {     
      $product_collection = Mage::getModel('catalog/product')->getCollection()
        ->addAttributeToFilter('entity_id', $row["entity_id"])
        ->addUrlRewrite();
        $product_collection_url = $product_collection->getFirstItem()->getProductUrl(); 
       return $product_collection_url;
    }
}

and then in the _prepareColumns we call it:

protected function _prepareColumns()
    {
       $this->addColumn('link',
            array(
                'header'=> Mage::helper('catalog')->__('link'),
                 'renderer'  => 'ailin_orders/adminhtml_sales_linkrender'
        )); 
    }
| improve this answer | |
1

You could use an addtional column of the type action to show a link:

     $this->addColumn('link',
        array(
            'header'    => Mage::helper('catalog')->__('link'),
            'type'      => 'action',
            'getter'     => 'getId',
            'actions'   => array(
                array(
                    'caption' => Mage::helper('catalog')->__('link'),
                    'url'     => Mage::getBaseUrl() . '$url_key',
                )
            ),
    ));

'$url_key' will be replaced with the value of the url_key column.

| improve this answer | |
  • have tried but it not works.. – Amit Bera Jul 3 '15 at 7:04
  • how $url_key can come from that – Nickool Jul 6 '15 at 15:13
  • Magic. It is weird syntax but Magento does it the same way in the core. You must load the url_key attribute in the collection, of course. It might also be necessary to include it as additional column, I did not test it. – Fabian Schmengler Jul 6 '15 at 15:16
  • I don't think so, we have index->url_key which will assign the keys I cannot even assign it to $url_key – Nickool Jul 6 '15 at 15:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.