简体   繁体   English

如何在magento网格视图的自定义模块中获取产品名称?

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

I have product id . 我有product id database field name productid in custom module 定制模块中的数据库字段名称productid

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.. 为此,您必须单独创建集合(或在现有集合中包括以下查询)以从产品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. addAttributeToFilter('productid')中,您必须传递您的产品ID。 Hope this work:-) 希望这项工作:-)

Instead of using too much bunch of code, add below code in your model/productpdf.php file. 不要在代码/productpdf.php文件中添加以下代码,而不必使用过多的代码。

  /**
 * @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!! 干杯!!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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