Getbasket.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. <?php
  2. namespace Dotdigitalgroup\Email\Controller\Email;
  3. class Getbasket extends \Magento\Framework\App\Action\Action
  4. {
  5. /**
  6. * @var \Magento\Quote\Model\ResourceModel\Quote
  7. */
  8. private $quoteResource;
  9. /**
  10. * @var \Magento\Quote\Model\QuoteFactory
  11. */
  12. private $quoteFactory;
  13. /**
  14. * @var \Magento\Customer\Model\SessionFactory
  15. */
  16. private $checkoutSession;
  17. /**
  18. * @var \Magento\Quote\Model\Quote
  19. */
  20. private $quote;
  21. /**
  22. * @var \Magento\Customer\Model\SessionFactory
  23. */
  24. private $customerSessionFactory;
  25. /**
  26. * Getbasket constructor.
  27. *
  28. * @param \Magento\Quote\Model\ResourceModel\Quote $quoteResource
  29. * @param \Magento\Checkout\Model\SessionFactory $checkoutSessionFactory
  30. * @param \Magento\Quote\Model\QuoteFactory $quoteFactory
  31. * @param \Magento\Framework\App\Action\Context $context
  32. * @param \Magento\Customer\Model\SessionFactory $customerSessionFactory
  33. */
  34. public function __construct(
  35. \Magento\Quote\Model\ResourceModel\Quote $quoteResource,
  36. \Magento\Checkout\Model\SessionFactory $checkoutSessionFactory,
  37. \Magento\Quote\Model\QuoteFactory $quoteFactory,
  38. \Magento\Framework\App\Action\Context $context,
  39. \Magento\Customer\Model\SessionFactory $customerSessionFactory
  40. ) {
  41. $this->checkoutSession = $checkoutSessionFactory;
  42. $this->quoteFactory = $quoteFactory;
  43. $this->quoteResource = $quoteResource;
  44. $this->customerSessionFactory = $customerSessionFactory;
  45. parent::__construct($context);
  46. }
  47. /**
  48. * Wishlist page to display the user items with specific email.
  49. *
  50. * @return null
  51. */
  52. public function execute()
  53. {
  54. $quoteId = $this->getRequest()->getParam('quote_id');
  55. //no quote id redirect to base url
  56. if (!$quoteId) {
  57. return $this->_redirect('');
  58. }
  59. /** @var \Magento\Quote\Model\Quote $quoteModel */
  60. $quoteModel = $this->quoteFactory->create();
  61. $this->quoteResource->load($quoteModel, $quoteId);
  62. //no quote id redirect to base url
  63. if (! $quoteModel->getId()) {
  64. return $this->_redirect('');
  65. }
  66. //set quoteModel to _quote property for later use
  67. $this->quote = $quoteModel;
  68. if ($quoteModel->getCustomerId()) {
  69. return $this->handleCustomerBasket();
  70. } else {
  71. return $this->handleGuestBasket();
  72. }
  73. }
  74. /**
  75. * Process customer basket.
  76. *
  77. * @return null
  78. */
  79. private function handleCustomerBasket()
  80. {
  81. /** @var \Magento\Customer\Model\Session $customerSession */
  82. $customerSession = $this->customerSessionFactory->create();
  83. $configCartUrl = $this->quote->getStore()->getWebsite()->getConfig(
  84. \Dotdigitalgroup\Email\Helper\Config::XML_PATH_CONNECTOR_CONTENT_CART_URL
  85. );
  86. //if customer is logged in then redirect to cart
  87. if ($customerSession->isLoggedIn()
  88. && $customerSession->getCustomerId() == $this->quote->getCustomerId()) {
  89. $checkoutSession = $this->checkoutSession->create();
  90. if ($checkoutSession->getQuote()
  91. && $checkoutSession->getQuote()->hasItems()
  92. ) {
  93. $quote = $checkoutSession->getQuote();
  94. if ($this->quote->getId() != $quote->getId()) {
  95. $this->checkMissingAndAdd();
  96. }
  97. }
  98. if ($configCartUrl) {
  99. $url = $configCartUrl;
  100. } else {
  101. $url = $this->quote->getStore()->getUrl(
  102. 'checkout/cart'
  103. );
  104. }
  105. $this->_redirect($url);
  106. } else {
  107. //set after auth url. customer will be redirected to cart after successful login
  108. if ($configCartUrl) {
  109. $cartUrl = $configCartUrl;
  110. } else {
  111. $cartUrl = 'checkout/cart';
  112. }
  113. $customerSession->setAfterAuthUrl(
  114. $this->quote->getStore()->getUrl($cartUrl)
  115. );
  116. //send customer to login page
  117. $configLoginUrl = $this->quote->getStore()->getWebsite()
  118. ->getConfig(
  119. \Dotdigitalgroup\Email\Helper\Config::XML_PATH_CONNECTOR_CONTENT_LOGIN_URL
  120. );
  121. if ($configLoginUrl) {
  122. $loginUrl = $configLoginUrl;
  123. } else {
  124. $loginUrl = 'customer/account/login';
  125. }
  126. $this->_redirect($this->quote->getStore()->getUrl($loginUrl));
  127. }
  128. }
  129. /**
  130. * Check missing items from current quote and add.
  131. *
  132. * @return null
  133. */
  134. private function checkMissingAndAdd()
  135. {
  136. /** @var \Magento\Checkout\Model\Session $checkoutSession */
  137. $checkoutSession = $this->checkoutSession->create();
  138. $currentQuote = $checkoutSession->getQuote();
  139. if ($currentQuote->hasItems()) {
  140. $currentSessionItems = $currentQuote->getAllItems();
  141. $currentItemIds = [];
  142. foreach ($currentSessionItems as $currentSessionItem) {
  143. $currentItemIds[] = $currentSessionItem->getId();
  144. }
  145. /** @var \Magento\Quote\Model\Quote\Item $item */
  146. foreach ($this->quote->getAllItems() as $item) {
  147. if (!in_array($item->getId(), $currentItemIds)) {
  148. $currentQuote->addItem($item);
  149. }
  150. }
  151. $currentQuote->collectTotals();
  152. $this->quoteResource->save($currentQuote);
  153. }
  154. }
  155. /**
  156. * Process guest basket.
  157. *
  158. * @return null
  159. */
  160. private function handleGuestBasket()
  161. {
  162. $configCartUrl = $this->quote->getStore()->getWebsite()->getConfig(
  163. \Dotdigitalgroup\Email\Helper\Config::XML_PATH_CONNECTOR_CONTENT_CART_URL
  164. );
  165. if ($configCartUrl) {
  166. $url = $configCartUrl;
  167. } else {
  168. $url = 'checkout/cart';
  169. }
  170. $this->_redirect($this->quote->getStore()->getUrl($url));
  171. }
  172. }