3
votes

I totally begin with Magento during my training period. I've been working on a project for two weeks now and sometimes I can't figure out how to proceed.

I'm gonna try to represent my categories tree :

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

My actual problem is : I'm in one of the accessories subcategory (for example visors). I added a form with a select allowing to choose a helmet model. When the select is submitted, I'd like to display the list of visors related to the chosen helmet model (that is actually a virtual product).

I can get the current category ID (in this case, visors) and the virtual product ID (so the helmet model). But I can't figure out how to display related products by both product ID and category ID.

I tried stuff like this :

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

But it seems not to work.

Any help is welcome. Thanks.

EDIT : 10 days after I asked this question, I still don't know how to solve my problem. If anyone could help, even a little, just a clue...

2
refer to this and apply code // Add OR condition: $collection->addAttributeToFilter(array( array( 'attribute' => 'field_name', 'in' => array(1, 2, 3), ), array( 'attribute' => 'date_field', 'from' => '2000-09-10', ), ));Oscprofessionals

2 Answers

4
votes

I cannot test this at the moment, but you can try to do something like this:


$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

I'm gonna test it, when I will get some more time.

0
votes

Actually I just solved my problem by myself.

Here is my solution if it could help anyone in the future :

$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
    }
}