Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Guest.php
Go to the documentation of this file.
1 <?php
7 namespace Magento\Sales\Helper;
8 
13 use \Magento\Sales\Model\Order;
14 
20 {
26  protected $coreRegistry;
27 
31  protected $customerSession;
32 
36  protected $cookieManager;
37 
42 
46  protected $messageManager;
47 
51  protected $orderFactory;
52 
57 
61  private $orderRepository;
62 
66  private $searchCriteriaBuilder;
67 
71  const COOKIE_NAME = 'guest-view';
72 
76  const COOKIE_PATH = '/';
77 
81  const COOKIE_LIFETIME = 600;
82 
86  private $storeManager;
87 
91  private $inputExceptionMessage = 'You entered incorrect data. Please try again.';
92 
108  public function __construct(
109  App\Helper\Context $context,
110  \Magento\Store\Model\StoreManagerInterface $storeManager,
111  \Magento\Framework\Registry $coreRegistry,
112  \Magento\Customer\Model\Session $customerSession,
114  \Magento\Framework\Stdlib\Cookie\CookieMetadataFactory $cookieMetadataFactory,
115  \Magento\Framework\Message\ManagerInterface $messageManager,
116  \Magento\Sales\Model\OrderFactory $orderFactory,
117  \Magento\Framework\Controller\Result\RedirectFactory $resultRedirectFactory,
118  \Magento\Sales\Api\OrderRepositoryInterface $orderRepository = null,
119  \Magento\Framework\Api\SearchCriteriaBuilder $searchCriteria = null
120  ) {
121  $this->coreRegistry = $coreRegistry;
122  $this->storeManager = $storeManager;
123  $this->customerSession = $customerSession;
124  $this->cookieManager = $cookieManager;
125  $this->cookieMetadataFactory = $cookieMetadataFactory;
126  $this->messageManager = $messageManager;
127  $this->orderFactory = $orderFactory;
128  $this->resultRedirectFactory = $resultRedirectFactory;
129  $this->orderRepository = $orderRepository ?: \Magento\Framework\App\ObjectManager::getInstance()
130  ->get(\Magento\Sales\Api\OrderRepositoryInterface::class);
131  $this->searchCriteriaBuilder = $searchCriteria?: \Magento\Framework\App\ObjectManager::getInstance()
132  ->get(\Magento\Framework\Api\SearchCriteriaBuilder::class);
133  parent::__construct(
134  $context
135  );
136  }
137 
148  public function loadValidOrder(App\RequestInterface $request)
149  {
150  if ($this->customerSession->isLoggedIn()) {
151  return $this->resultRedirectFactory->create()->setPath('sales/order/history');
152  }
153  $post = $request->getPostValue();
154  $fromCookie = $this->cookieManager->getCookie(self::COOKIE_NAME);
155  if (empty($post) && !$fromCookie) {
156  return $this->resultRedirectFactory->create()->setPath('sales/guest/form');
157  }
158  // It is unique place in the class that process exception and only InputException. It is need because by
159  // input data we found order and one more InputException could be throws deeper in stack trace
160  try {
161  $order = (!empty($post)
162  && isset($post['oar_order_id'], $post['oar_type'])
163  && !$this->hasPostDataEmptyFields($post))
164  ? $this->loadFromPost($post) : $this->loadFromCookie($fromCookie);
165  $this->coreRegistry->register('current_order', $order);
166  return true;
167  } catch (InputException $e) {
168  $this->messageManager->addErrorMessage($e->getMessage());
169  return $this->resultRedirectFactory->create()->setPath('sales/guest/form');
170  }
171  }
172 
179  public function getBreadcrumbs(\Magento\Framework\View\Result\Page $resultPage)
180  {
181  $breadcrumbs = $resultPage->getLayout()->getBlock('breadcrumbs');
182  if (!$breadcrumbs) {
183  return;
184  }
185  $breadcrumbs->addCrumb(
186  'home',
187  [
188  'label' => __('Home'),
189  'title' => __('Go to Home Page'),
190  'link' => $this->storeManager->getStore()->getBaseUrl()
191  ]
192  );
193  $breadcrumbs->addCrumb(
194  'cms_page',
195  ['label' => __('Order Information'), 'title' => __('Order Information')]
196  );
197  }
198 
208  private function setGuestViewCookie($cookieValue)
209  {
210  $metadata = $this->cookieMetadataFactory->createPublicCookieMetadata()
211  ->setPath(self::COOKIE_PATH)
212  ->setHttpOnly(true);
213  $this->cookieManager->setPublicCookie(self::COOKIE_NAME, $cookieValue, $metadata);
214  }
215 
225  private function loadFromCookie($fromCookie)
226  {
227  $cookieData = explode(':', base64_decode($fromCookie));
228  $protectCode = isset($cookieData[0]) ? $cookieData[0] : null;
229  $incrementId = isset($cookieData[1]) ? $cookieData[1] : null;
230  if (!empty($protectCode) && !empty($incrementId)) {
231  $order = $this->getOrderRecord($incrementId);
232  if (hash_equals((string)$order->getProtectCode(), $protectCode)) {
233  $this->setGuestViewCookie($fromCookie);
234  return $order;
235  }
236  }
237  throw new InputException(__($this->inputExceptionMessage));
238  }
239 
249  private function loadFromPost(array $postData)
250  {
252  $order = $this->getOrderRecord($postData['oar_order_id']);
253  if (!$this->compareStoredBillingDataWithInput($order, $postData)) {
254  throw new InputException(__('You entered incorrect data. Please try again.'));
255  }
256  $toCookie = base64_encode($order->getProtectCode() . ':' . $postData['oar_order_id']);
257  $this->setGuestViewCookie($toCookie);
258  return $order;
259  }
260 
268  private function compareStoredBillingDataWithInput(Order $order, array $postData)
269  {
270  $type = $postData['oar_type'];
271  $email = $postData['oar_email'];
272  $lastName = $postData['oar_billing_lastname'];
273  $zip = $postData['oar_zip'];
274  $billingAddress = $order->getBillingAddress();
275  return strtolower($lastName) === strtolower($billingAddress->getLastname()) &&
276  ($type === 'email' && strtolower($email) === strtolower($billingAddress->getEmail()) ||
277  $type === 'zip' && strtolower($zip) === strtolower($billingAddress->getPostcode()));
278  }
279 
286  private function hasPostDataEmptyFields(array $postData)
287  {
288  return empty($postData['oar_order_id']) || empty($postData['oar_billing_lastname']) ||
289  empty($postData['oar_type']) || empty($this->storeManager->getStore()->getId()) ||
290  !in_array($postData['oar_type'], ['email', 'zip'], true) ||
291  ('email' === $postData['oar_type'] && empty($postData['oar_email'])) ||
292  ('zip' === $postData['oar_type'] && empty($postData['oar_zip']));
293  }
294 
302  private function getOrderRecord($incrementId)
303  {
304  $records = $this->orderRepository->getList(
305  $this->searchCriteriaBuilder
306  ->addFilter('increment_id', $incrementId)
307  ->addFilter('store_id', $this->storeManager->getStore()->getId())
308  ->create()
309  );
310 
311  $items = $records->getItems();
312  if (empty($items)) {
313  throw new InputException(__($this->inputExceptionMessage));
314  }
315 
316  return array_shift($items);
317  }
318 }
$billingAddress
Definition: order.php:25
$email
Definition: details.phtml:13
getBreadcrumbs(\Magento\Framework\View\Result\Page $resultPage)
Definition: Guest.php:179
$order
Definition: order.php:55
$storeManager
__()
Definition: __.php:13
loadValidOrder(App\RequestInterface $request)
Definition: Guest.php:148
$searchCriteria
$type
Definition: item.phtml:13
__construct(App\Helper\Context $context, \Magento\Store\Model\StoreManagerInterface $storeManager, \Magento\Framework\Registry $coreRegistry, \Magento\Customer\Model\Session $customerSession, \Magento\Framework\Stdlib\CookieManagerInterface $cookieManager, \Magento\Framework\Stdlib\Cookie\CookieMetadataFactory $cookieMetadataFactory, \Magento\Framework\Message\ManagerInterface $messageManager, \Magento\Sales\Model\OrderFactory $orderFactory, \Magento\Framework\Controller\Result\RedirectFactory $resultRedirectFactory, \Magento\Sales\Api\OrderRepositoryInterface $orderRepository=null, \Magento\Framework\Api\SearchCriteriaBuilder $searchCriteria=null)
Definition: Guest.php:108
$items