Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
87 changes: 87 additions & 0 deletions Config/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use Magento\Store\Model\StoreManagerInterface;
use Magento\Framework\UrlInterface;
use Tagging\GTM\DataLayer\Tag\Version;
use Tagging\GTM\Model\Config\Source\HidePriceMode;

class Config implements ArgumentInterface
{
Expand Down Expand Up @@ -155,6 +156,59 @@ public function getStoreDomain(): string
return (string)$this->storeManager->getStore()->getBaseUrl(UrlInterface::URL_TYPE_WEB);
}

/**
* Check whether prices should be hidden in the dataLayer for specific customer groups
*
* B2B stores that only reveal prices to logged in (or approved) customers use this to keep those
* prices out of the dataLayer as well.
*
* @return bool
*/
public function isHidePricesEnabled(): bool
{
return (bool)$this->getModuleConfigValuePriceVisibility('enabled', false);
}

/**
* The customer group ids that are not allowed to see prices in the dataLayer
*
* @return int[]
*/
public function getHidePricesCustomerGroups(): array
{
$customerGroups = $this->getRawConfigValue('GTM/price_visibility/customer_groups');
if (!is_string($customerGroups) || $customerGroups === '') {
return [];
}

$customerGroupIds = [];
foreach (explode(',', $customerGroups) as $customerGroupId) {
$customerGroupId = trim($customerGroupId);
if ($customerGroupId === '') {
continue;
}

$customerGroupIds[] = (int)$customerGroupId;
}

return $customerGroupIds;
}

/**
* How a hidden price should be represented in the dataLayer
*
* @return string
*/
public function getHidePricesMode(): string
{
$mode = (string)$this->getModuleConfigValuePriceVisibility('mode', HidePriceMode::MODE_REMOVE);
if ($mode !== HidePriceMode::MODE_ZERO) {
return HidePriceMode::MODE_REMOVE;
}

return $mode;
}

/**
* Return a configuration value
*
Expand All @@ -168,6 +222,19 @@ public function getModuleConfigValue(string $key, $defaultValue = null)
return $this->getConfigValue('GTM/settings/' . $key, $defaultValue);
}

/**
* Return a price visibility configuration value
*
* @param string $key
* @param null $defaultValue
*
* @return mixed|null
*/
public function getModuleConfigValuePriceVisibility(string $key, $defaultValue = null)
{
return $this->getConfigValue('GTM/price_visibility/' . $key, $defaultValue);
}

public function getModuleConfigValueAdvanced(string $key, $defaultValue = null)
{
return $this->getConfigValue('GTM/advanced/' . $key, $defaultValue);
Expand Down Expand Up @@ -200,6 +267,26 @@ public function getConfigValue(string $key, $defaultValue = null)
return $value;
}

/**
* Return a configuration value without treating "0" and other falsy values as absent
*
* @param string $key
*
* @return mixed|null
*/
public function getRawConfigValue(string $key)
{
try {
return $this->scopeConfig->getValue(
$key,
ScopeInterface::SCOPE_STORE,
$this->storeManager->getStore()
);
} catch (NoSuchEntityException $e) {
return null;
}
}

