tomakun
tomakun

Reputation: 134

Magento - Get a Collection from current viewed product Attribute

I'm trying to return a collection of products, in the product view page, made from an attribute taken from the current viewed product.

In other words, let's say I sell CDs, and I'm viewing a Ray Charles CD.
In this very product page, I'd like to show others products that have the value 'Ray Charles', in the 'artist' attribute. Except this value has to be dynamically pulled from the current viewed product's 'artist' value.

I've just learned to do this:

<?php 

    $model = Mage::getModel('catalog/product');
    $collection = $model->getCollection();;
    $collection->addFieldToFilter('artist', '81');
    $collection->load();

?>

Which can return a collection of products based on the artist attribute with an ID of '81'. But this is static.

How can I tell my collection to check for the 'artist' value of the current viewed product and use it to filter its results?

product/view.phtml

Thanks much for your help.

Upvotes: 3

Views: 1374

Answers (1)

clockworkgeek
clockworkgeek

Reputation: 37700

Product pages register the viewed product already so you only need to retrieve it:

$currentProduct = Mage::registry('current_product');
$collection->addAttributeToFilter('artist', $currentProduct->getArtist());

Upvotes: 3

Related Questions