Product Reviews¶

Product Reviews are a marketing tool that let your customers give opinions about the products they buy in your shop. They have a rating and comment.

Rating¶

The rating of a product review is required and must be between 1 and 5.

Product review state machine¶

When you look inside the CoreBundle/Resources/config/app/state_machine/sylius_product_review.yml you will find out that a Review can have 3 different states:

  • new,
  • accepted,
  • rejected

There are only two possible transitions: accept (from new to accepted) and reject (from new to rejected).

../../_images/sylius_product_review.png

When a review is accepted the average rating of a product is updated.

How is the average rating calculated?¶

The average rating is updated by the AverageRatingUpdater service.

It wraps the AverageRatingCalculator, and uses it inside the updateFromReview method.

How to add a ProductReview programmatically?¶

Create a new review using a factory:

/** @var ReviewInterface $review */
$review = $this->container->get('sylius.factory.product_review')->createNew();

Fill the content of your review.

$review->setTitle('My Review');
$review->setRating(5);
$review->setComment('This product is really great');

Then get a customer from the repository, which you would like to make an author of this review.

$customer = $this->container->get('sylius.repository.customer')->findOneBy(['email' => '[email protected]']);

$review->setAuthor($customer);

Remember to set the object that is the subject of your review and then add the review to the repository.

$review->setReviewSubject($product);

$this->container->get('sylius.repository.product_review')->add($review);