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



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

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

该函数的代码如下:

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"表中。

$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();--->我希望这对你有帮助。

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

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

最新更新