-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathViewItem.php
130 lines (113 loc) · 3.4 KB
/
ViewItem.php
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
<?php
namespace Taggrs\DataLayer\Block\Event;
use Magento\Catalog\Api\Data\ProductInterface;
use Magento\Catalog\Api\ProductRepositoryInterface;
use Magento\Framework\App\RequestInterface;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Framework\View\Element\Template;
use Magento\Framework\View\Element\Template\Context;
use Taggrs\DataLayer\Block\DataLayer;
use Taggrs\DataLayer\Helper\ProductHelper;
use Taggrs\DataLayer\Helper\UserDataHelper;
/**
* Renders a view_item event on the Product Detail Page
*/
class ViewItem extends DataLayer
{
/**
* @var RequestInterface request object
*/
private RequestInterface $request;
/**
* @var ProductRepositoryInterface to retrieve products from database
*/
private ProductRepositoryInterface $productRepository;
/**
* @var ProductHelper to retrieve specific product information
*/
private ProductHelper $productHelper;
/**
* Class constructor
*
* @param RequestInterface $request
* @param ProductRepositoryInterface $productRepository
* @param UserDataHelper $userDataHelper
* @param Context $context
* @param ProductHelper $productHelper
* @param array $data
*/
public function __construct(
RequestInterface $request,
ProductRepositoryInterface $productRepository,
UserDataHelper $userDataHelper,
Template\Context $context,
ProductHelper $productHelper,
array $data = []
) {
parent::__construct($userDataHelper, $context, $data);
$this->request = $request;
$this->productRepository = $productRepository;
$this->productHelper = $productHelper;
}
/**
* Get the event name
*
* @return string the event name
*/
public function getEvent(): string
{
return 'view_item';
}
/**
* Get the e-commerce Data Layer
*
* @return array containing the e-commerce data
*/
public function getEcommerce(): array
{
try {
$product = $this->getCurrentProduct();
} catch ( NoSuchEntityException $e ) {
$product = null;
}
try {
$currency = $this->_storeManager
->getStore()
->getCurrentCurrency()
->getCode()
;
} catch ( NoSuchEntityException|LocalizedException $e ) {
$currency = null;
}
if ($product !== null) {
$price = (float)$product->getFinalPrice();
$item = [
'item_id' => $product->getSku(),
'item_name' => $product->getName(),
'price' => $price,
];
$item = array_merge($item, $this->productHelper->getCategoryNamesByProduct($product));
} else {
$item = [];
$price = 0;
}
return [
'currency' => $currency,
'value' => $price,
'items' => [$item],
'user_data' => $this->getUserData(),
];
}
/**
* Get the current product using the ID from the URL.
*
* @return ProductInterface
* @throws NoSuchEntityException
*/
private function getCurrentProduct(): ProductInterface
{
$id = $this->request->getParam('id');
return $this->productRepository->getById($id);
}
}