diff --git a/src/Helper/ProductVariantPriceHelper.php b/src/Helper/ProductVariantPriceHelper.php new file mode 100644 index 0000000..2a4b90c --- /dev/null +++ b/src/Helper/ProductVariantPriceHelper.php @@ -0,0 +1,33 @@ +productVariantPricesCalculator = $productVariantPricesCalculator; + $this->channelContext = $channelContext; + } + + public function getProductVariantPrice( + ProductVariantInterface $productVariant + ): int { + return $this->productVariantPricesCalculator->calculate( + $productVariant, + ['channel' => $this->channelContext->getChannel()], + ); + } +} diff --git a/src/Resources/config/services.yml b/src/Resources/config/services.yml index 70293b6..799b9aa 100644 --- a/src/Resources/config/services.yml +++ b/src/Resources/config/services.yml @@ -27,7 +27,7 @@ services: - "@sylius.context.currency" - "@sylius.google_tag_manager.enhanced_ecommerce_tracking.helper.product_identifier" - "@sylius.product_variant_resolver.default" - - "@sylius.calculator.product_variant_price" + - "@sylius.google_tag_manager.enhanced_ecommerce_tracking.helper.product_variant_price" - "@sylius.google_tag_manager.enhanced_ecommerce.tracking.google_implementation_enabled" sylius.google_tag_manager.enhanced_ecommerce_tracking.tag_manager.view_item_list: @@ -39,7 +39,7 @@ services: - "@sylius.context.locale" - "@sylius.google_tag_manager.enhanced_ecommerce_tracking.helper.product_identifier" - "@sylius.product_variant_resolver.default" - - "@sylius.calculator.product_variant_price" + - "@sylius.google_tag_manager.enhanced_ecommerce_tracking.helper.product_variant_price" - "@sylius.google_tag_manager.enhanced_ecommerce.tracking.google_implementation_enabled" sylius.google_tag_manager.enhanced_ecommerce_tracking.tag_manager.checkout_step: @@ -114,3 +114,9 @@ services: class: StefanDoorn\SyliusGtmEnhancedEcommercePlugin\Helper\ProductIdentifierHelper arguments: - "%sylius_gtm_enhanced_ecommerce.product_identifier%" + + sylius.google_tag_manager.enhanced_ecommerce_tracking.helper.product_variant_price: + class: StefanDoorn\SyliusGtmEnhancedEcommercePlugin\Helper\ProductVariantPriceHelper + arguments: + - "@sylius.calculator.product_variant_price" + - "@sylius.context.channel" diff --git a/src/TagManager/ViewItem.php b/src/TagManager/ViewItem.php index d8bb9e1..d18e8b7 100644 --- a/src/TagManager/ViewItem.php +++ b/src/TagManager/ViewItem.php @@ -6,8 +6,8 @@ use StefanDoorn\SyliusGtmEnhancedEcommercePlugin\Helper\GoogleImplementationEnabled; use StefanDoorn\SyliusGtmEnhancedEcommercePlugin\Helper\ProductIdentifierHelper; +use StefanDoorn\SyliusGtmEnhancedEcommercePlugin\Helper\ProductVariantPriceHelper; use Sylius\Component\Channel\Context\ChannelContextInterface; -use Sylius\Component\Core\Calculator\ProductVariantPriceCalculatorInterface; use Sylius\Component\Core\Model\ProductInterface; use Sylius\Component\Core\Model\TaxonInterface; use Sylius\Component\Currency\Context\CurrencyContextInterface; @@ -26,17 +26,17 @@ final class ViewItem implements ViewItemInterface private ProductVariantResolverInterface $productVariantResolver; - private ProductVariantPriceCalculatorInterface $productVariantPriceCalculator; - private GoogleImplementationEnabled $googleImplementationEnabled; + private ProductVariantPriceHelper $productVariantPriceHelper; + public function __construct( GoogleTagManagerInterface $googleTagManager, ChannelContextInterface $channelContext, CurrencyContextInterface $currencyContext, ProductIdentifierHelper $productIdentifierHelper, ProductVariantResolverInterface $productVariantResolver, - ProductVariantPriceCalculatorInterface $productVariantPriceCalculator, + ProductVariantPriceHelper $productVariantPriceHelper, GoogleImplementationEnabled $googleImplementationEnabled ) { $this->googleTagManager = $googleTagManager; @@ -44,8 +44,8 @@ public function __construct( $this->currencyContext = $currencyContext; $this->productIdentifierHelper = $productIdentifierHelper; $this->productVariantResolver = $productVariantResolver; - $this->productVariantPriceCalculator = $productVariantPriceCalculator; $this->googleImplementationEnabled = $googleImplementationEnabled; + $this->productVariantPriceHelper = $productVariantPriceHelper; } public function add(ProductInterface $product): void @@ -73,19 +73,11 @@ private function addViewItemData(ProductInterface $product): void ]; $productVariant = $this->productVariantResolver->getVariant($product); - if (null !== $productVariant) { - $productVariantPrice = $this->productVariantPriceCalculator->calculate( - $productVariant, - [ - 'channel' => $this->channelContext->getChannel(), - ] - ); - - $data['items'][0]['price'] = $productVariantPrice / 100; - + $data['value'] = $this->productVariantPriceHelper->getProductVariantPrice($productVariant) / 100; $data['currency'] = $this->currencyContext->getCurrencyCode(); - $data['value'] = $productVariantPrice / 100; + + $data['items'][0]['price'] = $data['value']; } // https://developers.google.com/analytics/devguides/collection/ga4/ecommerce?client_type=gtm#view_item_details diff --git a/src/TagManager/ViewItemList.php b/src/TagManager/ViewItemList.php index 4be8cbb..fda6727 100644 --- a/src/TagManager/ViewItemList.php +++ b/src/TagManager/ViewItemList.php @@ -7,8 +7,8 @@ use Doctrine\Common\Collections\ArrayCollection; use StefanDoorn\SyliusGtmEnhancedEcommercePlugin\Helper\GoogleImplementationEnabled; use StefanDoorn\SyliusGtmEnhancedEcommercePlugin\Helper\ProductIdentifierHelper; +use StefanDoorn\SyliusGtmEnhancedEcommercePlugin\Helper\ProductVariantPriceHelper; use Sylius\Component\Channel\Context\ChannelContextInterface; -use Sylius\Component\Core\Calculator\ProductVariantPriceCalculatorInterface; use Sylius\Component\Core\Model\ProductInterface; use Sylius\Component\Core\Model\TaxonInterface; use Sylius\Component\Core\Repository\ProductRepositoryInterface; @@ -30,10 +30,10 @@ final class ViewItemList implements ViewItemListInterface private ProductVariantResolverInterface $productVariantResolver; - private ProductVariantPriceCalculatorInterface $productVariantPriceCalculator; - private GoogleImplementationEnabled $googleImplementationEnabled; + private ProductVariantPriceHelper $productVariantPriceHelper; + public function __construct( GoogleTagManagerInterface $googleTagManager, ProductRepositoryInterface $productRepository, @@ -41,7 +41,7 @@ public function __construct( LocaleContextInterface $localeContext, ProductIdentifierHelper $productIdentifierHelper, ProductVariantResolverInterface $productVariantResolver, - ProductVariantPriceCalculatorInterface $productVariantPriceCalculator, + ProductVariantPriceHelper $productVariantPriceHelper, GoogleImplementationEnabled $googleImplementationEnabled ) { $this->googleTagManager = $googleTagManager; @@ -50,8 +50,8 @@ public function __construct( $this->localeContext = $localeContext; $this->productIdentifierHelper = $productIdentifierHelper; $this->productVariantResolver = $productVariantResolver; - $this->productVariantPriceCalculator = $productVariantPriceCalculator; $this->googleImplementationEnabled = $googleImplementationEnabled; + $this->productVariantPriceHelper = $productVariantPriceHelper; } public function add(TaxonInterface $taxon, ?string $listId = null): void @@ -95,16 +95,8 @@ private function addViewItemListData(TaxonInterface $taxon, ?string $listId = nu ]; $productVariant = $this->productVariantResolver->getVariant($product); - if (null !== $productVariant) { - $productVariantPrice = $this->productVariantPriceCalculator->calculate( - $productVariant, - [ - 'channel' => $this->channelContext->getChannel(), - ] - ); - - $productData['price'] = $productVariantPrice / 100; + $productData['price'] = $this->productVariantPriceHelper->getProductVariantPrice($productVariant) / 100; } return $productData;