Magento observer(sales_order_grid_collection_load_before),按产品属性过滤集合

时间:2011-12-22 11:50:40

标签: magento-1.5 magento

所以我现在正在使用sales_order_grid_collection_load_before观察者事件,在那里我可以通过$collection = $observer->getEvent()->getOrderGridCollection();使用该集合,我只是想知道,是否可以过滤此集合订单属性中的产品。 我的意思是订单网格集合有与该订单相关的子产品,我只需要显示订单,如果至少有一个产品符合特定标准(在我的情况下,我给了产品一个admin_id属性,设置为添加产品的管理员。)

谢谢!

2 个答案:

答案 0 :(得分:0)

我不确定您是否可以直接访问order_grid_collection中的产品(我不这么认为),但您可以使用sales_flat_order_item加入此系列,然后根据需要进行过滤。 /> HTH

答案 1 :(得分: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/core/Mage/Adminhtml/Block/Sales/Order/Grid.php复制到我的扩展文件夹/ app / code / local / Company / Module / Block / Sales /为了

  3. 在复制的文件中,我将班级名称更改为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();
    }
    
  5. 您可能不需要连接到sales_flat_order表...您可以通过在上面显示的_prepareCollection函数的第二部分中进行过滤来完成此操作。在我的例子中,我在网格中显示customer_group_id和customer_email,以便用户可以根据需要手动过滤。