yireo / Yireo_GoogleTagManager2

Open Software License 3.0
147 stars 76 forks source link

Magento 2.4.6-p1 - No such entity with cartId = 0 for guest users. #161

Closed mdkdev closed 1 year ago

mdkdev commented 1 year ago

In Yireo\GoogleTagManager2\Plugin\TriggerAddGuestPaymentInfoDataLayerEvent, there's a $cartId that is typecasted to an int, but the incoming parameter is a masked quoteId. When typecasted, this becomes 0, and an error is thrown.

Below is a patch that resolves this.

diff --git a/vendor/yireo/magento2-googletagmanager2/Plugin/TriggerAddGuestPaymentInfoDataLayerEvent.php b/vendor/yireo/magento2-googletagmanager2/Plugin/TriggerAddGuestPaymentInfoDataLayerEvent.php
--- a/vendor/yireo/magento2-googletagmanager2/Plugin/TriggerAddGuestPaymentInfoDataLayerEvent.php   
+++ b/vendor/yireo/magento2-googletagmanager2/Plugin/TriggerAddGuestPaymentInfoDataLayerEvent.php   (date 1686909085843)
@@ -4,6 +4,7 @@

 namespace Yireo\GoogleTagManager2\Plugin;

+use Magento\Quote\Model\MaskedQuoteIdToQuoteIdInterface;
 use Yireo\GoogleTagManager2\Api\CheckoutSessionDataProviderInterface;
 use Yireo\GoogleTagManager2\DataLayer\Event\AddPaymentInfo;

@@ -11,13 +12,16 @@
 {
     private CheckoutSessionDataProviderInterface $checkoutSessionDataProvider;
     private AddPaymentInfo $addPaymentInfo;
+    private MaskedQuoteIdToQuoteIdInterface $maskedQuoteIdToQuoteId;

     public function __construct(
         CheckoutSessionDataProviderInterface $checkoutSessionDataProvider,
-        AddPaymentInfo $addPaymentInfo
+        AddPaymentInfo $addPaymentInfo,
+        MaskedQuoteIdToQuoteIdInterface $maskedQuoteIdToQuoteId
     ) {
         $this->checkoutSessionDataProvider = $checkoutSessionDataProvider;
         $this->addPaymentInfo = $addPaymentInfo;
+        $this->maskedQuoteIdToQuoteId = $maskedQuoteIdToQuoteId;
     }

     public function afterSavePaymentInformationAndPlaceOrder(
@@ -28,6 +32,7 @@
         \Magento\Quote\Api\Data\PaymentInterface $paymentMethod,
         \Magento\Quote\Api\Data\AddressInterface $billingAddress = null
     ) {
+        $cartId = $this->maskedQuoteIdToQuoteId->execute($cartId);
         $addPaymentInfoEventData = $this->addPaymentInfo
             ->setPaymentMethod($paymentMethod->getMethod())
             ->setCartId((int)$cartId)
@@ -45,6 +50,7 @@
         \Magento\Quote\Api\Data\PaymentInterface $paymentMethod,
         \Magento\Quote\Api\Data\AddressInterface $billingAddress = null
     ) {
+        $cartId = $this->maskedQuoteIdToQuoteId->execute($cartId);
         $addPaymentInfoEventData = $this->addPaymentInfo
             ->setPaymentMethod($paymentMethod->getMethod())
             ->setCartId((int)$cartId)
jissereitsma commented 1 year ago

Thanks. This seems like a bug in the PR that came in 20 hours ago. I overlooked this myself as well. This fix will be included in the release of today (3.3.2).

jissereitsma commented 1 year ago

Note that copying and pasting diff files is actually also error-prone. Easiest is to make a Pull Request here on GitHub so that the PR allows for further reviewing and debating on a per line basis. Just 50 cents.

jissereitsma commented 1 year ago

I'm closing this issue because it should be fixed in 3.3.2+. Let me know if it is not fixed yet :)