I want to show the Customer Reviews list below the review form
Does anyone know a solution?
Magento – Show Product Review List Below Review form on product page in magento2
magento2product-review
Related Solutions
You need to overwrite the ResourceModel. At 4 am in morning I am able to figure it now :-)
Add the below XML:
<preference for="Magento\Review\Model\ResourceModel\Review" type="NS\Module\Model\ResourceModel\Review" />
and add the following php code:
<?php
namespace NS\Module\Model\ResourceModel;
use Magento\Framework\Model\AbstractModel;
/**
* Review resource model
*/
class Review extends \Magento\Review\Model\ResourceModel\Review
{
/**
* Perform actions after object save
*
* @param \Magento\Framework\Model\AbstractModel $object
* @return $this
*/
protected function _afterSave(AbstractModel $object)
{
$connection = $this->getConnection();
/**
* save detail
*/
$detail = [
'title' => $object->getTitle(),
'detail' => $object->getDetail(),
'nickname' => $object->getNickname(),
'refer_field' => $object->getReferField(),
];
.
.
.
Just overwrite the function "_afterSave" not the whole file.
Please remove the alert function alert(reviewurl);
from your js file and modify you controller file as follows and try,
namespace XXX\YYY\Controller\Index;
use Magento\Framework\Controller\ResultFactory;
class Index extends \Magento\Review\Controller\Product\Post
{
public function execute()
{
$message = null;
$status = null;
if (!$this->formKeyValidator->validate($this->getRequest())) {
$message[] = "Invalid form key";
$status = 'failed';
}
else
{
$data = $this->reviewSession->getFormData(true);
if ($data) {
$rating = [];
if (isset($data['ratings']) && is_array($data['ratings'])) {
$rating = $data['ratings'];
}
} else {
$data = $this->getRequest()->getPostValue();
$rating = $this->getRequest()->getParam('ratings', []);
}
if (($product = $this->initProduct()) && !empty($data))
{
/** @var \Magento\Review\Model\Review $review */
$review = $this->reviewFactory->create()->setData($data);
$review->unsetData('review_id');
$validate = $review->validate();
if ($validate === true) {
try {
$review->setEntityId($review->getEntityIdByCode(Review::ENTITY_PRODUCT_CODE))
->setEntityPkValue($product->getId())
->setStatusId(Review::STATUS_PENDING)
->setCustomerId($this->customerSession->getCustomerId())
->setStoreId($this->storeManager->getStore()->getId())
->setStores([$this->storeManager->getStore()->getId()])
->save();
foreach ($rating as $ratingId => $optionId) {
$this->ratingFactory->create()
->setRatingId($ratingId)
->setReviewId($review->getId())
->setCustomerId($this->customerSession->getCustomerId())
->addOptionVote($optionId, $product->getId());
}
$review->aggregate();
$message[] = __('You submitted your review for moderation.');
$status = 'success';
} catch (\Exception $e) {
$this->reviewSession->setFormData($data);
$message[] = __('We can\'t post your review right now.');
$status = 'failed';
}
} else {
$this->reviewSession->setFormData($data);
if (is_array($validate)) {
foreach ($validate as $errorMessage) {
$message[] = $errorMessage;
}
} else {
$message[] = __('We can\'t post your review right now.');
}
$status = 'failed';
}
}
}
$result = $this->resultFactory
->create(\Magento\Framework\Controller\ResultFactory::TYPE_JSON)
->setData([
'messages' => $message,
'status' => $status,
]);
return $result;
}
}
Best Answer
You need to copy the below file in your custom theme:
from
to