基于product_id或user_id过滤订单(供应商)

时间:2014-06-23 12:25:12

标签: magento filtering

我已按照How to create a custom grid from scratch创建自定义销售订单。管理员正在创建供应商,供应商将上传产品。我想限制供应商,使他们只能看到自己产品上的订单。 有一个新型号jbmarketplace / jbmarketplaceproducts,当供应商创建产品时,供应商user_id和product_id存储在其中。但是,当我进行过滤时,会给出 SQLSTATE [42S22]:未找到列:1054未知列&product;产品ID'在' where子句' 。但是product_id在sales_flat_order_item表中可用。

此问题已修复。更新的代码

protected function _prepareCollection()
{
    // Get current logged in user
    $current_user = Mage::getSingleton( 'admin/session' )->getUser();

    // Limit only for vendors
   if ( $current_user->getRole()->getRoleId() == Mage::getStoreConfig( 'jbmarketplace/jbmarketplace/vendors_role' ) ) {
      // echo( $current_user->getUserId());
       $my_products = Mage::getModel( 'jbmarketplace/jbmarketplaceproducts' )
           ->getCollection()
           ->addFieldToSelect( 'product_id' )
           ->addFieldToFilter( 'user_id', $current_user->getUserId() )
           ->load();
       $my_product_array = array();
       foreach ( $my_products as $product ) {
           $my_product_array[] = $product->getProductId();
           $entity = Mage::getModel('sales/order_item')
               ->getCollection()
               ->addFieldToSelect('order_id')
               ->addFieldToFilter('product_id',$my_product_array)
               ->load();
          // echo $entity->getSelect();// will print sql query

       }
       $d=$entity->getData();

       if($d){

           $collection = Mage::getResourceModel('sales/order_collection')
           // My code
             ->addFieldToFilter('entity_id', $d)
        ->join(array('a' => 'sales/order_address'), 'main_table.entity_id = a.parent_id AND a.address_type != \'billing\'', array(
            'city'       => 'city',
            'country_id' => 'country_id'
        ))

         //  ->join(Mage::getConfig()->getTablePrefix().'catalog_product_entity_varchar', 'main_table.products_id ='.Mage::getConfig()->getTablePrefix().'catalog_product_entity_varchar.entity_id',array('value'))
        ->join(array('c' => 'customer/customer_group'), 'main_table.customer_group_id = c.customer_group_id', array(
            'customer_group_code' => 'customer_group_code'
        ))


            ->addExpressionFieldToSelect(
            'fullname',
            'CONCAT({{customer_firstname}}, \' \', {{customer_lastname}})',
            array('customer_firstname' => 'main_table.customer_firstname', 'customer_lastname' => 'main_table.customer_lastname'))
        ->addExpressionFieldToSelect(
            'products',
            '(SELECT GROUP_CONCAT(\' \', x.name)
                FROM sales_flat_order_item x
                WHERE {{entity_id}} = x.order_id
                    AND x.product_type != \'configurable\')',
            array('entity_id' => 'main_table.entity_id')
        )

       ;
         parent::_prepareCollection();
       $this->setCollection($collection);
    return $this;

   }
       else
       {
           echo("Current there are no purchases on your product. Thank you");
       }
   }
   else{
       echo("Please Login as Vendor and you will see orders on your products.<br>");
      // $current_user = Mage::getSingleton( 'admin/session' )->getUser()->getUserId();
      // echo($current_user);
    }

}

1 个答案:

答案 0 :(得分:1)

以下代码对我有用。

protected function _prepareCollection()
{
    // Get current logged in user
    $current_user = Mage::getSingleton( 'admin/session' )->getUser();

    // Limit only for vendors
   if ( $current_user->getRole()->getRoleId() == Mage::getStoreConfig( 'jbmarketplace/jbmarketplace/vendors_role' ) ) {
      // echo( $current_user->getUserId());
       $my_products = Mage::getModel( 'jbmarketplace/jbmarketplaceproducts' )
           ->getCollection()
           ->addFieldToSelect( 'product_id' )
           ->addFieldToFilter( 'user_id', $current_user->getUserId() )
           ->load();
       $my_product_array = array();
       foreach ( $my_products as $product ) {
           $my_product_array[] = $product->getProductId();
           $entity = Mage::getModel('sales/order_item')
               ->getCollection()
               ->addFieldToSelect('order_id')
               ->addFieldToFilter('product_id',$my_product_array)
               ->load();
          // echo $entity->getSelect();// will print sql query

       }
       $d=$entity->getData();

       if($d){

           $collection = Mage::getResourceModel('sales/order_collection')
           // My code
             ->addFieldToFilter('entity_id', $d)
        ->join(array('a' => 'sales/order_address'), 'main_table.entity_id = a.parent_id AND a.address_type != \'billing\'', array(
            'city'       => 'city',
            'country_id' => 'country_id'
        ))

         //  ->join(Mage::getConfig()->getTablePrefix().'catalog_product_entity_varchar', 'main_table.products_id ='.Mage::getConfig()->getTablePrefix().'catalog_product_entity_varchar.entity_id',array('value'))
        ->join(array('c' => 'customer/customer_group'), 'main_table.customer_group_id = c.customer_group_id', array(
            'customer_group_code' => 'customer_group_code'
        ))


            ->addExpressionFieldToSelect(
            'fullname',
            'CONCAT({{customer_firstname}}, \' \', {{customer_lastname}})',
            array('customer_firstname' => 'main_table.customer_firstname', 'customer_lastname' => 'main_table.customer_lastname'))
        ->addExpressionFieldToSelect(
            'products',
            '(SELECT GROUP_CONCAT(\' \', x.name)
                FROM sales_flat_order_item x
                WHERE {{entity_id}} = x.order_id
                    AND x.product_type != \'configurable\')',
            array('entity_id' => 'main_table.entity_id')
        )

       ;
         parent::_prepareCollection();
       $this->setCollection($collection);
    return $this;

   }
       else
       {
           echo("Current there are no purchases on your product. Thank you");
 }
   }
   else{
       echo("Please Login as Vendor and you will see orders on your products.<br>");
      // $current_user = Mage::getSingleton( 'admin/session' )->getUser()->getUserId();
      // echo($current_user);
    }
}