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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
<?php
namespace Dotdigitalgroup\Email\Block;
/**
* Basket block
*
* @api
*/
class Basket extends \Magento\Catalog\Block\Product\AbstractProduct
{
/**
* @var \Dotdigitalgroup\Email\Helper\Data
*/
public $helper;
/**
* @var \Magento\Framework\Pricing\Helper\Data
*/
public $priceHelper;
/**
* @var \Magento\Quote\Model\Quote
*/
public $quote;
/**
* @var \Magento\Quote\Model\QuoteFactory
*/
public $quoteFactory;
/**
* @var \Magento\Store\Model\App\EmulationFactory
*/
public $emulationFactory;
/**
* Basket constructor.
*
* @param \Magento\Catalog\Block\Product\Context $context
* @param \Magento\Store\Model\App\EmulationFactory $emulationFactory
* @param \Magento\Quote\Model\QuoteFactory $quoteFactory
* @param \Dotdigitalgroup\Email\Helper\Data $helper
* @param \Magento\Framework\Pricing\Helper\Data $priceHelper
* @param array $data
*/
public function __construct(
\Magento\Catalog\Block\Product\Context $context,
\Magento\Store\Model\App\EmulationFactory $emulationFactory,
\Magento\Quote\Model\QuoteFactory $quoteFactory,
\Dotdigitalgroup\Email\Helper\Data $helper,
\Magento\Framework\Pricing\Helper\Data $priceHelper,
array $data = []
) {
$this->quoteFactory = $quoteFactory;
$this->helper = $helper;
$this->priceHelper = $priceHelper;
$this->emulationFactory = $emulationFactory;
parent::__construct($context, $data);
}
/**
* Basket items.
*
* @return array
*/
public function getBasketItems()
{
$params = $this->getRequest()->getParams();
if (! isset($params['quote_id']) ||
! isset($params['code']) ||
! $this->helper->isCodeValid($params['code'])
) {
$this->helper->log('Abandoned cart not found or invalid code');
return false;
}
$quoteId = (int) $params['quote_id'];
$quoteModel = $this->quoteFactory->create()
->loadByIdWithoutStore($quoteId);
//check for any quote for this email, don't want to render further
if (!$quoteModel->getId()) {
$this->helper->log('no quote found for ' . $quoteId);
return false;
}
if (!$quoteModel->getIsActive()) {
$this->helper->log('Cart is not active : ' . $quoteId);
return false;
}
$this->quote = $quoteModel;
//Start environment emulation of the specified store
$storeId = $quoteModel->getStoreId();
$appEmulation = $this->emulationFactory->create();
$appEmulation->startEnvironmentEmulation($storeId);
$quoteItems = $quoteModel->getAllItems();
$itemsData = [];
/** @var \Magento\Quote\Model\Quote\Item $quoteItem */
foreach ($quoteItems as $quoteItem) {
//skip configurable products
if ($quoteItem->getParentItemId() != null) {
continue;
}
$_product = $quoteItem->getProduct();
$inStock = ($_product->isInStock())
? 'In Stock'
: 'Out of stock';
$total = $this->priceHelper->currency(
$quoteItem->getBaseRowTotalInclTax(),
true,
false
);
$productUrl = $_product->getProductUrl();
$grandTotal = $this->priceHelper->currency(
$this->getGrandTotal(),
true,
false
);
$itemsData[] = [
'grandTotal' => $grandTotal,
'total' => $total,
'inStock' => $inStock,
'productUrl' => $productUrl,
'product' => $_product,
'qty' => $quoteItem->getQty(),
];
}
return $itemsData;
}
/**
* Grand total.
*
* @return float
*/
public function getGrandTotal()
{
return $this->quote->getGrandTotal();
}
/**
* Url for "take me to basket" link.
*
* @return string
*/
public function getUrlForLink()
{
return $this->quote->getStore()->getUrl(
'connector/email/getbasket',
['quote_id' => $this->quote->getId()]
);
}
/**
* Can show go to basket url.
*
* @return bool
*/
public function canShowUrl()
{
return (boolean)$this->quote->getStore()->getWebsite()->getConfig(
\Dotdigitalgroup\Email\Helper\Config::XML_PATH_CONNECTOR_CONTENT_LINK_ENABLED
);
}
/**
* @return string|boolean
*/
public function takeMeToCartTextForUrl()
{
return $this->quote->getStore()->getWebsite()->getConfig(
\Dotdigitalgroup\Email\Helper\Config::XML_PATH_CONNECTOR_CONTENT_LINK_TEXT
);
}
/**
* Get dynamic style configuration.
*
* @return array
*/
public function getDynamicStyle()
{
$dynamicStyle = $this->helper->getDynamicStyles();
return $dynamicStyle;
}
}