2011-12-22 74 views
0

因此,我正在與sales_order_grid_collection_load_before觀察者事件一起工作,在那裏我可以通過$collection = $observer->getEvent()->getOrderGridCollection();獲得正在使用的集合,我只是想知道,如果有可能通過訂單屬性中的產品過濾此集合。 我的意思是訂單網格集合有與該訂單相關的子產品,如果至少有一個產品符合特定條件(在我的情況下,我給出的產品爲admin_id屬性),則只需顯示訂單,這是設置給添加產品的管理員)。Magento observer(sales_order_collection_load_before),通過產品屬性篩選集合

謝謝!

回答

0

我已經通過執行以下操作做了非常類似的事:

  1. 覆蓋的銷售訂單網格塊。要做到這一點,你需要建立自己的擴展名(它看起來像你可能已經這樣做,但爲了以防萬一,有在Magento wiki一些方便DOCO)

    <config> 
        <modules> 
         <Company_Module> 
          <version>0.1.0</version> 
         </Company_Module> 
        </modules> 
        <global> 
         <blocks> 
          <company_module> 
           <class>Company_Module_Block</class> 
          </company_module> 
          <adminhtml> 
           <rewrite> 
            <sales_order_grid>Company_Module_Block_Sales_Order_Grid</sales_order_grid> 
           </rewrite> 
          </adminhtml> 
         </blocks> 
        </global> 
    </config> 
    
  2. 然後我複製了/ app/code/local/Company/Module/Block/Sales/Order

  3. 在我的擴展文件夾中的複製文件I將類名改爲class Company_Module_Block_Sales_Order_Grid extends Mage_Adminhtml_Block_Widget_Grid

  4. 然後我改變了_prepareCollection函數。在這種情況下,我很感興趣,抓住從sales_flat_order表customer_group_id和CUSTOMER_EMAIL

    protected function _prepareCollection() { 
        $collection = Mage::getResourceModel($this->_getCollectionClass()); 
        // left join onto the sales_flat_order table, retrieving the customer_group_id and customer_email columns -< this can be expanded 
        $collection->getSelect()->join('sales_flat_order', 'main_table.entity_id=sales_flat_order.entity_id', array('customer_group_id'=>'customer_group_id', 'customer_email'=>'customer_email'), null, 'left'); 
    
        // grab the current user and get their associated customer groups (additional coding required to associate the customer groups to an admin user 
        $user = Mage::getSingleton('admin/session')->getUser(); 
        $roleId = implode('', $user->getRoles()); 
        $customerGroupIds = Mage::getModel('admin/roles')->load($roleId)->getCustomerGroupIds(); 
        $orders = Mage::getResourceModel('sales/order_collection'); 
    
        // if the admin user has associated customer group ids then find the orders associated with those 
        // this would be where you would do your filtering of the products 
        if (count($customerGroupIds)) { 
         $orders->addAttributeToFilter('customer_group_id', array('in' => $customerGroupIds)); 
        } 
        $orderIds = $orders->getAllIds(); 
        $collection->addAttributeToFilter('entity_id', array('in' => $orderIds)); 
    
        $this->setCollection($collection); 
    
        return parent::_prepareCollection(); 
    } 
    

您可能不需要加入到sales_flat_order表...你也許可以做到這一點只是做在上面顯示的_prepareCollection函數的第二部分進行過濾。就我而言,我在網格中顯示了customer_group_id和customer_email,以便用戶可以根據需要手動過濾。

+1

雖然在sales_flat_order表中不是customer_group_id嗎?就我而言,這些產品不在sales_flat_order中,這就是它讓我感到困難的原因。如果我有訂單,有多個產品關聯,都有不同的'admin_id'屬性,我只需要顯示至少有一個產品的訂單,其中'admin_id'是已登錄管理員的產品。 – 2011-12-31 15:43:58

+0

你是絕對正確的,如果你看看上面的代碼,我做一個左連接到sales_flat_order表來檢索它'$ collection-> getSelect() - > join('sales_flat_order','main_table.entity_id = sales_flat_order .entity_id',數組('customer_group_id'=>'customer_group_id','customer_email'=>'customer_email'),null,'left');'。你可以做同樣的事情 – CCBlackburn 2012-01-01 20:02:56

+0

太好了,我不得不像你說的那樣重寫類,但是我不能像你說的那樣過濾這些項目,最後不得不循環遍歷它們,如果它們不在那裏,就刪除它們。不管怎麼說,還是要謝謝你。 – 2012-01-03 11:57:03

0

我不確定您是否可以直接訪問order_grid_collection(我不這麼認爲)的產品,但您可以加入此收藏品sales_flat_order_item,然後根據需要過濾。
HTH

+0

我如何能夠將一個ID列表添加到sales_flat_order_item中,並且在清除緩存時它們不會被清空? – 2011-12-23 07:12:10

相關問題