如何在订单网格页面中添加SKU

时间:2012-09-25 06:23:08

标签: sql magento grid skus

我已经看到很多关于如何做到这一点的例子。但所有的例子都是针对不同版本的Magento。在我的案例中,他们都没有工作。我的版本是1.4.0.0rc-1。是的我知道这不应该在生产环境中,但不幸的是这就是这种情况。

问题是Mage / Adminhtml / Block / Sales / Order / Grid.php类中的_prepareCollection()函数与我见过的所有其他示例不同。所以不知道该怎么做。

这是该函数的代码:

protected function _prepareCollection()
{
    $collection = Mage::getResourceModel('sales/order_collection')
        ->addAttributeToSelect('*')
        ->joinAttribute('sku','sales_flat_order_item', null, null, 'left')
        ->joinAttribute('billing_firstname', 'order_address/firstname', 'billing_address_id', null, 'left')
        ->joinAttribute('billing_lastname', 'order_address/lastname', 'billing_address_id', null, 'left')
        ->joinAttribute('shipping_firstname', 'order_address/firstname', 'shipping_address_id', null, 'left')
        ->joinAttribute('shipping_lastname', 'order_address/lastname', 'shipping_address_id', null, 'left')
        ->addExpressionAttributeToSelect('billing_name',
            'CONCAT({{billing_firstname}}, " ", {{billing_lastname}})',
            array('billing_firstname', 'billing_lastname'))
        ->addExpressionAttributeToSelect('shipping_name',
            'CONCAT({{shipping_firstname}},  IFNULL(CONCAT(\' \', {{shipping_lastname}}), \'\'))',
            array('shipping_firstname', 'shipping_lastname'));
    $this->setCollection($collection);
    return parent::_prepareCollection();
}

我尝试添加左连接,但它不起作用。这是我在setCollection语句之前尝试过的:

$collection->getSelect()->join('sales_flat_order_item', 'order_id=entity_id',    array('name'=>'name', 'sku' =>'sku'), null,'left');

但它打印出以下错误:

SELECT `e`.*, `_table_billing_address_id`.`value` AS `billing_address_id`, `_table_billing_firstname`.`value` AS `billing_firstname`, `_table_billing_lastname`.`value` AS `billing_lastname`, `_table_shipping_address_id`.`value` AS `shipping_address_id`, `_table_shipping_firstname`.`value` AS `shipping_firstname`, `_table_shipping_lastname`.`value` AS `shipping_lastname`, CONCAT(_table_billing_firstname.value, " ", _table_billing_lastname.value) AS `billing_name`, CONCAT(_table_shipping_firstname.value,  IFNULL(CONCAT(' ', _table_shipping_lastname.value), '')) AS `shipping_name`, `sales_flat_order_item`.`name`, `sales_flat_order_item`.`sku` FROM `sales_order` AS `e`
 LEFT JOIN `sales_order_int` AS `_table_billing_address_id` ON (_table_billing_address_id.entity_id = e.entity_id) AND (_table_billing_address_id.attribute_id='112')
 LEFT JOIN `sales_order_entity_varchar` AS `_table_billing_firstname` ON (_table_billing_firstname.entity_id = _table_billing_address_id.value) AND (_table_billing_firstname.attribute_id='202')
 LEFT JOIN `sales_order_entity_varchar` AS `_table_billing_lastname` ON (_table_billing_lastname.entity_id = _table_billing_address_id.value) AND (_table_billing_lastname.attribute_id='204')
 LEFT JOIN `sales_order_int` AS `_table_shipping_address_id` ON (_table_shipping_address_id.entity_id = e.entity_id) AND (_table_shipping_address_id.attribute_id='113')
 LEFT JOIN `sales_order_entity_varchar` AS `_table_shipping_firstname` ON (_table_shipping_firstname.entity_id = _table_shipping_address_id.value) AND (_table_shipping_firstname.attribute_id='202')
 LEFT JOIN `sales_order_entity_varchar` AS `_table_shipping_lastname` ON (_table_shipping_lastname.entity_id = _table_shipping_address_id.value) AND (_table_shipping_lastname.attribute_id='204')
 INNER JOIN `sales_flat_order_item` ON order_id=entity_id WHERE (e.entity_type_id = '11') ORDER BY `e`.`created_at` desc, `e`.`created_at` desc LIMIT 20

有人可以帮我解决这个问题吗?对于您的信息,SKU字段位于“Sales_flat_order_item”表中。

2 个答案:

答案 0 :(得分:1)

$collection = Mage::getModel('catalog/product')->getCollection()
        ->addAttributeToSelect('sku')
        ->addAttributeToSelect('name')
        ->addAttributeToSelect('attribute_set_id')
        ->addAttributeToSelect('type_id')
        ->joinField('qty',
            'cataloginventory/stock_item',
            'qty',
            'product_id=entity_id',
            '{{table}}.stock_id=1',
            'left')
        ->joinAttribute('status', 'catalog_product/status',
                        'entity_id', null, 'inner', $store->getId());
像getId()一样你可以去getSku(); --->我希望这会对你有帮助。

答案 1 :(得分:0)

对于任何有问题的人,我在这里找到了一个很好的解决方案:

http://www.magentocommerce.com/boards/viewthread/18362/