Skip to content

Commit

Permalink
Fix inconsistent price/value formatting #187
Browse files Browse the repository at this point in the history
  • Loading branch information
jissereitsma committed Sep 27, 2023
1 parent 49ae3f2 commit 3f4bed9
Show file tree
Hide file tree
Showing 8 changed files with 46 additions and 22 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Fixed
- Fix login event fired on every pages Hyva #193
- Fix inconsistent price/value formatting #187

## [3.6.2] - 27 September 2023
### Fixed
Expand Down
8 changes: 6 additions & 2 deletions DataLayer/Event/AddPaymentInfo.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@
use Magento\Quote\Model\Quote as Cart;
use Yireo\GoogleTagManager2\Api\Data\EventInterface;
use Yireo\GoogleTagManager2\DataLayer\Tag\Cart\CartItems;
use Yireo\GoogleTagManager2\Util\PriceFormatter;

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

Expand All @@ -20,10 +22,12 @@ class AddPaymentInfo implements EventInterface
*/
public function __construct(
CartRepositoryInterface $cartRepository,
CartItems $cartItems
CartItems $cartItems,
PriceFormatter $priceFormatter
) {
$this->cartItems = $cartItems;
$this->cartRepository = $cartRepository;
$this->priceFormatter = $priceFormatter;
}

/**
Expand All @@ -37,7 +41,7 @@ public function get(): array
'event' => 'add_payment_info',
'ecommerce' => [
'currency' => $cart->getQuoteCurrencyCode(),
'value' => $cart->getGrandTotal(),
'value' => $this->priceFormatter->format((float)$cart->getGrandTotal()),
'coupon' => $cart->getCouponCode(),
'payment_type' => $this->paymentMethod,
'items' => $this->cartItems->get()
Expand Down
10 changes: 7 additions & 3 deletions DataLayer/Event/AddToCart.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,28 @@
use Yireo\GoogleTagManager2\Api\Data\EventInterface;
use Yireo\GoogleTagManager2\DataLayer\Mapper\ProductDataMapper;
use Yireo\GoogleTagManager2\DataLayer\Tag\CurrencyCode;
use Yireo\GoogleTagManager2\Util\PriceFormatter;

class AddToCart implements EventInterface
{
private ProductDataMapper $productDataMapper;
private CurrencyCode $currencyCode;
private PriceFormatter $priceFormatter;
private Product $product;
private int $qty = 1;
private CurrencyCode $currencyCode;

/**
* @param ProductDataMapper $productDataMapper
* @param CurrencyCode $currencyCode
*/
public function __construct(
ProductDataMapper $productDataMapper,
CurrencyCode $currencyCode
CurrencyCode $currencyCode,
PriceFormatter $priceFormatter
) {
$this->productDataMapper = $productDataMapper;
$this->currencyCode = $currencyCode;
$this->priceFormatter = $priceFormatter;
}

/**
Expand All @@ -45,7 +49,7 @@ public function get(): array
'event' => 'add_to_cart',
'ecommerce' => [
'currency' => $this->currencyCode->get(),
'value' => $value,
'value' => $this->priceFormatter->format((float)$value),
'items' => [$itemData]
]
];
Expand Down
12 changes: 8 additions & 4 deletions DataLayer/Event/Purchase.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,23 @@
use Yireo\GoogleTagManager2\Api\Data\EventInterface;
use Yireo\GoogleTagManager2\Config\Config;
use Yireo\GoogleTagManager2\DataLayer\Tag\Order\OrderItems;
use Yireo\GoogleTagManager2\Util\PriceFormatter;

class Purchase implements EventInterface
{
private ?OrderInterface $order = null;
private OrderItems $orderItems;
private Config $config;
private PriceFormatter $priceFormatter;

public function __construct(
OrderItems $orderItems,
Config $config
Config $config,
PriceFormatter $priceFormatter
) {
$this->orderItems = $orderItems;
$this->config = $config;
$this->priceFormatter = $priceFormatter;
}

/**
Expand All @@ -33,9 +37,9 @@ public function get(): array
'transaction_id' => $order->getIncrementId(),
'affiliation' => $this->config->getStoreName(),
'currency' => $order->getOrderCurrencyCode(),
'value' => (float)$order->getGrandTotal(),
'tax' => (float)$order->getTaxAmount(),
'shipping' => (float)$order->getShippingAmount(),
'value' => $this->priceFormatter->format((float)$order->getGrandTotal()),
'tax' => $this->priceFormatter->format((float)$order->getTaxAmount()),
'shipping' => $this->priceFormatter->format((float)$order->getShippingAmount()),
'coupon' => $order->getCouponCode(),
'items' => $this->orderItems->setOrder($order)->get()
]
Expand Down
12 changes: 8 additions & 4 deletions DataLayer/Event/Refund.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,24 @@
use Yireo\GoogleTagManager2\Api\Data\EventInterface;
use Yireo\GoogleTagManager2\Config\Config;
use Yireo\GoogleTagManager2\DataLayer\Tag\Order\OrderItems;
use Yireo\GoogleTagManager2\Util\PriceFormatter;

// @todo: Implement this event
class Refund implements EventInterface
{
private ?OrderInterface $order = null;
private OrderItems $orderItems;
private Config $config;
private PriceFormatter $priceFormatter;

public function __construct(
OrderItems $orderItems,
Config $config
Config $config,
PriceFormatter $priceFormatter
) {
$this->orderItems = $orderItems;
$this->config = $config;
$this->priceFormatter = $priceFormatter;
}

/**
Expand All @@ -34,9 +38,9 @@ public function get(): array
'transaction_id' => $order->getIncrementId(),
'affiliation' => $this->config->getStoreName(),
'currency' => $order->getOrderCurrencyCode(),
'value' => $order->getGrandTotal(),
'tax' => $order->getTaxAmount(),
'shipping' => $order->getShippingAmount(),
'value' => $this->priceFormatter->format((float)$order->getGrandTotal()),
'tax' => $this->priceFormatter->format((float)$order->getTaxAmount()),
'shipping' => $this->priceFormatter->format((float)$order->getShippingAmount()),
'coupon' => $order->getCouponCode(),
'items' => $this->orderItems->setOrder($order)->get()
]
Expand Down
2 changes: 1 addition & 1 deletion DataLayer/Mapper/OrderDataMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public function mapByOrder(OrderInterface $order): array
*/
private function getValueFromOrder(OrderInterface $order): float
{
return (float)$order->getGrandTotal();
return $this->priceFormatter->format((float)$order->getGrandTotal());
}

