1010use Exception ;
1111use Magento \Framework \DataObject ;
1212use Magento \Framework \Escaper ;
13+ use Magento \Framework \Exception \LocalizedException ;
1314use Magento \Framework \Exception \NoSuchEntityException ;
1415use Magento \Sales \Model \Order as SalesOrder ;
1516use Magento \Sales \Model \Order \Item ;
1617use Magento \Sales \Model \Order \Payment ;
1718use Magento \Sales \Model \ResourceModel \Order \Collection ;
1819use Magento \Sales \Model \ResourceModel \Order \CollectionFactoryInterface ;
19- use Magento \Store \Model \StoreManagerInterface ;
2020use MagePal \GoogleTagManager \DataLayer \OrderData \OrderItemProvider ;
2121use MagePal \GoogleTagManager \DataLayer \OrderData \OrderProvider ;
2222use MagePal \GoogleTagManager \Helper \Data as GtmHelper ;
@@ -44,9 +44,6 @@ class Order extends DataObject
4444 */
4545 protected $ _orderCollection = null ;
4646
47- /** @var StoreManagerInterface */
48- protected $ _storeManager ;
49-
5047 /**
5148 * Escaper
5249 *
@@ -70,7 +67,6 @@ class Order extends DataObject
7067 * Order constructor.
7168 * @param CollectionFactoryInterface $salesOrderCollection
7269 * @param GtmHelper $gtmHelper
73- * @param StoreManagerInterface $storeManager
7470 * @param Escaper $escaper
7571 * @param OrderProvider $orderProvider
7672 * @param OrderItemProvider $orderItemProvider
@@ -80,7 +76,6 @@ class Order extends DataObject
8076 public function __construct (
8177 CollectionFactoryInterface $ salesOrderCollection ,
8278 GtmHelper $ gtmHelper ,
83- StoreManagerInterface $ storeManager ,
8479 Escaper $ escaper ,
8580 OrderProvider $ orderProvider ,
8681 OrderItemProvider $ orderItemProvider ,
@@ -90,7 +85,6 @@ public function __construct(
9085 parent ::__construct ($ data );
9186 $ this ->gtmHelper = $ gtmHelper ;
9287 $ this ->_salesOrderCollection = $ salesOrderCollection ;
93- $ this ->_storeManager = $ storeManager ;
9488 $ this ->_escaper = $ escaper ;
9589 $ this ->orderProvider = $ orderProvider ;
9690 $ this ->orderItemProvider = $ orderItemProvider ;
@@ -117,12 +111,14 @@ public function getOrderLayer()
117111
118112 foreach ($ collection as $ order ) {
119113 $ transaction = $ this ->getTransactionDetail ($ order );
120- $ result [] = $ this ->orderProvider ->setOrder ($ order )->setTransactionData ($ transaction )->getData ();
114+ $ data = $ this ->orderProvider ->setOrder ($ order )->setTransactionData ($ transaction )->getData ();
115+ $ result [] = $ data ;
121116
122117 // retain backward comparability with gtm.orderComplete event
123- $ result [] = $ transaction = [
124- 'event ' => DataLayerEvent::PURCHASE_EVENT
125- ];
118+ if ($ transaction ['event ' ] !== DataLayerEvent::PURCHASE_EVENT ) {
119+ $ data ['event ' ] = DataLayerEvent::PURCHASE_EVENT ;
120+ $ result [] = $ data ;
121+ }
126122 }
127123
128124 return $ result ;
@@ -133,7 +129,7 @@ public function getTransactionDetail($order)
133129 return [
134130 'event ' => DataLayerEvent::GTM_ORDER_COMPLETE_EVENT ,
135131 'transactionId ' => $ order ->getIncrementId (),
136- 'transactionAffiliation ' => $ this ->_storeManager -> getStore ()-> getFrontendName ( ),
132+ 'transactionAffiliation ' => $ this ->escapeReturn ( $ order -> getStoreName () ),
137133 'transactionTotal ' => $ this ->gtmHelper ->formatPrice ($ order ->getBaseGrandTotal ()),
138134 'transactionSubTotal ' => $ this ->gtmHelper ->formatPrice ($ order ->getBaseSubtotal ()),
139135 'transactionShipping ' => $ this ->gtmHelper ->formatPrice ($ order ->getBaseShippingAmount ()),
@@ -215,7 +211,7 @@ public function escapeReturn($data)
215211 /**
216212 * @param SalesOrder $order
217213 * @return array
218- * @throws NoSuchEntityException
214+ * @throws LocalizedException
219215 */
220216 public function getOrderDataLayer (SalesOrder $ order )
221217 {
@@ -238,7 +234,7 @@ public function getOrderDataLayer(SalesOrder $order)
238234 'discount_amount ' => $ this ->gtmHelper ->formatPrice ($ item ->getDiscountAmount ()),
239235 'discount_percent ' => $ this ->gtmHelper ->formatPrice ($ item ->getDiscountPercent ()),
240236 'tax_amount ' => $ this ->gtmHelper ->formatPrice ($ item ->getTaxAmount ()),
241- 'is_virtual ' => $ item ->getIsVirtual () ? true : false ,
237+ 'is_virtual ' => ( bool ) $ item ->getIsVirtual (),
242238 ];
243239
244240 if ($ variant = $ this ->dataLayerItemHelper ->getItemVariant ($ item )) {
@@ -258,7 +254,7 @@ public function getOrderDataLayer(SalesOrder $order)
258254
259255 return [
260256 'order_id ' => $ order ->getIncrementId (),
261- 'store_name ' => $ this ->_storeManager -> getStore ()-> getFrontendName ( ),
257+ 'store_name ' => $ this ->escapeReturn ( $ order -> getStoreName () ),
262258 'total ' => $ this ->gtmHelper ->formatPrice ($ order ->getBaseGrandTotal ()),
263259 'subtotal ' => $ this ->gtmHelper ->formatPrice ($ order ->getBaseSubtotal ()),
264260 'shipping ' => $ this ->gtmHelper ->formatPrice ($ order ->getBaseShippingAmount ()),
@@ -268,8 +264,8 @@ public function getOrderDataLayer(SalesOrder $order)
268264 'discount ' => $ this ->gtmHelper ->formatPrice ($ order ->getDiscountAmount ()),
269265 'payment_method ' => $ this ->getPaymentMethod ($ order ),
270266 'shipping_method ' => ['title ' => $ order ->getShippingDescription (), 'code ' => $ order ->getShippingMethod ()],
271- 'is_virtual ' => $ order ->getIsVirtual () ? true : false ,
272- 'is_guest_checkout ' => $ order ->getCustomerIsGuest () ? true : false ,
267+ 'is_virtual ' => ( bool ) $ order ->getIsVirtual (),
268+ 'is_guest_checkout ' => ( bool ) $ order ->getCustomerIsGuest (),
273269 'items ' => $ products
274270 ];
275271 }
@@ -283,11 +279,11 @@ public function getPaymentMethod(SalesOrder $order)
283279 try {
284280 /** @var Payment $payment */
285281 $ payment = $ order ->getPayment ();
286- $ method = $ payment ->getMethodInstance ();
282+ $ methodInstance = $ payment ->getMethodInstance ();
287283
288284 $ method = [
289- 'title ' => $ method ->getTitle (),
290- 'code ' => $ method ->getCode ()
285+ 'title ' => $ methodInstance ->getTitle (),
286+ 'code ' => $ methodInstance ->getCode ()
291287 ];
292288 } catch (Exception $ e ) {
293289 $ method = [
0 commit comments