diff --git a/src/Plugin/GoogleTag/Event/Commerce/AddToCartEvent.php b/src/Plugin/GoogleTag/Event/Commerce/AddToCartEvent.php index e7e0d01341d8ffe33698cb7b50c3992c5b12570d..5d36ff9df18811fbce03307da12d43e20b4dbd58 100644 --- a/src/Plugin/GoogleTag/Event/Commerce/AddToCartEvent.php +++ b/src/Plugin/GoogleTag/Event/Commerce/AddToCartEvent.php @@ -53,7 +53,7 @@ final class AddToCartEvent extends EventBase { $item_data['item_id'] = $purchased_entity->id(); } return [ - 'currency' => $unit_price->getCurrencyCode(), + 'currency' => $unit_price?->getCurrencyCode() ?? NULL, 'value' => $this->formatPriceNumber($adjusted_price), 'items' => [ $item_data, diff --git a/src/Plugin/GoogleTag/Event/Commerce/AddToWishlistEvent.php b/src/Plugin/GoogleTag/Event/Commerce/AddToWishlistEvent.php index feaeb8723a0d2a12ea9675a1bcf498e383a9c4bc..5822466cd2835a1ffac2906e45d89b367716e3f3 100644 --- a/src/Plugin/GoogleTag/Event/Commerce/AddToWishlistEvent.php +++ b/src/Plugin/GoogleTag/Event/Commerce/AddToWishlistEvent.php @@ -71,7 +71,7 @@ final class AddToWishlistEvent extends EventBase implements ContainerFactoryPlug $item_data['item_id'] = $purchased_entity->id(); } return [ - 'currency' => $purchased_entity->getPrice()->getCurrencyCode(), + 'currency' => $purchased_entity?->getPrice()?->getCurrencyCode() ?? NULL, 'value' => $this->formatPriceNumber($purchased_entity->getPrice()), 'items' => [ $item_data, diff --git a/src/Plugin/GoogleTag/Event/Commerce/BeginCheckoutEvent.php b/src/Plugin/GoogleTag/Event/Commerce/BeginCheckoutEvent.php index 114a5a92a5b18f0f605281cf710e05758095fa44..39ada9633aef239eba737d91a7c7c6b103375a98 100644 --- a/src/Plugin/GoogleTag/Event/Commerce/BeginCheckoutEvent.php +++ b/src/Plugin/GoogleTag/Event/Commerce/BeginCheckoutEvent.php @@ -37,7 +37,7 @@ final class BeginCheckoutEvent extends EventBase { $affiliation = $order->getStore()->label(); return [ - 'currency' => $order_total->getCurrencyCode(), + 'currency' => $order_total?->getCurrencyCode() ?? NULL, 'value' => $this->formatPriceNumber($order_total), 'items' => array_map( function (OrderItemInterface $order_item) use ($affiliation) { diff --git a/src/Plugin/GoogleTag/Event/Commerce/CommerceEventTrait.php b/src/Plugin/GoogleTag/Event/Commerce/CommerceEventTrait.php index f77bf7e660e4dcd85e18416abc586e196dbf7aaa..2168f6c524533fb2dfc6aa67a2b5bc399be0f87e 100644 --- a/src/Plugin/GoogleTag/Event/Commerce/CommerceEventTrait.php +++ b/src/Plugin/GoogleTag/Event/Commerce/CommerceEventTrait.php @@ -14,14 +14,21 @@ trait CommerceEventTrait { /** * Formats price number. * - * @param \Drupal\commerce_price\Price $price + * @param \Drupal\commerce_price\Price|null $price * Price object. * * @return string * Formatted price. */ - protected function formatPriceNumber(Price $price): string { - return number_format((float) $price->getNumber(), 2, '.', ''); + protected function formatPriceNumber(?Price $price): string { + if ($price === NULL) { + $number = 0; + } + else { + $number = $price->getNumber(); + } + + return number_format((float) $number, 2, '.', ''); } } diff --git a/src/Plugin/GoogleTag/Event/Commerce/PurchaseEvent.php b/src/Plugin/GoogleTag/Event/Commerce/PurchaseEvent.php index 67500178c531a6258d8f0176ac3ced5f1fbdb782..80bc58f19aef3658f49982e50903f447f199ea68 100644 --- a/src/Plugin/GoogleTag/Event/Commerce/PurchaseEvent.php +++ b/src/Plugin/GoogleTag/Event/Commerce/PurchaseEvent.php @@ -63,7 +63,7 @@ final class PurchaseEvent extends EventBase implements ContainerFactoryPluginInt $affiliation = $order->getStore()->label(); return [ - 'currency' => $order_total->getCurrencyCode(), + 'currency' => $order_total?->getCurrencyCode() ?? NULL, 'value' => $this->formatPriceNumber($order_total), 'transaction_id' => $order->getOrderNumber(), 'shipping' => array_reduce( diff --git a/src/Plugin/GoogleTag/Event/Commerce/RefundEvent.php b/src/Plugin/GoogleTag/Event/Commerce/RefundEvent.php index 94f15c5c8735652735af049b2befcf8b1f802182..f872673b25229c84fa56a7eec5c1272a7cdd1db3 100644 --- a/src/Plugin/GoogleTag/Event/Commerce/RefundEvent.php +++ b/src/Plugin/GoogleTag/Event/Commerce/RefundEvent.php @@ -59,7 +59,7 @@ final class RefundEvent extends EventBase implements ContainerFactoryPluginInter $order_total = $order->getTotalPrice(); return [ - 'currency' => $order_total->getCurrencyCode(), + 'currency' => $order_total?->getCurrencyCode() ?? NULL, 'value' => $this->formatPriceNumber($order_total), 'transaction_id' => $order->getOrderNumber(), 'shipping' => array_reduce( diff --git a/src/Plugin/GoogleTag/Event/Commerce/RemoveFromCartEvent.php b/src/Plugin/GoogleTag/Event/Commerce/RemoveFromCartEvent.php index ce3919c37cd78b8bba53128fae052eff9207ccdf..175a105827e7163250edc1d0aadd5b78fbd10ec6 100644 --- a/src/Plugin/GoogleTag/Event/Commerce/RemoveFromCartEvent.php +++ b/src/Plugin/GoogleTag/Event/Commerce/RemoveFromCartEvent.php @@ -53,7 +53,7 @@ final class RemoveFromCartEvent extends EventBase { $item_data['item_id'] = $purchased_entity->id(); } return [ - 'currency' => $unit_price->getCurrencyCode(), + 'currency' => $unit_price?->getCurrencyCode() ?? NULL, 'value' => $this->formatPriceNumber($adjusted_price), 'items' => [ $item_data, diff --git a/src/Plugin/GoogleTag/Event/Commerce/ViewItemEvent.php b/src/Plugin/GoogleTag/Event/Commerce/ViewItemEvent.php index 10b1c8fa7fa2670b07ff96ee9ee2ea32d976ca17..e98ab519b073e509e183ca71bee1075951454b02 100644 --- a/src/Plugin/GoogleTag/Event/Commerce/ViewItemEvent.php +++ b/src/Plugin/GoogleTag/Event/Commerce/ViewItemEvent.php @@ -66,7 +66,7 @@ final class ViewItemEvent extends EventBase implements ContainerFactoryPluginInt 'affiliation' => $this->currentStore->getStore()->label(), ]; return [ - 'currency' => $item->getPrice()->getCurrencyCode(), + 'currency' => $item->getPrice()?->getCurrencyCode() ?? NULL, 'value' => $this->formatPriceNumber($item->getPrice()), 'items' => [ $item_data,