Magento - How can I add rating information to a review

Magento - How can I add rating information to a review

September 6, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Magento - How can I add rating information to a review

Adding Rating Information to Reviews in Magento

As a Magento developer, you might find yourself needing to programmatically add rating information to reviews for your products. This is a common requirement when manually creating reviews for testing or special promotions.

Understanding the Review and Rating Systems

In Magento, the review system is separate from the rating system. A review represents the entire customer feedback for a product, while ratings represent individual pieces of that feedback associated with specific attributes (like “Price,” “Value,” or “Quality”).

Adding a Basic Review Programmatically

First, let’s review how to add a basic review programmatically using Magento’s built-in model:

$review = Mage::getModel('review/review');
$review->setEntityPkValue(23); // product id
$review->setStatusId(1);
$review->setTitle("title");
$review->setDetail("detail");
$review->setEntityId($review->getEntityIdByCode(Mage_Review_Model_Review::ENTITY_PRODUCT_CODE));
$review->setStoreId(Mage::app()->getStore()->getId());
$review->setStatusId(1); // approved
$review->setNickname("Me");
$review->setReviewId($review->getId());
$review->setStores(array(Mage::app()->getStore()->getId()));
$review->save();
$review->aggregate();

This code creates a new review for product ID 23, sets the status to approved, and saves it. The aggregate method updates the average rating of the product based on all reviews.

Adding Ratings to Your Review

Now, let’s dive into adding ratings to your review. Magento allows you to rate multiple attributes at once using the Mage_Rating_Model_Resource_Rating_Vote_Collection model:

$rating = array(
    "Price" => 80,
    "Value" => 100,
    "Quality" => 80
);
$product_id = 23;
$review_id = 631;

foreach ($rating as $attributeId => $optionId) {
    Mage::getModel('rating/rating')->setRatingId(1)
        ->setReviewId($review_id)
        ->addOptionVote($optionId, $product_id);
}

In this code:

  • We’re creating an associative array $rating with the attribute codes as keys and the rating values as values.
  • We then loop through each attribute in the foreach loop.
  • For each attribute, we use Mage::getModel('rating/rating')->setRatingId(1) to get the default rating model for our product (assuming you have a rating named ‘Price’ with ID 1).
  • Next, we set the review ID using ->setReviewId($review_id).
  • Finally, we add the option vote using ->addOptionVote($optionId, $product_id);. This associates the rating value in the array with the current attribute and product.

Conclusion

Adding ratings to reviews programmatically in Magento involves understanding the separation between these two systems and using the appropriate models. By following this guide, you should now be able to create reviews and associate them with multiple ratings for specific attributes of your products.

Remember that when working with Magento’s built-in models, it’s important to handle resources and data correctly to avoid any unexpected behavior or errors. Always test your code in a controlled environment before deploying changes to a live site.

For more advanced use cases or customizations, consider exploring the Magento extension ecosystem at Laravel Company for specialized solutions tailored to your needs.

Catalog Customers Pricing

Our network