2013-07-31 201 views
0

我花了很多小时来解决这个问题,但我没有得到它:( 我需要一个特殊类别的所有订购物品的选择。我如何过滤收集例如categoryId'44'?Magento:如何按特定类别过滤集合(销售/订单)?

这里我的代码:

<?php 
require_once '/home/web/public_html/app/Mage.php'; 
Mage::app(); 

//$_category = Mage::getModel('catalog/category')->load($category_id); 

$salesCollection = Mage::getModel("sales/order")->getCollection(); 

echo $salesCollection->getSelect(); 

foreach ($salesCollection as $order) { 
    $items = $order->getAllItems(); 
... ?> 

谢谢大家对我的帮助, 最好,里克

回答

1

这里有一个(也许)不那么优雅的方式这样做......

所有产品类别中的你想

$category_id = 44; 
$category = Mage::getModel("catalog/category")->load($category_id); 
$products = Mage::getModel("catalog/product")->getCollection() 
    ->addCategoryFilter($category); 

下一个抢我收集只是产品ID,这样我可以把他们

$product_ids = array(); 
foreach ($products as $product) 
    $product_ids[] = $product->getId(); 

抓住所有订单项,其中产品ID从产品之一我们类别

$items = Mage::getModel("sales/order_item")->getCollection() 
    ->addFieldToFilter("product_id", array("in" => $product_ids)); 

立即抓取全部由项目所引用的唯一订单

$orders = array(); 
foreach ($items as $item) { 
    $order_id = $item->getOrderId(); 
    if (!isset($orders[$order_id])) 
    $orders[$order_id] = Mage::getModel("sales/order")->load($order_id); 
} 
2

sales_flat_order_item数据库表对分类一无所知。所以我猜你必须使用:$ collection-> getSelect() - > join(.....);

在sales_flat_order_item(Mage :: getModel('sales/order'))中,您可以找到product_id。 在catalog_category_product(法师:: getModel( '目录/ category_product'))你可以找到CATEGORY_ID,PRODUCT_ID,位置

现在你要加入他们的行列......

http://www.magentocommerce.com/wiki/1_-_installation_and_configuration/using_collections_in_magento

相关问题