_prepareCollection,product_options为sales_flat_order_item

时间:2016-05-25 12:16:29

标签: php magento magento-1.9

我正在尝试在订单网格上添加Product_Options数据。 我扩展了Mage_Adminhtml_Block_Sales_Order_Grid

protected function _prepareColumns() {
    $this->addColumn('product_options', array(
        'header' => $this->__('Image'),
        'index' => 'product_options',
        'width' => '70',
        'renderer'=> new Ips_CustomizeOrder_Block_Adminhtml_Renderer_Data()
    ));
    $this->addColumnsOrder('product_options', 'shipping_name');
    return parent::_prepareColumns();
}

受保护的函数_prepareCollection(){

    $collection = Mage::getResourceModel($this->_getCollectionClass());

    $collection->getSelect()->join(
             'sales_flat_order_item',
             '`sales_flat_order_item`.order_id=`main_table`.entity_id',
             array('product_options')
           )->group('sales_flat_order_item.entity_id');
    $this->setCollection($collection);
    return parent::_prepareCollection();
}

我只是在网格上获取图像列,但数据是空白的,我还检查了表 sales_flat_order_item ,其中存在实际数据。

1 个答案:

答案 0 :(得分:0)

这只是一个愚蠢的错误。 sales_flat_order_item 表创建配置产品的两个条目,一个用于简单,与configure相关联,一个是parent,即主配置产品。 你所要做的就是

protected function _prepareCollection() {
    $collection = Mage::getResourceModel($this->_getCollectionClass());
    $collection->getSelect()->join('sales_flat_order_item',
            '`sales_flat_order_item`.order_id=`main_table`.entity_id',
            array('product_options'))
            ->where('`sales_flat_order_item`.parent_item_id IS NULL')
            ->group('sales_flat_order_item.order_id');
    $this->setCollection($collection);
}

来自此处的帮助:https://magento.stackexchange.com/questions/61231/double-entries-appearing-when-adding-custom-attribute-to-orders-grid

更重要的是,在_prepareCollection之后不要使用 parent :: _ prepareCollection(); 。调用此函数将覆盖您进行集合更改的任何内容。在Magento-1.9上测试