我已经设法获得了我想要显示的字段,但我无法提取数据,例如 SKU 数据应该来自目录 > 管理产品。
使用的 Magento 版本是 1.5.1.0
基本上我需要从同一类中的'customer/customer_collection'和'sales/order_grid_collection'中提取数据,这可能吗?
要获取订单中的所有 Sku,您可以使用frame_callback
在单独的函数中查找订单的 Sku。
在_prepareColumns()
Sku 列的函数中:
...
$this->addColumn('increment_id', array(
'header' => Mage::helper('sales')->__('SKU'),
'index' => 'increment_id',
'frame_callback' => array($this, 'callback_skus')
));
...
然后在你的Grid.php
.
public function callback_skus($value, $row, $column, $isExport) {
$increment_id = $value;
$_order = Mage::getModel('sales/order')->loadByIncrementId($increment_id);
$_items = $_order->getAllItems();
$skus="";
foreach ($_items as $item) {
$skus .= $item->getSku()."<br/>";
}
return $skus;
}
这将返回订单的所有 Sku,而无需进行大量复杂的连接。
编辑 - 答案的第二部分
要在上面启用搜索/过滤,您需要将其更新_prepareCollection()
为以下内容:
protected function _prepareCollection()
{
$collection = Mage::getResourceModel($this->_getCollectionClass())
->join(
'sales/order_item',
'`sales/order_item`.order_id=`main_table`.entity_id',
array(
'skus' => new Zend_Db_Expr('group_concat(`sales/order_item`.sku SEPARATOR ",")'),
)
);
$collection->getSelect()->group('entity_id');
$this->setCollection($collection);
return parent::_prepareCollection();
}
添加一个名为的新函数filter_skus()
:
public function filter_skus($collection, $column) {
if (!$value = $column->getFilter()->getValue()) {
return $this;
}
$this->getCollection()->getSelect()->where(
"sku like ?"
, "%$value%");
return $this;
}
并像这样更新addColumn
for sku
:
...
$this->addColumn('sku', array(
'header' => Mage::helper('sales')->__('SKU'),
'index' => 'increment_id',
'frame_callback' => array($this, 'callback_skus'),
'filter_condition_callback' => array($this, 'filter_skus'),
));
...
请注意新的调用filter_condition_callback
添加了where
创建搜索功能所需的条件。
Step 1: Create the Renderer directory under /app/code/core/Mage/Adminhtml/Block/Sales/Order
Step 2:
Create a Block File in /app/code/core/Mage/Adminhtml/Block/Sales/Order/Renderer/Red.php (You can use any name)
<?php
class
Mage_Adminhtml_Block_Sales_Order_Renderer_Red extends
Mage_Adminhtml_Block_Widget_Grid_Column_Renderer_Abstract
{
public function render(Varien_Object $row)
{
//$value = $row->getData($this->getColumn()->getIndex());
$order_id=$row->getData('increment_id');
$order = Mage::getModel('sales/order')->loadByIncrementID($order_id);
$items = $order->getAllItems();
// print_r($items);exit();
foreach ($items as $itemId => $item)
{
if($item->getSku())
{
$sku[] = $item->getSku();
}
}
if(count($sku))
{
$skutext = implode(',',$sku);
}
$conbinetext=$skutext;
return $conbinetext;
}
}
?>
Step 3: Add the Below Code in
/app/code/core/Mage/Adminhtml/Block/Sales/Order/Grid.php
$this->addColumn('Shopby',
array(
'header'=>Mage::helper('catalog')->__('Sku'),
'index' => 'shopby',
'filter'=> false,
'sortable'=>false,
'renderer'=> 'Mage_Adminhtml_Block_Sales_Order_Renderer_Red',// THIS IS WHAT THIS POST IS ALL ABOUT
));
我已经按照上面“seanbreeden”帖子 https://stackoverflow.com/posts/15589455/revisions中提到的完成步骤
但是对于 SKU 搜索功能,我在此功能中添加了一行
public function filter_skus($collection, $column) {
if (!$value = $column->getFilter()->getValue()) {
return $this;
}
$this->getCollection()->getSelect()->join( array('table_alias'=>'sales_flat_order_item'), 'main_table.entity_id = table_alias.order_id',array('table_alias.sku'));
$this->getCollection()->getSelect()->where("sku like ?", "%$value%");
return $this;
}
添加此行搜索后对我来说工作正常
注意:为了不覆盖 magento 核心功能,您可以创建自己的模块并在 Grid.php 中使用以下代码
打开 app\code\core\Mage\Adminhtml\Block\Sales\Order\Grid.php
替换下面的函数
protected function _prepareCollection()
{
$collection = Mage::getResourceModel($this->_getCollectionClass());
$collection->getSelect()->joinLeft(
'sales_flat_order_item',
'`sales_flat_order_item`.order_id=`main_table`.entity_id',
array('skus' => new Zend_Db_Expr('group_concat(`sales_flat_order_item`.sku SEPARATOR ",")'))
);
$collection->getSelect()->group('entity_id');
$this->setCollection($collection);
return parent::_prepareCollection();
}
现在将以下代码放入 _prepareColumns() 函数中
$this->addColumn('sku', array(
'header' => Mage::helper('sales')->__('SKU'),
'index' => 'skus',
'type' => 'text',
'width' => '100px',
'filter' => false,
));
来源:http ://chandreshrana.blogspot.in/2016/12/how-to-add-sku-column-in-sales-order.html