Skip to content

Commit 620efbb

Browse files
committed
Implemented variation details and select product event
1 parent 1f322f8 commit 620efbb

20 files changed

+578
-59
lines changed

Block/Hyva/ExtraData.php

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
<?php
2+
3+
namespace Stape\Gtm\Block\Hyva;
4+
5+
use Magento\Framework\Registry;
6+
use Magento\Framework\Serialize\Serializer\Json;
7+
use Magento\Framework\View\Element\Template\Context;
8+
use Stape\Gtm\Model\Product\Mapper\EventItemsMapper;
9+
10+
class ExtraData extends \Magento\Framework\View\Element\Template
11+
{
12+
13+
/**
14+
* @var Registry $coreRegistry
15+
*/
16+
private $coreRegistry;
17+
18+
/**
19+
* @var Json $json
20+
*/
21+
private $json;
22+
23+
/**
24+
* @var EventItemsMapper $mapper
25+
*/
26+
private $mapper;
27+
28+
/**
29+
* Define class dependencies
30+
*
31+
* @param Context $context
32+
* @param Registry $coreRegistry
33+
* @param Json $json
34+
* @param array $data
35+
*/
36+
public function __construct(
37+
Context $context,
38+
Registry $coreRegistry,
39+
Json $json,
40+
EventItemsMapper $mapper,
41+
array $data = []
42+
) {
43+
parent::__construct($context, $data);
44+
45+
$this->coreRegistry = $coreRegistry;
46+
$this->json = $json;
47+
$this->mapper = $mapper;
48+
}
49+
50+
/**
51+
* Retrive current product
52+
*
53+
* @return \Magento\Catalog\Model\Product
54+
*/
55+
public function getProduct()
56+
{
57+
return $this->coreRegistry->registry('current_product');
58+
}
59+
60+
/**
61+
* Retrieve currency code
62+
*
63+
* @return string
64+
* @throws \Magento\Framework\Exception\LocalizedException
65+
* @throws \Magento\Framework\Exception\NoSuchEntityException
66+
*/
67+
public function getCurrencyCode()
68+
{
69+
return $this->_storeManager->getStore()->getCurrentCurrency()->getCode();
70+
}
71+
72+
/**
73+
* Convert array to json
74+
*
75+
* @param array $data
76+
* @return bool|string
77+
*/
78+
public function dataToJson(array $data)
79+
{
80+
return $this->json->serialize($data);
81+
}
82+
83+
/**
84+
* Convert products to event items
85+
*
86+
* @param array $items
87+
* @return array
88+
*/
89+
public function toEventItems($items)
90+
{
91+
return $this->mapper->toEventItems($items);
92+
}
93+
}

ViewModel/Compare.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
namespace Stape\Gtm\ViewModel;
4+
5+
use Magento\Framework\Serialize\Serializer\Json;
6+
use Magento\Framework\View\Element\Block\ArgumentInterface;
7+
8+
class Compare implements ArgumentInterface, DatalayerInterface
9+
{
10+
/**
11+
* @var Json $json
12+
*/
13+
private $json;
14+
15+
public function __construct(Json $json)
16+
{
17+
$this->json = $json;
18+
}
19+
20+
/**
21+
* Retrieve json
22+
*
23+
* @return bool|string
24+
* @throws \Magento\Framework\Exception\LocalizedException
25+
* @throws \Magento\Framework\Exception\NoSuchEntityException
26+
*/
27+
public function getJson()
28+
{
29+
return $this->json->serialize([]);
30+
}
31+
}