public function getVersion(): string
{
return $this->version->get();
Expand Down
22 changes: 19 additions & 3 deletions DataLayer/Event/AddPaymentInfo.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,27 +7,33 @@
use Tagging\GTM\Api\Data\EventInterface;
use Tagging\GTM\DataLayer\Tag\Cart\CartItems;
use Tagging\GTM\Util\PriceFormatter;
use Tagging\GTM\Util\PriceVisibility;

class AddPaymentInfo implements EventInterface
{
private CartItems $cartItems;
private CartRepositoryInterface $cartRepository;
private PriceFormatter $priceFormatter;
private PriceVisibility $priceVisibility;
private int $cartId;
private string $paymentMethod;

/**
* @param CartRepositoryInterface $cartRepository
* @param CartItems $cartItems
* @param PriceFormatter $priceFormatter
* @param PriceVisibility $priceVisibility
*/
public function __construct(
CartRepositoryInterface $cartRepository,
CartItems $cartItems,
PriceFormatter $priceFormatter
PriceFormatter $priceFormatter,
PriceVisibility $priceVisibility
) {
$this->cartItems = $cartItems;
$this->cartRepository = $cartRepository;
$this->priceFormatter = $priceFormatter;
$this->priceVisibility = $priceVisibility;
}

/**
Expand All @@ -37,16 +43,26 @@ public function get(): array
{
/** @var Cart $cart */
$cart = $this->cartRepository->get($this->cartId);
return [
$value = $this->priceVisibility->filter(
$this->priceFormatter->format((float)$cart->getGrandTotal())
);

$eventData = [
'event' => 'trytagging_add_payment_info',
'ecommerce' => [
'currency' => $cart->getQuoteCurrencyCode(),
'value' => $this->priceFormatter->format((float)$cart->getGrandTotal()),
'value' => $value,
'coupon' => $cart->getCouponCode(),
'payment_type' => $this->paymentMethod,
'items' => $this->cartItems->get()
]
];

if ($value === null) {
unset($eventData['ecommerce']['value']);
}

return $eventData;
}

/**
Expand Down
15 changes: 12 additions & 3 deletions DataLayer/Event/AddToCart.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,16 +56,25 @@ public function get(): array

$itemData = $this->productDataMapper->mapByProduct($product);
$itemData['quantity'] = $qty;
$value = $itemData['price'] * $qty;

return [
// No item price means the price is hidden for this visitor, so no value is reported either.
$price = $itemData['price'] ?? null;
$value = $price === null ? null : $this->priceFormatter->format((float)$price * $qty);

$eventData = [
'event' => 'trytagging_add_to_cart',
'ecommerce' => [
'currency' => $this->currencyCode->get(),
'value' => $this->priceFormatter->format((float)$value),
'value' => $value,
'items' => [$itemData]
]
];

if ($value === null) {
unset($eventData['ecommerce']['value']);
}

return $eventData;
}

/**
Expand Down
8 changes: 7 additions & 1 deletion DataLayer/Event/BeginCheckout.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public function __construct(

public function get(): array
{
return [
$eventData = [
'event' => 'trytagging_begin_checkout',
'ecommerce' => [
'currency' => $this->currencyCode->get(),
Expand All @@ -44,5 +44,11 @@ public function get(): array
'items' => $this->cartItems->get()
]
];

if ($eventData['ecommerce']['value'] === null) {
unset($eventData['ecommerce']['value']);
}

return $eventData;
}
}
8 changes: 7 additions & 1 deletion DataLayer/Event/ViewCart.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public function __construct(
*/
public function get(): array
{
return [
$eventData = [
'meta' => [
'cacheable' => true,
'allowed_pages' => $this->getAllowedPages(),
Expand All @@ -56,6 +56,12 @@ public function get(): array
'items' => $this->cartItems->get()
]
];

if ($eventData['ecommerce']['value'] === null) {
unset($eventData['ecommerce']['value']);
}

return $eventData;
}

/**
Expand Down
21 changes: 16 additions & 5 deletions DataLayer/Mapper/CartItemDataMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Magento\Store\Model\ScopeInterface;
use Magento\Tax\Model\Config;
use Tagging\GTM\Util\PriceFormatter;
use Tagging\GTM\Util\PriceVisibility;
use Tagging\GTM\Util\ProductProvider;

class CartItemDataMapper
Expand All @@ -17,23 +18,27 @@ class CartItemDataMapper
private ProductProvider $productProvider;
private PriceFormatter $priceFormatter;
private ScopeConfigInterface $scopeConfig;
private PriceVisibility $priceVisibility;

/**
* @param ProductDataMapper $productDataMapper
* @param ProductProvider $productProvider
* @param PriceFormatter $priceFormatter
* @param ScopeConfigInterface $scopeConfig
* @param PriceVisibility $priceVisibility
*/
public function __construct(
ProductDataMapper $productDataMapper,
ProductProvider $productProvider,
PriceFormatter $priceFormatter,
ScopeConfigInterface $scopeConfig
ScopeConfigInterface $scopeConfig,
PriceVisibility $priceVisibility
) {
$this->productDataMapper = $productDataMapper;
$this->productProvider = $productProvider;
$this->priceFormatter = $priceFormatter;
$this->scopeConfig = $scopeConfig;
$this->priceVisibility = $priceVisibility;
}

/**
Expand All @@ -50,20 +55,26 @@ public function mapByCartItem(CartItemInterface $cartItem): array
$cartItemData = [];
}

return array_merge($cartItemData, [
$cartItemData = array_merge($cartItemData, [
'item_sku' => $cartItem->getSku(),
'item_name' => $cartItem->getName(),
'order_item_id' => $cartItem->getItemId(),
'quantity' => (float) $cartItem->getQty(),
'price' => $this->getPrice($cartItem)
]);

if ($cartItemData['price'] === null) {
unset($cartItemData['price']);
}

return $cartItemData;
}

/**
* @param CartItemInterface $cartItem
* @return float
* @return float|null
*/
private function getPrice(CartItemInterface $cartItem): float
private function getPrice(CartItemInterface $cartItem): ?float
{
$displayType = (int)$this->scopeConfig->getValue(
Config::CONFIG_XML_PATH_PRICE_DISPLAY_TYPE,
Expand All @@ -82,6 +93,6 @@ private function getPrice(CartItemInterface $cartItem): float
break;
}

return $this->priceFormatter->format((float)$price);
return $this->priceVisibility->filter($this->priceFormatter->format((float)$price));
}
}
15 changes: 13 additions & 2 deletions DataLayer/Mapper/ProductDataMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Tagging\GTM\Config\Config;
use Tagging\GTM\Util\Attribute\GetAttributeValue;
use Tagging\GTM\Util\PriceFormatter;
use Tagging\GTM\Util\PriceVisibility;
use Tagging\GTM\Util\CategoryProvider;
use Magento\Catalog\Api\ProductRepositoryInterface;

Expand All @@ -23,6 +24,7 @@ class ProductDataMapper
private GetAttributeValue $getAttributeValue;
private CategoryProvider $categoryProvider;
private PriceFormatter $priceFormatter;
private PriceVisibility $priceVisibility;
private Configurable $configurableType;
private ProductRepositoryInterface $productRepository;

Expand All @@ -35,6 +37,7 @@ class ProductDataMapper
* @param GetAttributeValue $getAttributeValue
* @param CategoryProvider $categoryProvider
* @param PriceFormatter $priceFormatter
* @param PriceVisibility $priceVisibility
* @param array $dataLayerMapping
*/
public function __construct(
Expand All @@ -44,12 +47,14 @@ public function __construct(
PriceFormatter $priceFormatter,
Configurable $configurableType,
ProductRepositoryInterface $productRepository,
PriceVisibility $priceVisibility,
array $dataLayerMapping = []
) {
$this->config = $config;
$this->getAttributeValue = $getAttributeValue;
$this->categoryProvider = $categoryProvider;
$this->priceFormatter = $priceFormatter;
$this->priceVisibility = $priceVisibility;
$this->configurableType = $configurableType;
$this->productRepository = $productRepository;
$this->dataLayerMapping = $dataLayerMapping;
Expand Down Expand Up @@ -102,10 +107,16 @@ public function mapByProduct(ProductInterface $product): array
// Use the price info final price instead of getFinalPrice(): for grouped products the
// parent product itself has no price (getFinalPrice() returns 0) and Magento resolves the
// final price to the lowest priced associated product, which is what is shown on the front-end.
$productData['price'] = $this->priceFormatter->format(
(float)$product->getPriceInfo()->getPrice(FinalPrice::PRICE_CODE)->getValue()
$price = $this->priceVisibility->filter(
$this->priceFormatter->format(
(float)$product->getPriceInfo()->getPrice(FinalPrice::PRICE_CODE)->getValue()
)
);

if ($price !== null) {
$productData['price'] = $price;
}

$productData = $this->attachCategoriesData($product, $productData);
$productData = $this->parseDataLayerMapping($product, $productData);
$productData['index'] = $this->counter++;
Expand Down
Loading
Loading