Skip to content

Commit

Permalink
Merge pull request #188 from stefandoorn/price-variant-helper
Browse files Browse the repository at this point in the history
Introduce price variant helper for easier customisation in shops
  • Loading branch information
stefandoorn authored Jun 15, 2023
2 parents bb6893e + 60a67f3 commit 7b8253c
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 32 deletions.
33 changes: 33 additions & 0 deletions src/Helper/ProductVariantPriceHelper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

declare(strict_types=1);

namespace StefanDoorn\SyliusGtmEnhancedEcommercePlugin\Helper;

use Sylius\Component\Channel\Context\ChannelContextInterface;
use Sylius\Component\Core\Calculator\ProductVariantPricesCalculatorInterface;
use Sylius\Component\Core\Model\ProductVariantInterface;

final class ProductVariantPriceHelper
{
private ProductVariantPricesCalculatorInterface $productVariantPricesCalculator;

private ChannelContextInterface $channelContext;

public function __construct(
ProductVariantPricesCalculatorInterface $productVariantPricesCalculator,
ChannelContextInterface $channelContext
) {
$this->productVariantPricesCalculator = $productVariantPricesCalculator;
$this->channelContext = $channelContext;
}

public function getProductVariantPrice(
ProductVariantInterface $productVariant
): int {
return $this->productVariantPricesCalculator->calculate(
$productVariant,
['channel' => $this->channelContext->getChannel()],
);
}
}
10 changes: 8 additions & 2 deletions src/Resources/config/services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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:
Expand Down Expand Up @@ -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"
24 changes: 8 additions & 16 deletions src/TagManager/ViewItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -26,26 +26,26 @@ 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;
$this->channelContext = $channelContext;
$this->currencyContext = $currencyContext;
$this->productIdentifierHelper = $productIdentifierHelper;
$this->productVariantResolver = $productVariantResolver;
$this->productVariantPriceCalculator = $productVariantPriceCalculator;
$this->googleImplementationEnabled = $googleImplementationEnabled;
$this->productVariantPriceHelper = $productVariantPriceHelper;
}

public function add(ProductInterface $product): void
Expand Down Expand Up @@ -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
Expand Down
20 changes: 6 additions & 14 deletions src/TagManager/ViewItemList.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -30,18 +30,18 @@ final class ViewItemList implements ViewItemListInterface

private ProductVariantResolverInterface $productVariantResolver;

private ProductVariantPriceCalculatorInterface $productVariantPriceCalculator;

private GoogleImplementationEnabled $googleImplementationEnabled;

private ProductVariantPriceHelper $productVariantPriceHelper;

public function __construct(
GoogleTagManagerInterface $googleTagManager,
ProductRepositoryInterface $productRepository,
ChannelContextInterface $channelContext,
LocaleContextInterface $localeContext,
ProductIdentifierHelper $productIdentifierHelper,
ProductVariantResolverInterface $productVariantResolver,
ProductVariantPriceCalculatorInterface $productVariantPriceCalculator,
ProductVariantPriceHelper $productVariantPriceHelper,
GoogleImplementationEnabled $googleImplementationEnabled
) {
$this->googleTagManager = $googleTagManager;
Expand All @@ -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
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit 7b8253c

Please sign in to comment.