【问题标题】:Display Gift Messages in Sales Order Grid per Order Magento Admin在每个订单 Magento 管理员的销售订单网格中显示礼品消息
【发布时间】:2013-11-14 07:21:28
【问题描述】:

伙计们,我是 Magento 的新手,所以有人可以帮助我吗? 以下是我在管理员中的销售订单网格上获取礼品消息的代码:

  protected function _prepareCollection()
{
    $collection = Mage::getResourceModel($this->_getCollectionClass());

    $collection->join('sales/order_item','`sales/order_item`.order_id=`main_table`.entity_id',array(
    'giftavail' => new Zend_Db_Expr('group_concat(`sales/order_item`.gift_message_available SEPARATOR ",")'),
    'giftid' => new Zend_Db_Expr('group_concat(`sales/order_item`.gift_message_id SEPARATOR ",")'),
    'skus' => new Zend_Db_Expr('group_concat(`sales/order_item`.sku SEPARATOR ",")'),
    'name' => new Zend_Db_Expr('group_concat(`sales/order_item`.name SEPARATOR ",")'),)

    );
    $collection->getSelect()->group('main_table.entity_id');

    $collection->getSelect()->joinLeft(array('sfog' => 'sales_flat_order_grid'),
    'main_table.entity_id = sfog.entity_id',array('sfog.shipping_name','sfog.billing_name'));

    $collection->getSelect()->joinLeft(array('sfo'=>'sales_flat_order'),
    'sfo.entity_id=main_table.entity_id',array('sfo.customer_email','sfo.weight',
    'sfo.discount_description','sfo.increment_id','sfo.store_id','sfo.created_at','sfo.status',
    'sfo.base_grand_total','sfo.grand_total'));

    $collection->getSelect()->joinLeft(array('sfoa'=>'sales_flat_order_address'),
    'main_table.entity_id = sfoa.parent_id AND sfoa.address_type="shipping"',array('sfoa.street',
    'sfoa.city','sfoa.region','sfoa.postcode','sfoa.telephone' , 'sfoa.country_id'));

    $collection->getSelect()->joinLeft(array('gfm' => 'gift_message'),
    'main_table.giftid = gfm.gift_message_id',array('gfm.message'));

    $this->setCollection($collection);

    return parent::_prepareCollection();

}

这是生成的 SQL 查询:

"SELECT main_table.*, group_concat(sales/order_item.gift_message_available SEPARATOR ",") AS giftavail, group_concat(sales/order_item.gift_message_id SEPARATOR ",") AS giftid, group_concat(sales/order_item98765432@ .sku SEPARATOR ",") AS skus, group_concat(sales/order_item.name SEPARATOR ",") AS name, sfog.shipping_name, sfog.billing_name, sfo.@ 987654336@, sfo.weight, sfo.discount_description, sfo.increment_id, sfo.store_id, sfo.created_at, sfo.status , sfo.base_grand_total, sfo.grand_total, sfoa.street, sfoa.city, sfoa.region,region,sfoa,@9876 987654361@.telephone, sfoa.country_id, gfm.message FROM sales_flat_order_grid AS main_table INNER JOIN sales_flat_order_item AS sales/order_item ON sales/order_item.order_id=main_table. entity_id 左联接sales_flat_order_grid AS sfog ON main_table.entity_id = sfog.entity_id 左联接sales_flat_order AS sfo ON sfo.entity_id=main_ table.entity_id 左连接 sales_flat_order_address AS sfoa ON main_table.entity_id = sfoa.parent_id AND sfoa.address_type="shipping" 左连接 gift_message AS gfm ON main_table.entity_id = gfm.giftid GROUP BY @987654381 .entity_id"

但这不起作用,有人可以帮忙吗? 提前致谢

【问题讨论】:

  • $this->setCollection($collection); 行之前打印$collection->getSelect(); exit; 并手动运行该sql 查询。然后分享,如果有任何错误。
  • 当我在数据库中运行该查询时,我收到以下错误:'#1054 - Unknown column 'main_table.giftid' in 'on Clause''
  • 可以分享一下sql查询吗?
  • 我收到了与订单有关的礼物消息 ID,但我想要与该礼物消息相关的消息...有任何想法吗??

