Review.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. <?php
  2. namespace Dotdigitalgroup\Email\Block;
  3. /**
  4. * Review block
  5. *
  6. * @api
  7. */
  8. class Review extends \Magento\Catalog\Block\Product\AbstractProduct
  9. {
  10. /**
  11. * @var \Dotdigitalgroup\Email\Helper\Data
  12. */
  13. public $helper;
  14. /**
  15. * @var \Magento\Framework\Pricing\Helper\Data
  16. */
  17. public $priceHelper;
  18. /**
  19. * @var \Magento\Sales\Api\Data\OrderInterfaceFactory
  20. */
  21. public $orderFactory;
  22. /**
  23. * @var \Dotdigitalgroup\Email\Model\ResourceModel\Review
  24. */
  25. public $review;
  26. /**
  27. * @var \Magento\Sales\Model\Spi\OrderResourceInterface
  28. */
  29. private $orderResource;
  30. /**
  31. * Review constructor.
  32. *
  33. * @param \Magento\Catalog\Block\Product\Context $context
  34. * @param \Magento\Sales\Model\Spi\OrderResourceInterface $orderResource
  35. * @param \Dotdigitalgroup\Email\Model\ResourceModel\Review $review
  36. * @param \Magento\Sales\Api\Data\OrderInterfaceFactory $orderFactory
  37. * @param \Dotdigitalgroup\Email\Helper\Data $helper
  38. * @param \Magento\Framework\Pricing\Helper\Data $priceHelper
  39. * @param array $data
  40. */
  41. public function __construct(
  42. \Magento\Catalog\Block\Product\Context $context,
  43. \Magento\Sales\Model\Spi\OrderResourceInterface $orderResource,
  44. \Dotdigitalgroup\Email\Model\ResourceModel\Review $review,
  45. \Magento\Sales\Api\Data\OrderInterfaceFactory $orderFactory,
  46. \Dotdigitalgroup\Email\Helper\Data $helper,
  47. \Magento\Framework\Pricing\Helper\Data $priceHelper,
  48. array $data = []
  49. ) {
  50. $this->review = $review;
  51. $this->orderFactory = $orderFactory;
  52. $this->helper = $helper;
  53. $this->priceHelper = $priceHelper;
  54. $this->orderResource = $orderResource;
  55. parent::__construct($context, $data);
  56. }
  57. /**
  58. * Current Order.
  59. *
  60. * @return bool|mixed
  61. */
  62. public function getOrder()
  63. {
  64. $params = $this->getRequest()->getParams();
  65. if (! isset($params['code']) || ! $this->helper->isCodeValid($params['code'])) {
  66. $this->helper->log('Review no valid code is set');
  67. return false;
  68. }
  69. $orderId = $this->_coreRegistry->registry('order_id');
  70. $order = $this->_coreRegistry->registry('current_order');
  71. if (! $orderId) {
  72. $orderId = (int) $this->getRequest()->getParam('order_id');
  73. if (! $orderId) {
  74. return false;
  75. }
  76. $this->_coreRegistry->unregister('order_id'); // additional measure
  77. $this->_coreRegistry->register('order_id', $orderId);
  78. }
  79. if (! $order) {
  80. if (! $orderId) {
  81. return false;
  82. }
  83. $order = $this->orderFactory->create();
  84. $this->orderResource->load($order, $orderId);
  85. $this->_coreRegistry->unregister('current_order'); // additional measure
  86. $this->_coreRegistry->register('current_order', $order);
  87. }
  88. return $order;
  89. }
  90. /**
  91. * @param string $mode
  92. *
  93. * @return boolean|string
  94. */
  95. public function getMode($mode = 'list')
  96. {
  97. if ($this->getOrder()) {
  98. $website = $this->_storeManager
  99. ->getStore($this->getOrder()->getStoreId())
  100. ->getWebsite();
  101. $mode = $this->helper->getReviewDisplayType($website);
  102. }
  103. return $mode;
  104. }
  105. /**
  106. * Filter items for review. If a customer has already placed a review for a product then exclude the product.
  107. *
  108. * @param array $items
  109. * @param int $websiteId
  110. *
  111. * @return boolean|array
  112. */
  113. public function filterItemsForReview($items, $websiteId)
  114. {
  115. $order = $this->getOrder();
  116. if (empty($items) || ! $order) {
  117. return false;
  118. }
  119. //if customer is guest then no need to filter any items
  120. if ($order->getCustomerIsGuest()) {
  121. return $items;
  122. }
  123. if (!$this->helper->isNewProductOnly($websiteId)) {
  124. return $items;
  125. }
  126. $customerId = $order->getCustomerId();
  127. $items = $this->review->filterItemsForReview($items, $customerId, $order);
  128. return $items;
  129. }
  130. /**
  131. * @return array|\Magento\Framework\Data\Collection\AbstractDb
  132. */
  133. public function getItems()
  134. {
  135. $order = $this->getOrder();
  136. if (! $order) {
  137. return [];
  138. }
  139. $items = $this->review->getProductCollection($order);
  140. return $items;
  141. }
  142. /**
  143. * @param int|string $productId
  144. *
  145. * @return string
  146. */
  147. public function getReviewItemUrl($productId)
  148. {
  149. return $this->_urlBuilder->getUrl('review/product/list', ['id' => $productId]);
  150. }
  151. }