2012-04-30 156 views
2

我试图将新产品属性添加到R​​eports-> Products Ordered grid。我复制了/app/code/core/Mage/Adminhtml/Block/Report/Product/Sold/Grid.php/app/code/local/Mage/Adminhtml/Block/Report/Product/Sold/Grid.php并添加属性,像这样:在产品报告网格中添加下拉属性Magento

protected function _prepareColumns() 
    { 
     $this->addColumn('created_at', array(
      'header' =>Mage::helper('reports')->__('Create At'), 
      'index'  =>'created_at' 
     )); 
     $this->addColumn('sku', array(
      'header' =>Mage::helper('reports')->__('sku'), 
      'index'  =>'sku' 
     )); 
     $this->addColumn('name', array(
      'header' =>Mage::helper('reports')->__('Product Name'), 
      'index'  =>'name' 
     )); 
     $this->addColumn('color', array(
      'header' =>Mage::helper('reports')->__('Color'), 
      'index'  =>'color', 
      'type'  => 'options' 
     )); 
     $this->addColumn('size', array(
      'header' =>Mage::helper('reports')->__('Size'), 
      'index'  =>'size' 
     )); 
     $this->addColumn('price', array(
      'header' =>Mage::helper('reports')->__('Price'), 
      'width'  =>'120px', 
      'type'  =>'currency', 
      'currency_code' => $this->getCurrentCurrencyCode(), 
      'index'  =>'price' 
     )); 

     $this->addColumn('ordered_qty', array(
      'header' =>Mage::helper('reports')->__('Quantity Ordered'), 
      'width'  =>'120px', 
      'align'  =>'right', 
      'index'  =>'ordered_qty', 
      'total'  =>'sum', 
      'type'  =>'number' 
     )); 

     $this->addExportType('*/*/exportSoldCsv', Mage::helper('reports')->__('CSV')); 
     $this->addExportType('*/*/exportSoldExcel', Mage::helper('reports')->__('Excel')); 

     return parent::_prepareColumns(); 
    } 

问题是,“颜色”和“大小”下拉列表是属性这就是为什么他们表现出的期权价值,而不是选择文本。如何在网格中显示下拉属性的文本值?

编辑1

感谢BOOMER ......我已经改变了我Grid.php如你所说,现在显示空色列。继承人我所做的:

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

     parent::_prepareCollection(); 
     $this->getCollection() 
      ->initReport('reports/product_sold_collection'); 
     return $this; 
    } 

    /** 
    * Prepare Grid columns 
    * 
    * @return Mage_Adminhtml_Block_Report_Product_Sold_Grid 
    */ 
    protected function _prepareColumns() 
    { 
     $colors = Mage::getResourceModel('eav/entity_attribute_option_collection') 
      ->setAttributeFilter(80) // set your attribute ID here 
      ->setStoreFilter() 
      ->load() 
      ->toOptionHash('option_id', 'value'); 

     $this->addColumn('created_at', array(
      'header' =>Mage::helper('reports')->__('Create At'), 
      'index'  =>'created_at' 
     )); 
     $this->addColumn('sku', array(
      'header' =>Mage::helper('reports')->__('sku'), 
      'index'  =>'sku' 
     )); 
     $this->addColumn('name', array(
      'header' =>Mage::helper('reports')->__('Product Name'), 
      'index'  =>'name' 
     )); 
     $this->addColumn('color', array(
      'header' =>Mage::helper('reports')->__('Color'), 
      'index'  =>'color', 
      'type'  => 'options', 
      'options' => $colors 
     )); 
} 
+0

更可能您的收藏没有属性的数据。我会''zend_debug :: dump($ colors);''和'zend_debug :: dump($ collection);'以确保数据在那里。 – B00MER

回答

3

首先确保您要添加的属性,在_prepareCollection()方法来选择,例如:

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

并在您的_prepareColumns()你需要定义集合使用选项:

$colors = Mage::getResourceModel('eav/entity_attribute_option_collection') 
      ->setAttributeFilter(15) // set your attribute ID here 
      ->setStoreFilter() 
      ->load() 
      ->toOptionHash('option_id', 'value'); 

确保并相应地设置属性ID。然后在addColumns中使用此收集列表,如下所示:

$this->addColumn('color', array(
      'header' =>Mage::helper('reports')->__('Color'), 
      'index'  =>'color', 
      'type'  => 'options', 
      'options' => $colors 
     )); 

希望这有助于您!

+0

感谢您的回复,但现在颜色栏显示空白。我已按照您的建议进行了更改,请参阅有关“编辑1”的问题。 – Hum

0

我建议你使用不同的方法,至少对于自定义属性。试着用以下替换您$colors =行动

$attribute = Mage::getModel('eav/config')->getAttribute('catalog_product', 'color'); 
$colors = array(); 
foreach($attribute->getSource()->getAllOptions(true, true) as $option) { 
    $colors[$option['value']] = $option['label']; 
} 

我通常与替换里面的foreach语句,如果条件以避免重复空白选项,即:

if ($option['label']) 
    $sets[$option['value']] = $option['label']; 
相关问题