Skip to content

Commit 63392e4

Browse files
committed
Improvements for 10727-Do-not-include-shipping-or-tax
1 parent 8470693 commit 63392e4

File tree

9 files changed

+168
-137
lines changed

9 files changed

+168
-137
lines changed

Model/AbstractDataLayer.php

Lines changed: 5 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ protected function formatPrice(float $price): float
220220

221221
/**
222222
* Get product price
223-
*
223+
* @deprecated
224224
* @param Product $product
225225
* @return float
226226
*/
@@ -237,59 +237,16 @@ protected function getPrice(Product $product): float
237237
*/
238238
protected function getProductValue($product): float
239239
{
240-
$value = $this->getPrice($product);
241-
240+
$priceInfo = $product->getPriceInfo()->getPrice('final_price')->getAmount();
242241
if (!$this->config->isPurchaseTaxEnabled()) {
243-
$value = $product->getPriceInfo()->getPrice('final_price')->getAmount()->getValue('tax');
242+
$value = $priceInfo->getValue('tax');
243+
} else {
244+
$value = $priceInfo->getValue();
244245
}
245246

246247
return $this->formatPrice($value);
247248
}
248249

249-
/**
250-
* @param $quote
251-
* @return float
252-
*/
253-
protected function getQuoteValue($quote): float
254-
{
255-
$quoteValue = (float)$quote->getGrandTotal();
256-
$address = $quote->getShippingAddress() ?: $quote->getBillingAddress();
257-
258-
if (!$this->config->isPurchaseTaxEnabled()) {
259-
$quoteValue -= (float)$address->getTaxAmount();
260-
}
261-
262-
if (!$this->config->isPurchaseShippingEnabled()) {
263-
$quoteValue -= (float)$address->getShippingAmount();
264-
}
265-
266-
return $this->formatPrice($quoteValue);
267-
}
268-
269-
/**
270-
* @param $quoteItem
271-
* @return float
272-
*/
273-
protected function getQuoteItemValue($quoteItem): float
274-
{
275-
//fix for magento 2.3.2 - module-quote/Model/Quote/Item/Processor.php prepareItem does not set price to quote item
276-
$quoteItemValue = $quoteItem->getPriceInclTax();
277-
if (!$quoteItemValue && ($quoteItemProduct = $quoteItem->getProduct())) {
278-
$quoteItemValue = (float)$quoteItemProduct->getPrice();
279-
}
280-
281-
if (!$this->config->isPurchaseTaxEnabled()) {
282-
$quoteItemValue -= (float)$quoteItem->getTaxAmount();
283-
}
284-
285-
if (!$this->config->isPurchaseTaxEnabled()) {
286-
$quoteItemValue -= (float)$quoteItem->getAddress()->getShippingAmount();
287-
}
288-
289-
return $this->formatPrice($quoteItemValue);
290-
}
291-
292-
293250
/**
294251
* @param Product $product
295252
* @param string $attributeCode

Model/DataLayer/AbstractOrder.php

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
<?php
2+
/**
3+
* Copyright © Magefan ([email protected]). All rights reserved.
4+
* Please visit Magefan.com for license details (https://magefan.com/end-user-license-agreement).
5+
*/
6+
7+
declare(strict_types=1);
8+
9+
namespace Magefan\GoogleTagManager\Model\DataLayer;
10+
11+
use Magefan\GoogleTagManager\Model\AbstractDataLayer;
12+
use Magefan\GoogleTagManager\Model\Config;
13+
use Magento\Catalog\Api\CategoryRepositoryInterface;
14+
use Magento\Sales\Model\Order;
15+
use Magento\Store\Model\StoreManagerInterface;
16+
use Magefan\GoogleTagManager\Api\DataLayer\Order\ItemInterface;
17+
18+
abstract class AbstractOrder extends AbstractDataLayer
19+
{
20+
/**
21+
* @var ItemInterface
22+
*/
23+
private $gtmItem;
24+
25+
/**
26+
* Purchase constructor.
27+
*
28+
* @param Config $config
29+
* @param StoreManagerInterface $storeManager
30+
* @param CategoryRepositoryInterface $categoryRepository
31+
* @param ItemInterface $gtmItem
32+
*/
33+
public function __construct(
34+
Config $config,
35+
StoreManagerInterface $storeManager,
36+
CategoryRepositoryInterface $categoryRepository,
37+
ItemInterface $gtmItem
38+
) {
39+
$this->gtmItem = $gtmItem;
40+
parent::__construct($config, $storeManager, $categoryRepository);
41+
}
42+
43+
/**
44+
* @inheritDoc
45+
*/
46+
public function get(Order $order, string $requester = ''): array
47+
{
48+
if ($order) {
49+
$items = [];
50+
foreach ($order->getAllVisibleItems() as $item) {
51+
$items[] = $this->gtmItem->get($item);
52+
}
53+
54+
return $this->eventWrap([
55+
'event' => $this->getEventName(),
56+
'ecommerce' => [
57+
'transaction_id' => $order->getIncrementId(),
58+
'value' => $this->getValue($order),
59+
'tax' => $this->formatPrice((float)$order->getTaxAmount()),
60+
'shipping' => $this->formatPrice((float)$order->getShippingAmount()),
61+
'currency' => $this->getCurrentCurrencyCode(),
62+
'coupon' => $order->getCouponCode() ?: '',
63+
'items' => $items
64+
],
65+
'is_virtual' => (bool)$order->getIsVirtual(),
66+
'shipping_description' => $order->getShippingDescription(),
67+
'customer_is_guest' => (bool)$order->getCustomerIsGuest(),
68+
'customer_identifier' => hash('sha256', (string)$order->getCustomerEmail()),
69+
]);
70+
}
71+
72+
return [];
73+
}
74+
75+
/**
76+
* @param Order $order
77+
* @return float
78+
*/
79+
protected function getValue(Order $order): float
80+
{
81+
$orderValue = (float)$order->getGrandTotal();
82+
83+
if (!$this->config->isPurchaseTaxEnabled()) {
84+
$orderValue -= $order->getTaxAmount();
85+
}
86+
87+
if (!$this->config->isPurchaseShippingEnabled()) {
88+
$orderValue -= $order->getShippingAmount();
89+
}
90+
91+
return $this->formatPrice($orderValue);
92+
}
93+
94+
/**
95+
* @return string
96+
*/
97+
abstract protected function getEventName(): string;
98+
}

