2013-03-28 250 views
3

我在訓練期間完全從Magento開始。我一直在做一個項目兩個星期,有時我不知道如何繼續。Magento:從產品ID和當前類別獲取相關產品ID

我會盡力代表我的類別樹:

- accessories 
    * visors 
    * communication systems 
    * other 
- helmets 
    * a lot of subcategories and subcategories... 

我實際的問題是:我在配件子類別(例如面罩)之一。我添加了一個表格,允許選擇一個頭盔模型select。提交select時,我想顯示與所選頭盔模型相關的遮陽板列表(實際上這是一個virtual product)。

我可以獲得當前的類別ID(在這種情況下,遮陽帽)和虛擬產品ID(如此頭盔模型)。但我無法弄清楚如何通過產品ID和類別ID顯示相關產品。

我嘗試這樣的東西:

$relatedProducts = Mage::getModel('catalog/product_link') 
         ->getCollection() 
         ->addCategoryFilter($myCurrentCat) 
         ->addFieldToFilter('product_id',$myVirtualProductId) 
         ->addFieldToFilter('link_type_id','1'); 

但似乎沒有工作。

任何幫助,歡迎。謝謝。

編輯:10天后,我問這個問題,我仍然不知道如何解決我的問題。如果有人可以幫助,哪怕是一點點,只是一個線索...

+0

參閱本和應用代碼//添加或條件: $收藏 - > addAttributeToFilter(陣列( 陣列( 「屬性'=>'field_name', 'in'=>'array(1,2,3), ), array( 'attribute'=>'date_field', 'from'=>'2000-09-10 ', ), )); – oscprofessionals

回答

0

其實我只是自己解決了我的問題。

這裏是我的解決方案,如果它可以幫助任何人在未來:

$mainProduct = Mage::getModel('catalog/category')->load($myCurrentCat->getId()); 
$mainProduct = $mainProduct->getProductCollection(); 
$mainProduct = $mainProduct->addAttributeToFilter('product_id', $myVirtualProductId); 

foreach ($mainProduct as $product) { 
    $related = $product->getRelatedProductIds(); 
    if ($this->array_contains($related, $myVirtualProductId)) { 
     //TODO 
    } 
} 
4

我不能在此刻進行測試,但你可以嘗試做這樣的事情:


$collection = Mage::getSingleton('catalog/product_link') 
    ->useRelatedLinks() 
    ->getProductCollection() 
    ->setIsStrongMode(); 
$product = Mage::getModel('catalog/product')->load($productId); 
$collection->setProduct($product); 
$collection->addCategoryFilter($category); //I'm not sure if this will work correctly 

我要測試,當我會得到更多的時間。