0

I try to get the foreach result in a grid column (I have the result but I don't how to put it into the column for each category id).

my grid.php

public function _prepareCollection()
    {
    $subcategories = Mage::getModel('catalog/category')
    ->setStoreId(Mage::app()->getStore()->getId())
    ->getCollection()
    ->addAttributeToSelect('*')
    ->setOrder('parent_id', 'ASC');
    $categories = array();
foreach ($subcategories as $category){
    //do something with $category and put it in Route column
    if ($category['level'] > 1) {
    $categories[$category['entity_id']] = array('category_route' => $category['level'] == 2 ? $category['name'] : $categories[$category['parent_id']]['category_route'] ." -> ". $category['name']);
}
var_dump($categories[$category['entity_id']]);
}
        $collection = Mage::getModel('thorleif/commerciaux')->getCollection();
        $collection->addFieldToFilter('entity_id',array("nin"=>array(1,2))); 
        $this->setCollection($collection);
        return parent::_prepareCollection();
    }

    public function _prepareColumns()
    {
        $this->addColumn('entity_id',
            array(
                'header' => 'ID',
                'align' => 'left',
                'width' => '10%',
                'index' => 'entity_id'
            )
        );
        $this->addColumn('name',
            array(
                'header' => 'Category Name',
                'align' => 'left',
                'index' => 'name'
            )
        );
        $this->addColumn('route',
            array(
                'header' => 'Route',
                'align' => 'left',
                'index' => array($category['name'] .'>'. $category['name'])
            )
        );

The result of the var_dump it's like this

this

2
  • Which column you want to show? Jun 13, 2017 at 4:56
  • The result in the Route column.
    – javaTodev
    Jun 13, 2017 at 5:15

1 Answer 1

1

You can't directly show your custom collection in the required column. You need to use renderer class for this requirement. You need to create a renderer folder extends Mage_Adminhtml_Block_Widget_Grid_Column_Renderer_Abstract class.

class Namespace_Module_Block_Product extends Mage_Adminhtml_Block_Widget_Grid_Column_Renderer_Abstract
{   

public function render(Varien_Object $row)
{
$productId =  $row->getData($this->getColumn()->getIndex());
$product = Mage::getModel('catalog/product')->load($productId);

$value = '<img src="">';
if($product->getImage()!= 'noselection')
{
     $value='<img src="' . $product->getImageUrl() . '" width="100" height="100" />';
}

return $value;
}
}

In the Grid column to need to call this class by using renderer parameter as shown below.

   $this->addColumn(
            'product_id',
            [
                'header' => __('Product Name'),
                'sortable' => true,
                'index' => 'product_id',
                'renderer'  => 'Namespace\Module\Block\Product'

            ]
        );

You can also pass the category ID, to this specific renderer class.

3
  • the class you created is to get images and products?
    – javaTodev
    Jun 13, 2017 at 15:37
  • I have edited the code. It will return only images by product_id..In the same way, you can get your requirement done. Jun 14, 2017 at 4:46
  • Thanks for the help, I'll add this url if that could help someone
    – javaTodev
    Jun 19, 2017 at 20:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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