Skip to content

Commit

Permalink
Only add viewItem price data if variant is found/enabled
Browse files Browse the repository at this point in the history
only add viewItem data if variant is found/enabled

Input default view_item data, only include price if variant is available
  • Loading branch information
dsbe-ak authored and stefandoorn committed Jun 15, 2023
1 parent cd95aeb commit cd58087
Showing 1 changed file with 16 additions and 11 deletions.
27 changes: 16 additions & 11 deletions src/TagManager/ViewItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,27 +62,32 @@ private function addViewItemData(ProductInterface $product): void
/** @var TaxonInterface|null $mainTaxon */
$mainTaxon = $product->getMainTaxon();

$productVariant = $this->productVariantResolver->getVariant($product);
$productVariantPrice = $this->productVariantPriceCalculator->calculate(
$productVariant,
[
'channel' => $this->channelContext->getChannel(),
]
);

$data = [
'items' => [
'item_id' => $this->productIdentifierHelper->getProductIdentifier($product),
'item_name' => $product->getName(),
'affiliation' => $this->channelContext->getChannel()->getName(),
'item_category' => null !== $mainTaxon ? $mainTaxon->getName() : '',
'price' => $productVariantPrice / 100,
'index' => 0,
],
'currency' => $this->currencyContext->getCurrencyCode(),
'value' => $productVariantPrice / 100,
];

$productVariant = $this->productVariantResolver->getVariant($product);

if ($productVariant) {
$productVariantPrice = $this->productVariantPriceCalculator->calculate(
$productVariant,
[
'channel' => $this->channelContext->getChannel(),
]
);

$data['items'][0]['price'] = $productVariantPrice / 100;

$data['currency'] = $this->currencyContext->getCurrencyCode();
$data['value'] = $productVariantPrice / 100;
}

// https://developers.google.com/analytics/devguides/collection/ga4/ecommerce?client_type=gtm#view_item_details
$this->googleTagManager->addPush([
'ecommerce' => null,
Expand Down

0 comments on commit cd58087

Please sign in to comment.