在产品报告网格Magento中添加下拉属性

时间:2012-04-30 13:41:02

标签: magento report

我正在尝试向Reports-> Products Ordered网格添加新的产品属性。我将/app/code/core/Mage/Adminhtml/Block/Report/Product/Sold/Grid.php复制到了 /app/code/local/Mage/Adminhtml/Block/Report/Product/Sold/Grid.php并添加如下属性:

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

2 个答案:

答案 0 :(得分: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
        ));

希望这有帮助!

答案 1 :(得分: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'];
}

我通常用if条件替换foreach中的语句,以避免重复的空白选项,即:

if ($option['label'])
    $sets[$option['value']] = $option['label'];