Model/DataLayer/BeginCheckout.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,16 +47,19 @@ public function __construct(
4747
public function get(Quote $quote): array
4848
{
4949
$items = [];
50+
$value = 0;
5051

51-
foreach ($quote->getAllVisibleItems() as $item) {
52-
$items[] = $this->gtmItem->get($item);
52+
foreach ($quote->getAllVisibleItems() as $quoteItem) {
53+
$item = $this->gtmItem->get($quoteItem);
54+
$items[] = $item;
55+
$value += $item['price'] * $item['quantity'];
5356
}
5457

5558
return $this->eventWrap([
5659
'event' => 'begin_checkout',
5760
'ecommerce' => [
5861
'currency' => $this->getCurrentCurrencyCode(),
59-
'value' => $this->getQuoteValue($quote),
62+
'value' => $this->formatPrice($value),
6063
'coupon' => $quote->getCouponCode() ?: '',
6164
'items' => $items
6265
],

Model/DataLayer/Cart/Item.php

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,14 @@
1010

1111
use Magefan\GoogleTagManager\Api\DataLayer\Cart\ItemInterface;
1212
use Magefan\GoogleTagManager\Model\AbstractDataLayer;
13+
use Magento\Quote\Model\Quote\Item as QuoteItem;
1314

1415
class Item extends AbstractDataLayer implements ItemInterface
1516
{
1617
/**
1718
* @inheritDoc
1819
*/
19-
public function get(\Magento\Quote\Model\Quote\Item $quoteItem): array
20+
public function get(QuoteItem $quoteItem): array
2021
{
2122
$product = $quoteItem->getProduct();
2223
$categoryNames = $this->getCategoryNames($product);
@@ -26,9 +27,30 @@ public function get(\Magento\Quote\Model\Quote\Item $quoteItem): array
2627
: $this->getProductAttributeValue($product, $this->config->getProductAttribute()),
2728
'item_name' => $quoteItem->getName(),
2829
'discount' => $this->formatPrice((float)$quoteItem->getDiscountAmount()),
30+
'coupon_code' => $quoteItem->getQuote()->getCouponCode() ?: '',
2931
'item_brand' => $this->getProductAttributeValue($product, $this->config->getBrandAttribute()),
30-
'price' => $this->formatPrice((float)$quoteItem->getPriceInclTax()),
32+
'price' => $this->getValue($quoteItem),
3133
'quantity' => $quoteItem->getQty() * 1
3234
]), $categoryNames);
3335
}
36+
37+
/**
38+
* @param $quoteItem
39+
* @return float
40+
*/
41+
protected function getValue(QuoteItem $quoteItem): float
42+
{
43+
if ($this->config->isPurchaseTaxEnabled()) {
44+
$value = (float)$quoteItem->getPriceInclTax();
45+
} else {
46+
$value = (float)$quoteItem->getPrice();
47+
}
48+
49+
//fix for magento 2.3.2 - module-quote/Model/Quote/Item/Processor.php prepareItem does not set price to quote item
50+
if (!$value && ($quoteItemProduct = $quoteItem->getProduct())) {
51+
return $this->getProductValue($quoteItemProduct);
52+
} else {
53+
return $this->formatPrice($value);
54+
}
55+
}
3456
}

Model/DataLayer/Order/Item.php

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,23 @@ public function get(OrderItemInterface $orderItem): array
2828
'item_name' => $orderItem->getName(),
2929
'discount' => $this->formatPrice((float)$orderItem->getDiscountAmount()),
3030
'item_brand' => $this->getProductAttributeValue($product, $this->config->getBrandAttribute()),
31-
'price' => $this->formatPrice((float)$orderItem->getPriceInclTax()),
31+
'price' => $this->getValue($orderItem),
3232
'quantity' => $orderItem->getQtyOrdered() * 1
3333
]), $categoryNames);
3434
}
35+
36+
/**
37+
* @param $quoteItem
38+
* @return float
39+
*/
40+
protected function getValue(OrderItemInterface $orderItem): float
41+
{
42+
if ($this->config->isPurchaseTaxEnabled()) {
43+
$value = (float)$orderItem->getPriceInclTax();
44+
} else {
45+
$value = (float)$orderItem->getPrice();
46+
}
47+
48+
return $this->formatPrice($value);
49+
}
3550
}

