I have modified product review form as popup modal, Now I have passed the form data through Ajax to my overrides controller, but it shows the below error.
{"0":"Source class \"\XXX\YYY\Controller\Index\Result\" for
\"XXX\YYY\Controller\Index\ResultFactory\" generation does not
exist.","1":"
Please provide me a solution
js file
define(['jquery', 'Magento_Ui/js/modal/modal'], function (jQuery, modal) {
return function (config) {
var customFunction = {
ajaxSubmit: function () {
var form_data = jQuery("#review-form").serialize();
alert(form_data);
var reviewurl = config.url;
alert(reviewurl);
jQuery.ajax({
url: reviewurl,
type: 'POST',
data: form_data,
success: function (response) {
alert("successful");
},
error: function (response) {
alert("failed");
}
});
},
options: {
type: 'popup',
responsive: true,
innerScroll: true,
title: jQuery.mage.__('Product Review'),
buttons: [{
text: jQuery.mage.__('submit'),
class: '',
click: function () {
//Ajax function call
customFunction.ajaxSubmit();
var close = this;
setTimeout(function () {
close.closeModal();
}, 3000);
}
}]
}
};
jQuery(".product-reviews-summary").on('click', function () {
modal(customFunction.options, jQuery('#popup-modal'));
jQuery("#popup-modal").modal("openModal");
});
jQuery(".add-review").on('click', function () {
modal(customFunction.options, jQuery('#popup-modal'));
jQuery("#popup-modal").modal("openModal");
});
jQuery(".star-popup").on('click', function () {
modal(customFunction.options, jQuery('#popup-modal'));
jQuery("#popup-modal").modal("openModal");
});
};
});
Controller
namespace XXX\YYY\Controller\Index;
class Index extends \Magento\Review\Controller\Product\Post
{
public function execute()
{
/** @var \Magento\Framework\Controller\Result\Redirect $resultRedirect */
$resultRedirect = $this->resultFactory->create(ResultFactory::TYPE_REDIRECT);
if (!$this->formKeyValidator->validate($this->getRequest())) {
$resultRedirect->setUrl($this->_redirect->getRefererUrl());
return $resultRedirect;
}
$data = $this->reviewSession->getFormData(true);
print_r($data);
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();
$this->messageManager->addSuccess(__('You submitted your review for moderation.'));
} catch (\Exception $e) {
$this->reviewSession->setFormData($data);
$this->messageManager->addError(__('We can\'t post your review right now.'));
}
} else {
$this->reviewSession->setFormData($data);
if (is_array($validate)) {
foreach ($validate as $errorMessage) {
$this->messageManager->addError($errorMessage);
}
} else {
$this->messageManager->addError(__('We can\'t post your review right now.'));
}
}
}
$redirectUrl = $this->reviewSession->getRedirectUrl(true);
$resultRedirect->setUrl($redirectUrl ?: $this->_redirect->getRedirectUrl());
return $resultRedirect;
}
}
Best Answer
Please remove the alert function
alert(reviewurl);
from your js file and modify you controller file as follows and try,