-
-
Notifications
You must be signed in to change notification settings - Fork 87
/
Product.php
157 lines (137 loc) · 4.3 KB
/
Product.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
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
<?php
/**
* Copyright © MagePal LLC. All rights reserved.
* See COPYING.txt for license details.
* http://www.magepal.com | support@magepal.com
*/
namespace MagePal\GoogleTagManager\Block\Data;
use Exception;
use Magento\Catalog\Api\Data\ProductInterface;
use Magento\Catalog\Block\Product\AbstractProduct;
use Magento\Catalog\Block\Product\Context;
use Magento\Catalog\Helper\Data;
use Magento\Catalog\Pricing\Price\FinalPrice;
use MagePal\GoogleTagManager\Block\DataLayer;
use MagePal\GoogleTagManager\DataLayer\ProductData\ProductProvider;
use MagePal\GoogleTagManager\Helper\Product as ProductHelper;
use MagePal\GoogleTagManager\Model\DataLayerEvent;
class Product extends AbstractProduct
{
/**
* Catalog data
*
* @var Data
*/
protected $catalogHelper = null;
/**
* @var ProductHelper
*/
private $productHelper;
/**
* @var ProductProvider
*/
private $productProvider;
/**
* @param Context $context
* @param ProductHelper $productHelper
* @param ProductProvider $productProvider
* @param array $data
*/
public function __construct(
Context $context,
ProductHelper $productHelper,
ProductProvider $productProvider,
array $data = []
) {
$this->catalogHelper = $context->getCatalogHelper();
parent::__construct($context, $data);
$this->productHelper = $productHelper;
$this->productProvider = $productProvider;
}
/**
* Add product data to datalayer
*
* @return $this
*/
protected function _prepareLayout()
{
/** @var $tm DataLayer */
$tm = $this->getParentBlock();
if ($product = $this->getProduct()) {
$productData = [
'id' => $product->getId(),
'sku' => $product->getSku(),
'parent_sku' => $product->getData('sku'),
'product_type' => $product->getTypeId(),
'name' => $product->getName(),
'price' => $this->getPrice(),
'attribute_set_id' => $product->getAttributeSetId(),
'path' => implode(" > ", $this->getBreadCrumbPath()),
'category' => $this->getProductCategoryName(),
'image_url' => $this->productHelper->getImageUrl($product)
];
$productData = $this->productProvider->setProduct($product)->setProductData($productData)->getData();
$data = [
'event' => DataLayerEvent::PRODUCT_PAGE_EVENT,
'product' => $productData
];
$tm->addVariable('list', 'detail');
$tm->addCustomDataLayerByEvent(DataLayerEvent::PRODUCT_PAGE_EVENT, $data);
}
return $this;
}
/**
* @return mixed
*/
public function getPrice()
{
/** @var $tm DataLayer */
$tm = $this->getParentBlock();
$price = 0;
/** @var $product ProductInterface */
if ($this->getProduct()) {
$price = $this->getProduct()
->getPriceInfo()
->getPrice(FinalPrice::PRICE_CODE)
->getAmount()
->getBaseAmount() ?: 0;
}
return $tm->formatPrice($price);
}
/**
* Get category name from breadcrumb
*
* @return string
*/
protected function getProductCategoryName()
{
$categoryName = '';
try {
$categoryArray = $this->getBreadCrumbPath();
if (count($categoryArray) > 1) {
end($categoryArray);
$categoryName = prev($categoryArray);
} elseif ($this->getProduct()) {
$category = $this->getProduct()->getCategoryCollection()->addAttributeToSelect('name')->getFirstItem();
$categoryName = ($category) ? $category->getName() : '';
}
} catch (Exception $e) {
$categoryName = '';
}
return $categoryName;
}
/**
* Get bread crumb path
*
* @return array
*/
protected function getBreadCrumbPath()
{
$titleArray = [];
$breadCrumbs = $this->catalogHelper->getBreadcrumbPath();
foreach ($breadCrumbs as $breadCrumb) {
$titleArray[] = $breadCrumb['label'];
}
return $titleArray;
}
}