Basket.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. <?php
  2. namespace Dotdigitalgroup\Email\Block;
  3. /**
  4. * Basket block
  5. *
  6. * @api
  7. */
  8. class Basket 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\Quote\Model\Quote
  20. */
  21. public $quote;
  22. /**
  23. * @var \Magento\Quote\Model\QuoteFactory
  24. */
  25. public $quoteFactory;
  26. /**
  27. * @var \Magento\Store\Model\App\EmulationFactory
  28. */
  29. public $emulationFactory;
  30. /**
  31. * Basket constructor.
  32. *
  33. * @param \Magento\Catalog\Block\Product\Context $context
  34. * @param \Magento\Store\Model\App\EmulationFactory $emulationFactory
  35. * @param \Magento\Quote\Model\QuoteFactory $quoteFactory
  36. * @param \Dotdigitalgroup\Email\Helper\Data $helper
  37. * @param \Magento\Framework\Pricing\Helper\Data $priceHelper
  38. * @param array $data
  39. */
  40. public function __construct(
  41. \Magento\Catalog\Block\Product\Context $context,
  42. \Magento\Store\Model\App\EmulationFactory $emulationFactory,
  43. \Magento\Quote\Model\QuoteFactory $quoteFactory,
  44. \Dotdigitalgroup\Email\Helper\Data $helper,
  45. \Magento\Framework\Pricing\Helper\Data $priceHelper,
  46. array $data = []
  47. ) {
  48. $this->quoteFactory = $quoteFactory;
  49. $this->helper = $helper;
  50. $this->priceHelper = $priceHelper;
  51. $this->emulationFactory = $emulationFactory;
  52. parent::__construct($context, $data);
  53. }
  54. /**
  55. * Basket items.
  56. *
  57. * @return array
  58. */
  59. public function getBasketItems()
  60. {
  61. $params = $this->getRequest()->getParams();
  62. if (! isset($params['quote_id']) ||
  63. ! isset($params['code']) ||
  64. ! $this->helper->isCodeValid($params['code'])
  65. ) {
  66. $this->helper->log('Abandoned cart not found or invalid code');
  67. return false;
  68. }
  69. $quoteId = (int) $params['quote_id'];
  70. $quoteModel = $this->quoteFactory->create()
  71. ->loadByIdWithoutStore($quoteId);
  72. //check for any quote for this email, don't want to render further
  73. if (!$quoteModel->getId()) {
  74. $this->helper->log('no quote found for ' . $quoteId);
  75. return false;
  76. }
  77. if (!$quoteModel->getIsActive()) {
  78. $this->helper->log('Cart is not active : ' . $quoteId);
  79. return false;
  80. }
  81. $this->quote = $quoteModel;
  82. //Start environment emulation of the specified store
  83. $storeId = $quoteModel->getStoreId();
  84. $appEmulation = $this->emulationFactory->create();
  85. $appEmulation->startEnvironmentEmulation($storeId);
  86. $quoteItems = $quoteModel->getAllItems();
  87. $itemsData = [];
  88. /** @var \Magento\Quote\Model\Quote\Item $quoteItem */
  89. foreach ($quoteItems as $quoteItem) {
  90. //skip configurable products
  91. if ($quoteItem->getParentItemId() != null) {
  92. continue;
  93. }
  94. $_product = $quoteItem->getProduct();
  95. $inStock = ($_product->isInStock())
  96. ? 'In Stock'
  97. : 'Out of stock';
  98. $total = $this->priceHelper->currency(
  99. $quoteItem->getBaseRowTotalInclTax(),
  100. true,
  101. false
  102. );
  103. $productUrl = $_product->getProductUrl();
  104. $grandTotal = $this->priceHelper->currency(
  105. $this->getGrandTotal(),
  106. true,
  107. false
  108. );
  109. $itemsData[] = [
  110. 'grandTotal' => $grandTotal,
  111. 'total' => $total,
  112. 'inStock' => $inStock,
  113. 'productUrl' => $productUrl,
  114. 'product' => $_product,
  115. 'qty' => $quoteItem->getQty(),
  116. ];
  117. }
  118. return $itemsData;
  119. }
  120. /**
  121. * Grand total.
  122. *
  123. * @return float
  124. */
  125. public function getGrandTotal()
  126. {
  127. return $this->quote->getGrandTotal();
  128. }
  129. /**
  130. * Url for "take me to basket" link.
  131. *
  132. * @return string
  133. */
  134. public function getUrlForLink()
  135. {
  136. return $this->quote->getStore()->getUrl(
  137. 'connector/email/getbasket',
  138. ['quote_id' => $this->quote->getId()]
  139. );
  140. }
  141. /**
  142. * Can show go to basket url.
  143. *
  144. * @return bool
  145. */
  146. public function canShowUrl()
  147. {
  148. return (boolean)$this->quote->getStore()->getWebsite()->getConfig(
  149. \Dotdigitalgroup\Email\Helper\Config::XML_PATH_CONNECTOR_CONTENT_LINK_ENABLED
  150. );
  151. }
  152. /**
  153. * @return string|boolean
  154. */
  155. public function takeMeToCartTextForUrl()
  156. {
  157. return $this->quote->getStore()->getWebsite()->getConfig(
  158. \Dotdigitalgroup\Email\Helper\Config::XML_PATH_CONNECTOR_CONTENT_LINK_TEXT
  159. );
  160. }
  161. /**
  162. * Get dynamic style configuration.
  163. *
  164. * @return array
  165. */
  166. public function getDynamicStyle()
  167. {
  168. $dynamicStyle = $this->helper->getDynamicStyles();
  169. return $dynamicStyle;
  170. }
  171. }