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