Recentlyviewed.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. <?php
  2. namespace Dotdigitalgroup\Email\Block\Recommended;
  3. /**
  4. * Recently viewed block
  5. *
  6. * @api
  7. */
  8. class Recentlyviewed 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 \Dotdigitalgroup\Email\Helper\Recommended
  20. */
  21. public $recommnededHelper;
  22. /**
  23. * @var \Magento\Customer\Model\SessionFactory
  24. */
  25. public $sessionFactory;
  26. /**
  27. * @var \Dotdigitalgroup\Email\Model\ResourceModel\Catalog
  28. */
  29. public $catalog;
  30. /**
  31. * @var \Magento\Store\Model\StoreManagerInterface
  32. */
  33. private $storeManager;
  34. /**
  35. * Recentlyviewed constructor.
  36. *
  37. * @param \Magento\Catalog\Block\Product\Context $context
  38. * @param \Dotdigitalgroup\Email\Model\ResourceModel\Catalog $catalog
  39. * @param \Magento\Customer\Model\SessionFactory $sessionFactory
  40. * @param \Dotdigitalgroup\Email\Helper\Data $helper
  41. * @param \Magento\Framework\Pricing\Helper\Data $priceHelper
  42. * @param \Dotdigitalgroup\Email\Helper\Recommended $recommended
  43. * @param array $data
  44. */
  45. public function __construct(
  46. \Magento\Catalog\Block\Product\Context $context,
  47. \Dotdigitalgroup\Email\Model\ResourceModel\Catalog $catalog,
  48. \Magento\Customer\Model\SessionFactory $sessionFactory,
  49. \Dotdigitalgroup\Email\Helper\Data $helper,
  50. \Magento\Framework\Pricing\Helper\Data $priceHelper,
  51. \Dotdigitalgroup\Email\Helper\Recommended $recommended,
  52. array $data = []
  53. ) {
  54. parent::__construct($context, $data);
  55. $this->sessionFactory = $sessionFactory;
  56. $this->helper = $helper;
  57. $this->recommnededHelper = $recommended;
  58. $this->priceHelper = $priceHelper;
  59. $this->storeManager = $this->_storeManager;
  60. $this->catalog = $catalog;
  61. }
  62. /**
  63. * Products collection.
  64. *
  65. * @return array
  66. */
  67. public function getLoadedProductCollection()
  68. {
  69. $params = $this->getRequest()->getParams();
  70. //check for param code and id
  71. if (! isset($params['customer_id']) ||
  72. ! isset($params['code']) ||
  73. ! $this->helper->isCodeValid($params['code'])
  74. ) {
  75. $this->helper->log('Recently viewed no id or valid code is set');
  76. return [];
  77. }
  78. $productsToDisplay = [];
  79. $mode = $this->getRequest()->getActionName();
  80. $customerId = (int) $this->getRequest()->getParam('customer_id');
  81. $limit = (int) $this->recommnededHelper->getDisplayLimitByMode($mode);
  82. //login customer to receive the recent products
  83. $session = $this->sessionFactory->create();
  84. $isLoggedIn = $session->loginById($customerId);
  85. $productIds = $this->catalog->getRecentlyViewed($customerId, $limit);
  86. //get product collection to check for salable
  87. $productCollection = $this->catalog->getProductCollectionFromIds($productIds);
  88. //show products only if is salable
  89. foreach ($productCollection as $product) {
  90. if ($product->isSalable()) {
  91. $productsToDisplay[$product->getId()] = $product;
  92. }
  93. }
  94. $this->helper->log(
  95. 'Recentlyviewed customer : ' . $customerId . ', mode ' . $mode
  96. . ', limit : ' . $limit .
  97. ', items found : ' . count($productIds) . ', is customer logged in : '
  98. . $isLoggedIn . ', products :' . count($productsToDisplay)
  99. );
  100. $session->logout();
  101. return $productsToDisplay;
  102. }
  103. /**
  104. * Display mode type.
  105. *
  106. * @return string|boolean
  107. */
  108. public function getMode()
  109. {
  110. return $this->recommnededHelper->getDisplayType();
  111. }
  112. /**
  113. * @param null|string|bool|int|\Magento\Store\Api\Data\StoreInterface $store
  114. *
  115. * @return string|boolean
  116. */
  117. public function getTextForUrl($store)
  118. {
  119. $store = $this->_storeManager->getStore($store);
  120. return $store->getConfig(
  121. \Dotdigitalgroup\Email\Helper\Config::XML_PATH_CONNECTOR_DYNAMIC_CONTENT_LINK_TEXT
  122. );
  123. }
  124. }