yireo/Yireo_GoogleTagManager2

AddShippingInfo breaks integration API

rhoerr opened this issue · 1 comments

rhoerr commented

The AddShippingInfo class (triggered by plugin TriggerAddShippingInfoDataLayerEvent) breaks integration API calls to /shipping-information.

Example:
/rest/V1/carts/1559842/shipping-information
with this module installed, calling this via an admin integration token (rather than frontend session/user token) results in error

No such entity with cartId = null

This happens because checkoutSession is not instantiated in this context, and so $this->checkoutSession->getQuote()->getId() returns null.

I don't think the GTM event is necessary in this context because the process is fully server-side (no client to send the GTM data), so this is easily fixed by checking for a session quote before loading:

diff --git a/DataLayer/Event/AddShippingInfo.php b/DataLayer/Event/AddShippingInfo.php
--- a/DataLayer/Event/AddShippingInfo.php
+++ b/DataLayer/Event/AddShippingInfo.php
@@ -38,7 +38,7 @@
     {
         $shippingMethod = $this->cart->getShippingAddress()->getShippingMethod();

-        if (empty($shippingMethod)) {
+        if (empty($shippingMethod) && $this->checkoutSession->hasQuote()) {
             $quoteId = $this->checkoutSession->getQuote()->getId();
             $shippingMethod = $this->getShippingMethodFromQuote((int)$quoteId);
         }

Hi @rhoerr sorry for not following up earlier. Indeed this sounds like a solid fix. I've added it in the following commit to be shipped in the next release: df695f3