Model/DataLayer/Product/Item.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public function get(Product $product): array
2424
'item_id' => $this->getProductAttributeValue($product, $this->config->getProductAttribute()),
2525
'item_name' => $product->getName(),
2626
'item_brand' => $this->getProductAttributeValue($product, $this->config->getBrandAttribute()),
27-
'price' => $this->getPrice($product)
27+
'price' => $this->getProductValue($product)
2828
]), $categoryNames);
2929
}
3030
}

Model/DataLayer/Purchase.php

Lines changed: 4 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -9,91 +9,19 @@
99
namespace Magefan\GoogleTagManager\Model\DataLayer;
1010

1111
use Magefan\GoogleTagManager\Api\DataLayer\PurchaseInterface;
12-
use Magefan\GoogleTagManager\Model\AbstractDataLayer;
13-
use Magefan\GoogleTagManager\Model\Config;
14-
use Magento\Catalog\Api\CategoryRepositoryInterface;
15-
use Magento\Sales\Model\Order;
16-
use Magento\Store\Model\StoreManagerInterface;
17-
use Magefan\GoogleTagManager\Api\DataLayer\Order\ItemInterface;
1812

19-
class Purchase extends AbstractDataLayer implements PurchaseInterface
13+
class Purchase extends AbstractOrder implements PurchaseInterface
2014
{
21-
/**
22-
* @var ItemInterface
23-
*/
24-
private $gtmItem;
25-
2615
/**
2716
* @var string
2817
*/
2918
protected $ecommPageType = 'purchase';
3019

3120
/**
32-
* Purchase constructor.
33-
*
34-
* @param Config $config
35-
* @param StoreManagerInterface $storeManager
36-
* @param CategoryRepositoryInterface $categoryRepository
37-
* @param ItemInterface $gtmItem
21+
* @return string
3822
*/
39-
public function __construct(
40-
Config $config,
41-
StoreManagerInterface $storeManager,
42-
CategoryRepositoryInterface $categoryRepository,
43-
ItemInterface $gtmItem
44-
) {
45-
$this->gtmItem = $gtmItem;
46-
parent::__construct($config, $storeManager, $categoryRepository);
47-
}
48-
49-
/**
50-
* @inheritDoc
51-
*/
52-
public function get(Order $order, string $requester = ''): array
23+
protected function getEventName(): string
5324
{
54-
if ($order) {
55-
$items = [];
56-
foreach ($order->getAllVisibleItems() as $item) {
57-
$items[] = $this->gtmItem->get($item);
58-
}
59-
60-
return $this->eventWrap([
61-
'event' => 'purchase',
62-
'ecommerce' => [
63-
'transaction_id' => $order->getIncrementId(),
64-
'value' => $this->getOrderValue($order),
65-
'tax' => $this->formatPrice((float)$order->getTaxAmount()),
66-
'shipping' => $this->formatPrice((float)$order->getShippingAmount()),
67-
'currency' => $this->getCurrentCurrencyCode(),
68-
'coupon' => $order->getCouponCode() ?: '',
69-
'items' => $items
70-
],
71-
'is_virtual' => (bool)$order->getIsVirtual(),
72-
'shipping_description' => $order->getShippingDescription(),
73-
'customer_is_guest' => (bool)$order->getCustomerIsGuest(),
74-
'customer_identifier' => hash('sha256', (string)$order->getCustomerEmail()),
75-
]);
76-
}
77-
78-
return [];
79-
}
80-
81-
/**
82-
* @param $order
83-
* @return float
84-
*/
85-
protected function getOrderValue($order): float
86-
{
87-
$orderValue = (float)$order->getGrandTotal();
88-
89-
if (!$this->config->isPurchaseTaxEnabled()) {
90-
$orderValue -= $order->getTaxAmount();
91-
}
92-
93-
if (!$this->config->isPurchaseShippingEnabled()) {
94-
$orderValue -= $order->getShippingAmount();
95-
}
96-
97-
return $this->formatPrice($orderValue);
25+
return 'purchase';
9826
}
9927
}

0 commit comments

Comments
 (0)