Display product reviews by specific attribute
-
16-10-2019 - |
Question
I'm trying to create the collection of latest 5 reviews from products with specific attribute (e.g. - brand
). Here is my current code that displays just latest 5 reviews:
<?php
$reviews = Mage::getModel('review/review')->getResourceCollection();
$reviews->addStoreFilter( Mage::app()->getStore()->getId() )
->addStatusFilter( Mage_Review_Model_Review::STATUS_APPROVED )
->setDateOrder()
->addRateVotes()
->setPageSize(5)
->setCurPage(1)
->load();
?>
<!-- Print reviews -->
<ol>
<?php
foreach($reviews as $review) {
$review_data = $review->getData();
$product = Mage::getModel('catalog/product')->load( $review_data['entity_pk_value'] );
?>
<li>
<h4>Review of: <?php echo $product->getName() ?></h4>
<p><?php echo $review_data['detail']; ?></p>
</li>
<?php
}
?>
</ol>
Is there any way to apply some filter to $reviews
collection so it'll contain just reviews from specific attribute name?
Thanks for help.
UPD: code that works: https://gist.github.com/dimsemenov/5845535
Solution
Let's assume that your attribute has int
type. In this case you can do something like this:
$collection = Mage::getModel('review/review')->getCollection()
->addStoreFilter(Mage::app()->getStore()->getId())
->addStatusFilter(Mage_Review_Model_Review::STATUS_APPROVED)
->setDateOrder()
->addRateVotes()
->setPageSize(5)
->setCurPage(1);
$collection->getSelect()
->join(array('attr' => 'catalog_product_entity_int'), 'attr.entity_id = main_table.entity_pk_value', array('*'))
->where('attr.attribute_id = 272');
Where 272
is the of your attribute id.
If you also want to limit a collection to reviews of products with specific attribute set to specific value you can add something like this to the end
->where('attr.value = 24')
Where 24 is the value of your attribute.
If your attribute has different type then just replace catalog_product_entity_int
with EAV table of your attribute.