标签: magento grid


【解决方案1】:

使用以下代码更新您的代码:

protected function _prepareCollection(){

$collection->join(array('order_item'=>'sales/order_item'),'`order_item`.order_id=`main_table`.entity_id',array(
'giftavail' => new Zend_Db_Expr('group_concat(`order_item`.gift_message_available SEPARATOR ",")'),
'giftid' => new Zend_Db_Expr('group_concat(`order_item`.gift_message_id SEPARATOR ",")'),
'skus' => new Zend_Db_Expr('group_concat(`order_item`.sku SEPARATOR ",")'),
'name' => new Zend_Db_Expr('group_concat(`order_item`.name SEPARATOR ",")'))

);
$collection->getSelect()->group('main_table.entity_id');

$collection->getSelect()->joinLeft(array('sfog' => 'sales_flat_order_grid'),
'main_table.entity_id = sfog.entity_id',array('sfog.shipping_name','sfog.billing_name'));

$collection->getSelect()->joinLeft(array('sfo'=>'sales_flat_order'),
'sfo.entity_id=main_table.entity_id',array('sfo.customer_email','sfo.weight',
'sfo.discount_description','sfo.increment_id','sfo.store_id','sfo.created_at','sfo.status',
'sfo.base_grand_total','sfo.grand_total'));

$collection->getSelect()->joinLeft(array('sfoa'=>'sales_flat_order_address'),
'main_table.entity_id = sfoa.parent_id AND sfoa.address_type="shipping"',array('sfoa.street',
'sfoa.city','sfoa.region','sfoa.postcode','sfoa.telephone' , 'sfoa.country_id'));

$collection->getSelect()->joinLeft(array('gfm' => 'gift_message'),
'order_item.gift_message_id = gfm.gift_message_id',array('gfm.message'));

$this->setCollection($collection);

return parent::_prepareCollection();

}

然后您将获得有效的 sql 查询

SELECT `main_table`.*, group_concat(`order_item`.gift_message_available SEPARATOR ",") AS `giftavail`,
group_concat(`order_item`.gift_message_id SEPARATOR ",") AS `giftid`, 
group_concat(`order_item`.sku SEPARATOR ",") AS `skus`,
group_concat(`order_item`.name SEPARATOR ",") AS `name`, `sfog`.`shipping_name`,  
`sfog`.`billing_name`, `sfo`.`customer_email`,
`sfo`.`weight`, `sfo`.`discount_description`, `sfo`.`increment_id`, 
`sfo`.`store_id`, `sfo`.`created_at`, `sfo`.`status`, 
`sfo`.`base_grand_total`, `sfo`.`grand_total`, `sfoa`.`street`, 
`sfoa`.`city`, `sfoa`.`region`, `sfoa`.`postcode`,
`sfoa`.`telephone`, `sfoa`.`country_id`, `gfm`.`message` 
FROM `sales_flat_order_grid` AS `main_table` 
INNER JOIN 
   `sales_flat_order_item` AS `order_item` 
ON `order_item`.order_id=`main_table`.entity_id 
LEFT JOIN 
   `sales_flat_order_grid` AS `sfog`
ON main_table.entity_id = sfog.entity_id 
LEFT JOIN 
   `sales_flat_order` AS `sfo` 
ON sfo.entity_id=main_table.entity_id 
LEFT JOIN 
   `sales_flat_order_address` AS `sfoa` 
ON main_table.entity_id = sfoa.parent_id AND sfoa.address_type="shipping" 
LEFT JOIN 
   `gift_message` AS `gfm` 
ON order_item.gift_message_id = gfm.gift_message_id 
GROUP BY 
   `main_table`.`entity_id`

问题在于sales_flat_order_item 表别名,最后一行是礼物消息。您使用的是main_table.giftid 而不是order_item.gift_message_id

希望能有所帮助!

【讨论】:

    猜你喜欢
    • 2012-11-25
    • 2017-04-17
    • 2018-04-19
    • 2023-04-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-13
    • 1970-01-01
    相关资源
    最近更新 更多