Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Post.php
Go to the documentation of this file.
1 <?php
7 
8 use Magento\Framework\App\Action\HttpPostActionInterface as HttpPostActionInterface;
13 
14 class Post extends ProductController implements HttpPostActionInterface
15 {
19  public function execute()
20  {
21  $productId = $this->getRequest()->getParam('product_id', false);
23  $resultRedirect = $this->resultFactory->create(ResultFactory::TYPE_REDIRECT);
24  if ($data = $this->getRequest()->getPostValue()) {
26  $storeManager = $this->_objectManager->get(\Magento\Store\Model\StoreManagerInterface::class);
27  if ($storeManager->hasSingleStore()) {
28  $data['stores'] = [
29  $storeManager->getStore(true)->getId(),
30  ];
31  } elseif (isset($data['select_stores'])) {
32  $data['stores'] = $data['select_stores'];
33  }
34  $review = $this->reviewFactory->create()->setData($data);
35  try {
36  $review->setEntityId(1) // product
37  ->setEntityPkValue($productId)
38  ->setStoreId(Store::DEFAULT_STORE_ID)
39  ->setStatusId($data['status_id'])
40  ->setCustomerId(null)//null is for administrator only
41  ->save();
42 
43  $arrRatingId = $this->getRequest()->getParam('ratings', []);
44  foreach ($arrRatingId as $ratingId => $optionId) {
45  $this->ratingFactory->create()
46  ->setRatingId($ratingId)
47  ->setReviewId($review->getId())
48  ->addOptionVote($optionId, $productId);
49  }
50 
51  $review->aggregate();
52 
53  $this->messageManager->addSuccess(__('You saved the review.'));
54  if ($this->getRequest()->getParam('ret') == 'pending') {
55  $resultRedirect->setPath('review/*/pending');
56  } else {
57  $resultRedirect->setPath('review/*/');
58  }
59  return $resultRedirect;
60  } catch (LocalizedException $e) {
61  $this->messageManager->addError($e->getMessage());
62  } catch (\Exception $e) {
63  $this->messageManager->addException($e, __('Something went wrong while saving this review.'));
64  }
65  }
66  $resultRedirect->setPath('review/*/');
67  return $resultRedirect;
68  }
69 }
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
Definition: Post.php:14
__()
Definition: __.php:13