From 302593d67f60278bfa7b1576a369ba971af931b7 Mon Sep 17 00:00:00 2001 From: Renon Stewart Date: Fri, 26 Mar 2021 12:45:59 -0400 Subject: [PATCH] Fix inconsistency in escaping product name characters --- Model/Cart.php | 2 +- Model/Order.php | 87 +++++++++++++++++-------------- composer.json | 2 +- view/frontend/templates/js.phtml | 10 ++++ view/frontend/web/js/datalayer.js | 2 +- 5 files changed, 61 insertions(+), 42 deletions(-) diff --git a/Model/Cart.php b/Model/Cart.php index 361179c..1e912eb 100644 --- a/Model/Cart.php +++ b/Model/Cart.php @@ -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()), diff --git a/Model/Order.php b/Model/Order.php index 325af8b..eebaa16 100644 --- a/Model/Order.php +++ b/Model/Order.php @@ -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 @@ -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 @@ -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 * @@ -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, @@ -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()), diff --git a/composer.json b/composer.json index b433b10..74264e2 100755 --- a/composer.json +++ b/composer.json @@ -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" diff --git a/view/frontend/templates/js.phtml b/view/frontend/templates/js.phtml index 4fe1823..8c0ba46 100755 --- a/view/frontend/templates/js.phtml +++ b/view/frontend/templates/js.phtml @@ -44,5 +44,15 @@ $containerCode = $block->getEmbeddedCode(); } } + + diff --git a/view/frontend/web/js/datalayer.js b/view/frontend/web/js/datalayer.js index 1b6c3e7..24bcb24 100644 --- a/view/frontend/web/js/datalayer.js +++ b/view/frontend/web/js/datalayer.js @@ -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); }