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


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

我有product id . 自定义模块中的数据库字段名称productid

我想在自定义模块列表视图上显示产品名称。

这是我的代码:

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

您必须单独创建集合(或在现有集合中包含以下查询)以从产品ID获取产品名称。

你可以这样做

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'
    ));

}

addAttributeToFilter('productid')中,你必须传递你的产品ID。希望这项工作:-)

与其使用太多代码,不如在模型/产品pdf.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); 
}

现在只需在文件中调用此函数。

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

为显示产品名称添加新列

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

干杯!!