Skip to content

Commit

Permalink
Merge pull request #16 from stape-io/feature/hyva-support
Browse files Browse the repository at this point in the history
Implemented Hyva compatibility
  • Loading branch information
chaikovskyia authored Aug 16, 2024
2 parents 53ba548 + 349b53b commit 7f0c1d2
Show file tree
Hide file tree
Showing 16 changed files with 314 additions and 8 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
# Changelog
v1.0.17
- Hyva theme compatibility added.

v1.0.16
- acl.xml added.
Expand Down
10 changes: 10 additions & 0 deletions Model/Data/DataProviderInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace Stape\Gtm\Model\Data;

interface DataProviderInterface
{
public function get();
public function add($eventName, $data);
public function clear();
}
53 changes: 53 additions & 0 deletions Model/Data/SessionDataProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

namespace Stape\Gtm\Model\Data;

class SessionDataProvider implements DataProviderInterface
{

private $checkoutSession;

/**
* Define class dependencies
*
* @param \Magento\Checkout\Model\Session $checkoutSession
*/
public function __construct(\Magento\Checkout\Model\Session $checkoutSession)
{
$this->checkoutSession = $checkoutSession;
}

/**
* Retrieve Stape GTM events data
*
* @return array
*/
public function get()
{
return $this->checkoutSession->getStapeGtmEvents() ?? [];
}

/**
* Add gtm events data
*
* @param string $eventName
* @param array $data
* @return void
*/
public function add($eventName, $data)
{
$gtmEvents = $this->get();
$gtmEvents[$eventName] = $data;
$this->checkoutSession->setStapeGtmEvents($gtmEvents);
}

/**
* Clear gtm events
*
* @return void
*/
public function clear()
{
$this->checkoutSession->setStapeGtmEvents([]);
}
}
102 changes: 102 additions & 0 deletions Observer/AddToCartComplete.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
<?php

namespace Stape\Gtm\Observer;

use Magento\Checkout\Model\Session;
use Magento\Framework\Event\Observer;
use Magento\Framework\Event\ObserverInterface;
use Magento\Framework\Pricing\PriceCurrencyInterface;
use Stape\Gtm\Model\ConfigProvider;
use Stape\Gtm\Model\Data\DataProviderInterface;
use Stape\Gtm\Model\Product\CategoryResolver;

class AddToCartComplete implements ObserverInterface
{

/**
* @var CategoryResolver $categoryResolver
*/
private $categoryResolver;

/**
* @var Session $checkoutSession
*/
private $checkoutSession;

/**
* @var ConfigProvider $configProvider
*/
private $configProvider;

/**
* @var DataProviderInterface $dataProvider
*/
private $dataProvider;

/**
* @var PriceCurrencyInterface $priceCurrency
*/
private $priceCurrency;

/**
* Define class dependencies
*
* @param CategoryResolver $categoryResolver
* @param Session $checkoutSession
* @param ConfigProvider $configProvider
* @param DataProviderInterface $dataProvider
* @param PriceCurrencyInterface $priceCurrency
*/
public function __construct(
CategoryResolver $categoryResolver,
Session $checkoutSession,
ConfigProvider $configProvider,
DataProviderInterface $dataProvider,
PriceCurrencyInterface $priceCurrency
) {
$this->categoryResolver = $categoryResolver;
$this->checkoutSession = $checkoutSession;
$this->configProvider = $configProvider;
$this->dataProvider = $dataProvider;
$this->priceCurrency = $priceCurrency;
}

/**
* Execute observer logic
*
* @param Observer $observer
* @return void
*/
public function execute(Observer $observer)
{
if (!$this->configProvider->isActive()) {
return;
}

/** @var \Magento\Catalog\Model\Product $product */
$product = $observer->getData('product');

/** @var \Magento\Quote\Model\Quote\Item $quote */
$quoteItem = $this->checkoutSession->getQuote()->getItemByProduct($product);

$qty = (int)$observer->getData('request')->getParam('qty');
if ($qty === 0) {
$qty = 1;
}
$category = $this->categoryResolver->resolve($product);
$this->dataProvider->add('add_to_cart_stape', [
'currency' => $this->checkoutSession->getQuote()->getBaseCurrencyCode(),
'items' => [
[
'item_name' => $product->getName(),
'item_id' => $product->getId(),
'item_sku' => $product->getSku(),
'item_category' => $category ? $category->getName() : null,
'price' => $this->priceCurrency->round($quoteItem->getBasePriceInclTax()),
'quantity' => $qty,
'variation_id' => $quoteItem->getHasChildren() ? current($quoteItem->getChildren())->getProductId() : null
]
]
]);
}
}
40 changes: 40 additions & 0 deletions Plugin/CustomerData/CartPlugin.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

namespace Stape\Gtm\Plugin\CustomerData;

use Stape\Gtm\Model\Data\DataProviderInterface;