ViewModel/Compare/ExtraData.php

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
<?php
2+
3+
namespace Stape\Gtm\ViewModel\Compare;
4+
5+
use Magento\Catalog\Block\Product\Compare\ListCompare;
6+
use Magento\Framework\Serialize\Serializer\Json;
7+
use Magento\Framework\View\Element\Block\ArgumentInterface;
8+
use Magento\Framework\View\Layout;
9+
use Magento\Store\Model\StoreManagerInterface;
10+
use Magento\Wishlist\Block\Customer\Wishlist;
11+
use Stape\Gtm\Model\Product\Mapper\EventItemsMapper;
12+
use Stape\Gtm\ViewModel\DatalayerInterface;
13+
14+
class ExtraData implements ArgumentInterface, DatalayerInterface
15+
{
16+
17+
/**
18+
* @var Layout $layout
19+
*/
20+
protected $layout;
21+
22+
/**
23+
* @var StoreManagerInterface $storeManager
24+
*/
25+
protected $storeManager;
26+
27+
/**
28+
* @var EventItemsMapper $mapper
29+
*/
30+
protected $mapper;
31+
32+
/**
33+
* @var Json $json
34+
*/
35+
protected $json;
36+
37+
/**
38+
* Define class dependencies
39+
*
40+
* @param Layout $layout
41+
*/
42+
public function __construct(
43+
Layout $layout,
44+
StoreManagerInterface $storeManager,
45+
EventItemsMapper $mapper,
46+
Json $json
47+
) {
48+
$this->layout = $layout;
49+
$this->storeManager = $storeManager;
50+
$this->mapper = $mapper;
51+
$this->json = $json;
52+
}
53+
54+
/**
55+
* Retrieve json
56+
*
57+
* @return bool|string
58+
*/
59+
public function getJson()
60+
{
61+
$compareList = $this->layout->createBlock(ListCompare::class);
62+
return $this->json->serialize([
63+
'currency' => $this->storeManager->getStore()->getCurrentCurrency()->getCode(),
64+
'lists' => [
65+
[
66+
'item_list_name' => 'products',
67+
'items' => $this->mapper->toEventItems($compareList->getItems())
68+
]
69+
]
70+
]);
71+
}
72+
}

ViewModel/Product/LinkedProducts.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@ class LinkedProducts implements ArgumentInterface, DatalayerInterface
2020
* @var string[]
2121
*/
2222
private $blockTypes = [
23-
'related' => 'catalog.product.related',
24-
'upsell' => 'catalog.product.upsell',
25-
'crosssell' => 'checkout.cart.crosssell'
23+
'catalog.product.related' => 'related',
24+
'product.info.upsell' => 'upsell',
25+
'checkout.cart.crosssell' => 'crosssell',
2626
];
2727

2828
/**
@@ -99,7 +99,7 @@ public function getJson()
9999
$this->eventManager->dispatch('stape_gtm_linked_product_block_map', ['transport' => $transport]);
100100

101101
if (is_array($transport->getBlockTypes())) {
102-
foreach ($transport->getBlockTypes() as $blockType => $blockName) {
102+
foreach ($transport->getBlockTypes() as $blockName => $blockType) {
103103
if (!$block = $this->layout->getBlock($blockName)) {
104104
continue;
105105
}

ViewModel/Wishlist.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
namespace Stape\Gtm\ViewModel;
4+
5+
use Magento\Framework\Serialize\Serializer\Json;
6+
use Magento\Framework\View\Element\Block\ArgumentInterface;
7+
8+
class Wishlist implements ArgumentInterface, DatalayerInterface
9+
{
10+
/**
11+
* @var Json $json
12+
*/
13+
private $json;
14+
15+
public function __construct(Json $json)
16+
{
17+
$this->json = $json;
18+
}
19+
20+
/**
21+
* Retrieve json
22+
*
23+
* @return bool|string
24+
* @throws \Magento\Framework\Exception\LocalizedException
25+
* @throws \Magento\Framework\Exception\NoSuchEntityException
26+
*/
27+
public function getJson()
28+
{
29+
return $this->json->serialize([]);
30+
}
31+
}

ViewModel/Wishlist/ExtraData.php

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
<?php
2+
3+
namespace Stape\Gtm\ViewModel\Wishlist;
4+
5+
use Magento\Catalog\Block\Product\Context;
6+
use Magento\Framework\Serialize\Serializer\Json;
7+
use Magento\Framework\View\Element\Block\ArgumentInterface;
8+
use Magento\Framework\View\Layout;
9+
use Magento\Store\Model\StoreManagerInterface;
10+
use Magento\Wishlist\Block\Customer\Wishlist;
11+
use Stape\Gtm\Model\Product\Mapper\EventItemsMapper;
12+
use Stape\Gtm\ViewModel\DatalayerInterface;
13+
14+
class ExtraData implements ArgumentInterface, DatalayerInterface
15+
{
16+
17+
/**
18+
* @var Layout $layout
19+
*/
20+
protected $layout;
21+
22+
/**
23+
* @var StoreManagerInterface $storeManager
24+
*/
25+
protected $storeManager;
26+
27+
/**
28+
* @var EventItemsMapper $mapper
29+
*/
30+
protected $mapper;
31+
32+
/**
33+
* @var Json $json
34+
*/
35+
protected $json;
36+
37+
/**
38+
* @var \Magento\Wishlist\Helper\Data $wishlistHelper
39+
*/
40+
protected $wishlistHelper;
41+
42+
/**
43+
* Define class dependencies
44+
*
45+
* @param Layout $layout
46+
*/
47+
public function __construct(
48+
Layout $layout,
49+
StoreManagerInterface $storeManager,
50+
EventItemsMapper $mapper,
51+
Json $json,
52+
Context $context
53+
) {
54+
$this->layout = $layout;
55+
$this->storeManager = $storeManager;
56+
$this->mapper = $mapper;
57+
$this->json = $json;
58+
$this->wishlistHelper = $context->getWishlistHelper();
59+
}
60+
61+
/**
62+
* Retrieve json
63+
*
64+
* @return bool|string
65+
*/
66+
public function getJson()
67+
{
68+
$wishlist = $this->wishlistHelper->getWishlist();
69+
$items = array_map(function($item) {
70+
return $item->getProduct();
71+
}, $wishlist->getItemCollection()->getItems());
72+
73+
return $this->json->serialize([
74+
'currency' => $this->storeManager->getStore()->getCurrentCurrency()->getCode(),
75+
'lists' => [
76+
[
77+
'item_list_name' => 'products',
78+
'items' => $this->mapper->toEventItems($items)
79+
]
80+
]
81+
]);
82+
}
83+
}

view/frontend/layout/catalog_category_view.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
<referenceBlock name="stape.gtm">
77
<arguments>
88
<argument name="page_type" xsi:type="string">category</argument>
9+
<argument name="product_item_selector" xsi:type="string">.product-item</argument>
910
<argument name="data_layer" xsi:type="object">Stape\Gtm\ViewModel\Category</argument>
1011
<argument name="extra_data" xsi:type="object">Stape\Gtm\ViewModel\Category\ExtraData</argument>
1112
</arguments>
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?xml version="1.0"?>
2+
3+
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
4+
<body>
5+
<referenceBlock name="stape.gtm">
6+
<arguments>
7+
<argument name="page_type" xsi:type="string">compare_products</argument>
8+
<argument name="product_item_selector" xsi:type="string">.table-wrapper tr</argument>
9+
<argument name="data_layer" xsi:type="object">Stape\Gtm\ViewModel\Compare</argument>
10+
<argument name="extra_data" xsi:type="object">Stape\Gtm\ViewModel\Compare\ExtraData</argument>
11+
</arguments>
12+
</referenceBlock>
13+
</body>
14+
</page>

view/frontend/layout/catalog_product_view.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
<referenceBlock name="stape.gtm">
77
<arguments>
88
<argument name="page_type" xsi:type="string">product</argument>
9+
<argument name="product_item_selector" xsi:type="string">.product-item</argument>
910
<argument name="data_layer" xsi:type="object">Stape\Gtm\ViewModel\Product</argument>
1011
<argument name="extra_data" xsi:type="object">Stape\Gtm\ViewModel\Product\LinkedProducts</argument>
1112
</arguments>

view/frontend/layout/catalogsearch_advanced_result.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
<referenceBlock name="stape.gtm">
66
<arguments>
77
<argument name="page_type" xsi:type="string">search</argument>
8+
<argument name="product_item_selector" xsi:type="string">.product-item</argument>
89
<argument name="data_layer" xsi:type="object">Stape\Gtm\ViewModel\Search</argument>
910
<argument name="extra_data" xsi:type="object">Stape\Gtm\ViewModel\Search\ExtraData</argument>
1011
</arguments>

0 commit comments

Comments
 (0)