/**
Expand Down
11 changes: 7 additions & 4 deletions DataLayer/Tag/Cart/CartValue.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,30 @@
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Exception\NoSuchEntityException;
use Yireo\GoogleTagManager2\Api\Data\TagInterface;
use Yireo\GoogleTagManager2\Util\PriceFormatter;

class CartValue implements TagInterface
{
private CartModel $cartModel;
private PriceFormatter $priceFormatter;

/**
* @param CartModel $cartModel
* @throws NoSuchEntityException
* @throws LocalizedException
* @param PriceFormatter $priceFormatter
*/
public function __construct(
CartModel $cartModel
CartModel $cartModel,
PriceFormatter $priceFormatter
) {
$this->cartModel = $cartModel;
$this->priceFormatter = $priceFormatter;
}

/**
* @return string
*/
public function get(): string
{
return number_format((float)$this->cartModel->getQuote()->getBaseGrandTotal(), 4);
return (string) $this->priceFormatter->format((float)$this->cartModel->getQuote()->getBaseGrandTotal());
}
}
12 changes: 8 additions & 4 deletions DataLayer/Tag/Order/Order.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,14 @@
use Magento\Sales\Api\OrderRepositoryInterface;
use Yireo\GoogleTagManager2\Api\Data\MergeTagInterface;
use Yireo\GoogleTagManager2\Config\Config;
use Yireo\GoogleTagManager2\Util\PriceFormatter;

class Order implements MergeTagInterface
{
private CheckoutSession $checkoutSession;
private OrderRepositoryInterface $orderRepository;
private Config $config;
private PriceFormatter $priceFormatter;

/**
* @param CheckoutSession $checkoutSession
Expand All @@ -22,11 +24,13 @@ class Order implements MergeTagInterface
public function __construct(
CheckoutSession $checkoutSession,
OrderRepositoryInterface $orderRepository,
Config $config
Config $config,
PriceFormatter $priceFormatter
) {
$this->checkoutSession = $checkoutSession;
$this->orderRepository = $orderRepository;
$this->config = $config;
$this->priceFormatter = $priceFormatter;
}

/**
Expand All @@ -37,9 +41,9 @@ public function merge(): array
$order = $this->getOrder();
return [
'currency' => (string)$order->getOrderCurrencyCode(),
'value' => (float)$order->getGrandTotal(),
'tax' => (float)$order->getTaxAmount(),
'shipping' => (float)$order->getShippingAmount(),
'value' => $this->priceFormatter->format((float)$order->getGrandTotal()),
'tax' => $this->priceFormatter->format((float)$order->getTaxAmount()),
'shipping' => $this->priceFormatter->format((float)$order->getShippingAmount()),
'affiliation' => $this->config->getStoreName(),
'transaction_id' => $order->getIncrementId(),
'coupon' => $order->getCouponCode()
Expand Down

0 comments on commit 3f4bed9

Please sign in to comment.