简体   繁体   中英

How to get Product name in custom module in magento grid view?

I have product id . database field name productid in custom module

I want to show product name on custom module list view.

Here is my code:

protected function _prepareColumns() {         
    $this->addColumn('productid', array(
        'header'    => Mage::helper('productpdf')->__('productid'),
        'align'     => 'center',
        'index'     => 'productid',
    ));
}

For this You have to separately create the collection(or in your existing collection include the below query) for get product name from product id..

You can do like this

protected function _prepareCollection()
{
        $collection = Mage::getModel('catalog/product')->getCollection()
        ->addAttributeToSelect('name');
        $collection->addAttributeToFilter('productid');
}

protected function _prepareColumns()
{


       $this->addColumn('productid', array(
       'header' => Mage::helper('productpdf')->__('productid'),
      'align'     =>'center',
     'index'     =>'productid',


      ));


         $this->addColumn('name', array(
        'header'    => Mage::helper('productpdf')->__('Name'),
        'width'     => '150',
        'index'     => 'name'
    ));


}

In addAttributeToFilter('productid') you have to pass your product id. Hope this work:-)

Instead of using too much bunch of code, add below code in your model/productpdf.php file.

  /**
 * @param int $productId.
 * @return product name.
 */
public function getProductNameById($productId)
{
    $query_select = "SELECT value FROM `".Mage::getConfig()->getTablePrefix()."catalog_product_entity_varchar` WHERE
            entity_id = '".$productId."' AND `attribute_id` = 71";

    return $data = Mage::getSingleton('core/resource')->getConnection('core_read')->fetchAll($query_select); 

}

Now just call this function in your file.

  $productNameArray = Mage::getModel('productpdf/productpdf')->getProductNameById($productId);

Add new column for display product name

  $this->addColumn('name', array(
    'header'    => Mage::helper('productpdf')->__('Name'),
    'width'     => '150',
    'index'     =>  $productNameArray[0]['value']
));

Cheers!!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM