Skip to content

Commit 96d0d83

Browse files
authored
Merge pull request #55 from stape-io/feature/variation-details-and-select-product-event
Feature/variation details and select product event
2 parents b99cab2 + 620efbb commit 96d0d83

38 files changed

+1068
-83
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+
}

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
11
# Changelog
2+
3+
v1.0.31
4+
- renamed module in admin panel;
5+
- added select_item event;
6+
- added item_variant param to datalayer;
7+
28
v1.0.30
39
- added module version header;
410

Model/Data/Converter.php

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,15 +53,16 @@ public function prepareOrderItems(Order $order)
5353
/** @var \Magento\Sales\Model\Order\Item $item */
5454
foreach ($order->getAllVisibleItems() as $item) {
5555
$category = $this->categoryResolver->resolve($item->getProduct());
56+
$childItem = $item->getHasChildren() ? current($item->getChildrenItems() ?? []) : null;
5657
$items[] = [
5758
'item_id' => $item->getProductId(),
5859
'item_name' => $item->getName(),
5960
'item_sku' => $item->getSku(),
6061
'item_category' => $category ? $category->getName() : '',
6162
'price' => $this->priceCurrency->round($item->getBasePrice()),
6263
'quantity' => $item->getQtyOrdered(),
63-
'variation_id' => $item->getHasChildren()
64-
? current($item->getChildrenItems() ?? [])->getProductId() : null
64+
'item_variant' => $childItem ? $childItem->getSku() : null,
65+
'variation_id' => $childItem ? $childItem->getProductId() : null,
6566
];
6667
}
6768
return $items;
@@ -83,15 +84,17 @@ public function prepareCreditMemoItems(Creditmemo $creditmemo)
8384
}
8485

8586
$category = $this->categoryResolver->resolve($orderItem->getProduct());
87+
$childItem = $item->getOrderItem()->getHasChildren()
88+
? current($item->getOrderItem()->getChildrenItems() ?? []) : null;
8689
$items[] = [
8790
'item_id' => $item->getProductId(),
8891
'item_name' => $item->getName(),
8992
'item_sku' => $item->getSku(),
9093
'item_category' => $category ? $category->getName() : '',
9194
'price' => $this->priceCurrency->round($item->getBasePrice()),
9295
'quantity' => $item->getQty(),
93-
'variation_id' => $item->getOrderItem()->getHasChildren()
94-
? current($item->getOrderItem()->getChildrenItems() ?? [])->getProductId() : null
96+
'item_variant' => $childItem ? $childItem->getSku() : null,
97+
'variation_id' => $childItem ? $childItem->getProductId() : null,
9598
];
9699
}
97100
return $items;
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\Model\Product\Mapper;
4+
5+
use Magento\Catalog\Block\Product\Context;
6+
use Magento\Catalog\Model\ResourceModel\Product\Collection;
7+
use Magento\Framework\Pricing\PriceCurrencyInterface;
8+
use Magento\Store\Model\StoreManagerInterface;
9+
use Stape\Gtm\Model\Product\CategoryResolver;
10+
11+
class EventItemsMapper
12+
{
13+
14+
/**
15+
* @var PriceCurrencyInterface $priceCurrency
16+
*/
17+
protected $priceCurrency;
18+
19+
/**
20+
* @var StoreManagerInterface $storeManager
21+
*/
22+
protected $storeManager;
23+
24+
/**
25+
* @var CategoryResolver $categoryResolver
26+
*/
27+
protected $categoryResolver;
28+
29+
/**
30+
* @var Context $context
31+
*/
32+
protected $context;
33+
34+
/**
35+
* Define class dependencies
36+
*
37+
* @param PriceCurrencyInterface $priceCurrency
38+
* @param StoreManagerInterface $storeManager
39+
* @param CategoryResolver $categoryResolver
40+
* @param Context $context
41+
*/
42+
public function __construct(
43+
PriceCurrencyInterface $priceCurrency,
44+
StoreManagerInterface $storeManager,
45+
CategoryResolver $categoryResolver,
46+
Context $context
47+
) {
48+
$this->priceCurrency = $priceCurrency;
49+
$this->storeManager = $storeManager;
50+
$this->categoryResolver = $categoryResolver;
51+
$this->context = $context;
52+
}
53+
54+
/**
55+
* Map product collection to event items collection
56+
*
57+
* @param \Magento\Catalog\Model\Product[]|\Magento\Catalog\Model\ResourceModel\Product\Collection $itemList
58+
* @return array
59+
*/
60+
public function toEventItems($itemList)
61+
{
62+
$items = [];
63+
$index = 0;
64+
$imageBuilder = $this->context->getImageBuilder();
65+
66+
/** @var \Magento\Catalog\Model\Product $product */
67+
foreach ($itemList as $product) {
68+
$category = $this->categoryResolver->resolve($product);
69+
$items[$product->getId()] = [
70+
'imageUrl' => $imageBuilder->create($product, 'category_page_grid')->getImageUrl(),
71+
'item_name' => $product->getName(),
72+
'item_id' => $product->getId(),
73+
'item_sku' => $product->getSku(),
74+
'price' => $this->priceCurrency->round($product->getFinalPrice()),
75+
'index' => $index++,
76+
'quantity' => '1',
77+
'variant_name' => $product->getName(),
78+
'item_category' => $category ? $category->getName() : null,
79+
];
80+
}
81+
return $items;
82+
}
83+
}

Model/Webhook/Adapter.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
class Adapter
1414
{
1515

16-
public const MODULE_VERSION = '1.0.30';
16+
public const MODULE_VERSION = '1.0.31';
1717

1818
/**
1919
* @var Json $json

Observer/AddToCartComplete.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ public function execute(Observer $observer)
8484
$qty = 1;
8585
}
8686
$category = $this->categoryResolver->resolve($product);
87+
$childItem = $quoteItem->getHasChildren() ? current($quoteItem->getChildren()) : null;
8788
$this->dataProvider->add('add_to_cart', [
8889
'currency' => $this->checkoutSession->getQuote()->getBaseCurrencyCode(),
8990
'items' => [
@@ -94,8 +95,8 @@ public function execute(Observer $observer)
9495
'item_category' => $category ? $category->getName() : null,
9596
'price' => $this->priceCurrency->round($quoteItem->getBasePriceInclTax()),
9697
'quantity' => $qty,
97-
'variation_id' => $quoteItem->getHasChildren()
98-
? current($quoteItem->getChildren())->getProductId() : null
98+
'variation_id' => $childItem ? $childItem->getProductId() : null,
99+
'item_variant' => $childItem ? $childItem->getSku() : null,
99100
]
100101
]
101102
]);

Plugin/CustomerData/ItemPoolPlugin.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,9 @@ public function afterGetItemData(ItemPoolInterface $subject, $result, Item $item
5252
}
5353

5454
if ($item->getHasChildren()) {
55-
$result['child_product_id'] = current($item->getChildren())->getProductId();
55+
$childItem = current($item->getChildren());
56+
$result['child_product_id'] = $childItem->getProductId();
57+
$result['child_product_sku'] = $childItem->getSku();
5658
}
5759

5860
return $result;

ViewModel/Cart.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,8 @@ public function prepareItems(\Magento\Quote\Model\Quote $quote)
8787
'item_category' => $category ? $category->getName() : null,
8888
'price' => $this->priceCurrency->round($item->getBasePrice()),
8989
'quantity' => (int) $item->getQty(),
90-
'variation_id' => $item->getHasChildren() ? current($item->getChildren())->getProductId() : null
90+
'variation_id' => $item->getHasChildren() ? current($item->getChildren())->getProductId() : null,
91+
'item_variant' => $item->getHasChildren() ? current($item->getChildren())->getSku() : null
9192
];
9293
}
9394
return $items;

ViewModel/Category.php

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -83,20 +83,6 @@ public function __construct(
8383
$this->eventFormatter = $eventFormatter;
8484
}
8585

86-
/**
87-
* Retrieve category name
88-
*
89-
* @return string
90-
*/
91-
private function getCategoryName()
92-
{
93-
if ($currentCategory = $this->layer->getCurrentCategory()) {
94-
return $currentCategory->getName();
95-
}
96-
97-
return '';
98-
}
99-
10086
/**
10187
* Retrieve collection
10288
*
@@ -129,14 +115,38 @@ private function prepareCollection()
129115
return $collection;
130116
}
131117

118+
/**
119+
* Retrieve product collection
120+
*
121+
* @return \Magento\Catalog\Model\ResourceModel\Product\Collection
122+
*/
123+
public function getProductCollection()
124+
{
125+
return $this->prepareCollection();
126+
}
127+
128+
/**
129+
* Retrieve category name
130+
*
131+
* @return string
132+
*/
133+
public function getCategoryName()
134+
{
135+
if ($currentCategory = $this->layer->getCurrentCategory()) {
136+
return $currentCategory->getName();
137+
}
138+
139+
return '';
140+
}
141+
132142
/**
133143
* Preparing items
134144
*
135145
* @return array
136146
*/
137147
public function prepareItems()
138148
{
139-
$collection = $this->prepareCollection();
149+
$collection = $this->getProductCollection();
140150
$items = [];
141151
$index = 0;
142152
/** @var \Magento\Catalog\Model\Product $product */

0 commit comments

Comments
 (0)