2013-03-23 111 views
1

我已經設法讓我想要顯示的字段,但我不能在例如SKU數據應來自目錄>管理產品。添加SKU列Magento管理員銷售>訂單

的Magento使用的版本是1.5.1.0

enter image description here enter image description here

基本上我需要在同一類拉從 '客戶/ customer_collection' 和 '銷售/ order_grid_collection' 的數據,這可能嗎?

+0

我已獲得以下代碼以獲得以上結果: // SKU $ this-> addColumn('sku',array( 'header'=> Mage :: helper('sales') - > __ ('SKU'), 'index'=>'sku', )); 但我知道SKU來自另一個塊,只是不知道如何調用該函數。 – user1383080 2013-03-23 12:52:52

回答

5

要獲得訂單中的所有Skus,您可以使用frame_callback在單獨的函數中查找Skus的訂單。

_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; 
} 

這將返回訂單中的所有Skus,而沒有大量複雜的連接。


編輯 - 答案

要啓用搜索/上述過濾的第二部分,你需要更新_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; 

} 

和更新addColumnsku這樣的:

...

$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條件必須使搜索功能。

+0

工作就像一個魅力!現在我必須爲電話,產品名稱,購買數量和電子郵件做同樣的事情。這個概念是否適用於所有這些概念?我需要什麼字段才能更改? 非常感謝您花時間和幫助! – user1383080 2013-03-23 17:56:56

+0

也是在那裏,搜索功能可以工作以及它現在不會。 – user1383080 2013-03-23 18:25:05

+0

是的,你可以爲其他人做同樣的事情。要添加其他字段,您可以在答案中複製函數,並複製'addColumn'以添加每個附加字段。例如,函數'callback_skus'可以被複制並重命名爲'callback_qty'並將'$ item-> getSku()'更改爲'$ item-> getQty()'。無論你傳遞什麼'index'作爲'$ value'傳遞給'frame_callback'。要進行搜索和過濾工作,你需要找出你的'_prepareCollection()'連接。如果你按照答案第二部分的模式(見上面的編輯),那麼你應該沒問題。 – seanbreeden 2013-03-23 20:05:41

0

我按照其在上文中提到的完成步驟「seanbreeden」後 https://stackoverflow.com/posts/15589455/revisions

但對於我添加了一行在此函數

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; 
} 

增加這樣之後的SKU搜索功能搜索工作正常我

0
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 

)); 
0

注意:爲了不覆蓋magento核心功能,您可以創建自己的模塊,並在Grid.php使用下面的代碼

打開應用程序\代碼\核心\法師\ Adminhtml \塊\銷售\訂單\ 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

相關問題