Skip to content

Commit

Permalink
Fix inconsistency in escaping product name characters
Browse files Browse the repository at this point in the history
  • Loading branch information
srenon committed Mar 26, 2021
1 parent df30b6d commit 302593d
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 42 deletions.
2 changes: 1 addition & 1 deletion Model/Cart.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ public function getCart()
$itemData = [
'sku' => $item->getSku(),
'parent_sku' => $item->getProduct() ? $item->getProduct()->getData('sku') : $item->getSku(),
'name' => $this->escapeJsQuote($item->getName()),
'name' => $item->getName(),
'product_type' => $item->getProductType(),
'price' => $this->dataLayerItemHelper->formatPrice($item->getPrice()),
'price_incl_tax' => $this->dataLayerItemHelper->formatPrice($item->getPriceInclTax()),
Expand Down
87 changes: 48 additions & 39 deletions Model/Order.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
use MagePal\GoogleTagManager\Helper\DataLayerItem;

/**
* @method $this setOrderIds(Array $orderIds)
* @method $this setOrderIds($orderIds)
* @method Array getOrderIds()
*/
class Order extends DataObject
Expand Down Expand Up @@ -116,41 +116,7 @@ public function getOrderLayer()
/* @var SalesOrder $order */

foreach ($collection as $order) {
$products = [];
/* @var Item $item */
foreach ($order->getAllVisibleItems() as $item) {
$product = [
'sku' => $item->getSku(),
'name' => $this->escapeJsQuote($item->getName()),
'price' => $this->gtmHelper->formatPrice($item->getBasePrice()),
'quantity' => $item->getQtyOrdered() * 1
];

if ($category = $this->dataLayerItemHelper->getFirstCategory($item)) {
$product['category'] = $category;
}

$products[] = $this->orderItemProvider
->setItem($item)
->setItemData($product)
->setListType(OrderItemProvider::LIST_TYPE_GOOGLE)
->getData();
}

$transaction = [
'event' => DataLayerEvent::GTM_ORDER_COMPLETE_EVENT,
'transactionId' => $order->getIncrementId(),
'transactionAffiliation' => $this->escapeJsQuote($this->_storeManager->getStore()->getFrontendName()),
'transactionTotal' => $this->gtmHelper->formatPrice($order->getBaseGrandTotal()),
'transactionSubTotal' => $this->gtmHelper->formatPrice($order->getBaseSubtotal()),
'transactionShipping' => $this->gtmHelper->formatPrice($order->getBaseShippingAmount()),
'transactionTax' => $this->gtmHelper->formatPrice($order->getTaxAmount()),
'transactionCouponCode' => $order->getCouponCode() ? $order->getCouponCode() : '',
'transactionDiscount' => $this->gtmHelper->formatPrice($order->getDiscountAmount()),
'transactionProducts' => $products,
'order' => $this->getOrderDataLayer($order)
];

$transaction = $this->getTransactionDetail($order);
$result[] = $this->orderProvider->setOrder($order)->setTransactionData($transaction)->getData();

// retain backward comparability with gtm.orderComplete event
Expand All @@ -162,6 +128,49 @@ public function getOrderLayer()
return $result;
}

public function getTransactionDetail($order)
{
return [
'event' => DataLayerEvent::GTM_ORDER_COMPLETE_EVENT,
'transactionId' => $order->getIncrementId(),
'transactionAffiliation' => $this->_storeManager->getStore()->getFrontendName(),
'transactionTotal' => $this->gtmHelper->formatPrice($order->getBaseGrandTotal()),
'transactionSubTotal' => $this->gtmHelper->formatPrice($order->getBaseSubtotal()),
'transactionShipping' => $this->gtmHelper->formatPrice($order->getBaseShippingAmount()),
'transactionTax' => $this->gtmHelper->formatPrice($order->getTaxAmount()),
'transactionCouponCode' => $order->getCouponCode() ? $order->getCouponCode() : '',
'transactionDiscount' => $this->gtmHelper->formatPrice($order->getDiscountAmount()),
'transactionProducts' => $this->getItemTransactionDetail($order),
'order' => $this->getOrderDataLayer($order)
];
}

public function getItemTransactionDetail($order)
{
$products = [];
/* @var Item $item */
foreach ($order->getAllVisibleItems() as $item) {
$product = [
'sku' => $item->getSku(),
'name' => $item->getName(),
'price' => $this->gtmHelper->formatPrice($item->getBasePrice()),
'quantity' => $item->getQtyOrdered() * 1
];

if ($category = $this->dataLayerItemHelper->getFirstCategory($item)) {
$product['category'] = $category;
}

$products[] = $this->orderItemProvider
->setItem($item)
->setItemData($product)
->setListType(OrderItemProvider::LIST_TYPE_GOOGLE)
->getData();
}

return $products;
}

/**
* Get order collection
*
Expand Down Expand Up @@ -218,8 +227,8 @@ public function getOrderDataLayer(SalesOrder $order)
'sku' => $item->getSku(),
'id' => $item->getSku(),
'parent_sku' => $item->getProduct() ? $item->getProduct()->getData('sku') : $item->getSku(),
'name' => $this->escapeJsQuote($item->getProductOptionByCode('simple_name') ?: $item->getName()),
'parent_name' => $this->escapeJsQuote($item->getName()),
'name' => $item->getProductOptionByCode('simple_name') ?: $item->getName(),
'parent_name' => $item->getName(),
'price' => $this->gtmHelper->formatPrice($item->getBasePrice()),
'price_incl_tax' => $this->dataLayerItemHelper->formatPrice($item->getPriceInclTax()),
'quantity' => $item->getQtyOrdered() * 1,
Expand Down Expand Up @@ -249,7 +258,7 @@ public function getOrderDataLayer(SalesOrder $order)

return [
'order_id' => $order->getIncrementId(),
'store_name' => $this->escapeJsQuote($this->_storeManager->getStore()->getFrontendName()),
'store_name' => $this->_storeManager->getStore()->getFrontendName(),
'total' => $this->gtmHelper->formatPrice($order->getBaseGrandTotal()),
'subtotal' => $this->gtmHelper->formatPrice($order->getBaseSubtotal()),
'shipping' => $this->gtmHelper->formatPrice($order->getBaseShippingAmount()),
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
"magepal/magento2-enhanced-ecommerce": "Get more from Google Tag Manager with Enhanced E-commerce. Learn more at https://www.magepal.com/enhanced-ecommerce-for-google-tag-manager.html"
},
"type": "magento2-module",
"version": "2.6.0",
"version": "2.6.1",
"autoload": {
"files": [
"registration.php"
Expand Down
10 changes: 10 additions & 0 deletions view/frontend/templates/js.phtml
Original file line number Diff line number Diff line change
Expand Up @@ -44,5 +44,15 @@ $containerCode = $block->getEmbeddedCode();
}
}
</script>
<?php else : ?>
<script type="text/x-magento-init">
{
"*": {
"magepalGtmDatalayer": {
"dataLayer": "<?= /* @noEscape */ $block->getDataLayerName() ?>",
}
}
}
</script>
<?php endif; ?>
<!-- End Google Tag Manager by MagePal -->
2 changes: 1 addition & 1 deletion view/frontend/web/js/datalayer.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ define([

window[config.dataLayer] = window[config.dataLayer] || [];

if (isTrackingAllowed(config)) {
if (_.has(config, 'accountId') && isTrackingAllowed(config)) {
pushData(config.dataLayer, config.data);
initTracking(config.dataLayer, config.accountId, config.containerCode);
}
Expand Down

0 comments on commit 302593d

Please sign in to comment.