class CartPlugin
{
/**
* @var DataProviderInterface
*/
private $dataProvider;

/**
* @param DataProviderInterface $dataProvider
*/
public function __construct(DataProviderInterface $dataProvider)
{
$this->dataProvider = $dataProvider;
}

/**
* Add stape_gtm_events data to the cart section data
*
* @param $subject
* @param $result
* @return mixed
*/
public function afterGetSectionData($subject, $result)
{
$eventsData = $this->dataProvider->get();
$this->dataProvider->clear();

if (!empty($eventsData)) {
$result['stape_gtm_events'] = $eventsData;
}

return $result;
}
}
4 changes: 2 additions & 2 deletions ViewModel/Cart.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
use Magento\Store\Model\StoreManagerInterface;
use Stape\Gtm\Model\Product\CategoryResolver;

class Cart implements ArgumentInterface
class Cart implements ArgumentInterface, DatalayerInterface
{
/**
* @var Json $json
Expand Down Expand Up @@ -77,7 +77,7 @@ public function prepareItems(\Magento\Quote\Model\Quote $quote)
'item_sku' => $item->getSku(),
'item_category' => $category ? $category->getName() : null,
'price' => $this->priceCurrency->round($item->getBasePrice()),
'quantity' => (int) $item->getQtyOrdered(),
'quantity' => (int) $item->getQty(),
'variation_id' => $item->getHasChildren() ? current($item->getChildren())->getProductId() : null
];
}
Expand Down
3 changes: 1 addition & 2 deletions ViewModel/Category.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
use Magento\Framework\View\Layout;
use Magento\Store\Model\StoreManagerInterface;

class Category implements ArgumentInterface
class Category implements ArgumentInterface, DatalayerInterface
{
/**
* @var Json $json
Expand Down Expand Up @@ -77,7 +77,6 @@ private function getCategoryName()
*/
public function prepareItems()
{

/** @var \Magento\Catalog\Block\Product\ListProduct $productList */
$productList = $this->layout->createBlock(\Magento\Catalog\Block\Product\ListProduct::class);
$collection = $productList->getLoadedProductCollection();
Expand Down
2 changes: 1 addition & 1 deletion ViewModel/Checkout.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public function prepareItems(\Magento\Quote\Model\Quote $quote)
'item_id' => $item->getProductId(),
'item_sku' => $item->getSku(),
'item_category' => $category ? $category->getName() : null,
'price' => $this->priceCurrency->round($item->getBasePrice()),
'price' => $this->priceCurrency->round($item->getBasePriceInclTax()),
'quantity' => (int) $item->getQty(),
'variation_id' => (int) $item->getHasChildren() ? current($item->getChildren())->getProductId() : null,
];
Expand Down
11 changes: 11 additions & 0 deletions ViewModel/DatalayerInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace Stape\Gtm\ViewModel;

interface DatalayerInterface
{
/**
* @return string
*/
public function getJson();
}
2 changes: 1 addition & 1 deletion ViewModel/Product.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
use Magento\Store\Model\StoreManagerInterface;
use Stape\Gtm\Model\Product\CategoryResolver;

class Product implements ArgumentInterface
class Product implements ArgumentInterface, DatalayerInterface
{
/**
* @var Json $json
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"license": [
"GPL-3.0-only"
],
"version": "1.0.16",
"version": "1.0.17",
"require": {
"php": ">=7.4.0"
},
Expand Down
4 changes: 4 additions & 0 deletions etc/frontend/di.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
<preference type="Stape\Gtm\Model\Data\SessionDataProvider" for="Stape\Gtm\Model\Data\DataProviderInterface"/>
<type name="Magento\Framework\App\Response\Http">
<plugin name="stape_gtm_server_side_cookie" type="Stape\Gtm\Plugin\HttpPlugin" sortOrder="1"/>
</type>
Expand All @@ -12,4 +13,7 @@
<type name="Magento\Csp\Observer\Render">
<plugin name="stape_gtm_csp" type="Stape\Gtm\Plugin\CspObserverPlugin" />
</type>
<type name="Magento\Checkout\CustomerData\Cart">
<plugin name="stape_gtm_customer_cart" type="Stape\Gtm\Plugin\CustomerData\CartPlugin" />
</type>
</config>
7 changes: 7 additions & 0 deletions etc/frontend/events.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?xml version="1.0"?>

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
<event name="checkout_cart_add_product_complete">
<observer name="stape_gtm_checkout_cart_add_product_complete" instance="Stape\Gtm\Observer\AddToCartComplete" />
</event>
</config>
2 changes: 1 addition & 1 deletion etc/module.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0"?>

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/Module/etc/module.xsd">
<module name="Stape_Gtm" schema_version="1.0.15" setup_version="1.0.15">
<module name="Stape_Gtm" schema_version="1.0.17" setup_version="1.0.17">
<sequence>
<module name="Magento_Backend" />
<module name="Magento_Catalog" />
Expand Down
8 changes: 8 additions & 0 deletions view/frontend/layout/hyva_default.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0"?>

<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<body>
<referenceBlock name="stape.gtm" template="Stape_Gtm::hyva/html/gtm.phtml" />
</body>
</page>
Loading

0 comments on commit 7f0c1d2

Please sign in to comment.