2013-07-31 104 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

相關問題