From 5fdf8f43444e90f6d34fadb03e94aadecdf9af03 Mon Sep 17 00:00:00 2001 From: jisse Reitsma Date: Wed, 13 Dec 2023 09:45:49 +0100 Subject: [PATCH] Improve loading of shipping method (so `add_shipping_info` event) --- CHANGELOG.md | 4 +++ DataLayer/Event/AddShippingInfo.php | 51 +++++++++++++++++++---------- composer.json | 2 +- 3 files changed, 39 insertions(+), 18 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5a0173dd..9925f760 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +## [3.7.2] - 13 December 2023 +### Fixed +- Improve loading of shipping method (so `add_shipping_info` event) + ## [3.7.1] - 12 December 2023 ### Fixed - Fix error when removing cart item #201 diff --git a/DataLayer/Event/AddShippingInfo.php b/DataLayer/Event/AddShippingInfo.php index 0000df72..71f72177 100644 --- a/DataLayer/Event/AddShippingInfo.php +++ b/DataLayer/Event/AddShippingInfo.php @@ -3,32 +3,38 @@ namespace Yireo\GoogleTagManager2\DataLayer\Event; use Magento\Checkout\Model\Session as CheckoutSession; +use Magento\Framework\Exception\NoSuchEntityException; +use Magento\Framework\Exception\StateException; +use Magento\Quote\Api\CartRepositoryInterface; +use Magento\Quote\Api\Data\CartInterface; +use Magento\Quote\Api\Data\ShippingMethodInterface; use Magento\Quote\Api\ShippingMethodManagementInterface; -use Magento\Quote\Model\Quote as Cart; use Yireo\GoogleTagManager2\Api\Data\EventInterface; use Yireo\GoogleTagManager2\DataLayer\Tag\Cart\CartItems; class AddShippingInfo implements EventInterface { - private Cart $cart; private CartItems $cartItems; private ShippingMethodManagementInterface $shippingMethodManagement; private CheckoutSession $checkoutSession; + private CartRepositoryInterface $cartRepository; /** - * @param Cart $cart * @param CartItems $cartItems + * @param ShippingMethodManagementInterface $shippingMethodManagement + * @param CheckoutSession $checkoutSession + * @param CartRepositoryInterface $cartRepository */ public function __construct( - Cart $cart, CartItems $cartItems, ShippingMethodManagementInterface $shippingMethodManagement, - CheckoutSession $checkoutSession + CheckoutSession $checkoutSession, + CartRepositoryInterface $cartRepository ) { - $this->cart = $cart; $this->cartItems = $cartItems; $this->shippingMethodManagement = $shippingMethodManagement; $this->checkoutSession = $checkoutSession; + $this->cartRepository = $cartRepository; } /** @@ -36,11 +42,13 @@ public function __construct( */ public function get(): array { - $shippingMethod = $this->cart->getShippingAddress()->getShippingMethod(); - - if (empty($shippingMethod) && $this->checkoutSession->hasQuote()) { - $quoteId = $this->checkoutSession->getQuote()->getId(); - $shippingMethod = $this->getShippingMethodFromQuote((int)$quoteId); + $this->checkoutSession->getQuote()->getShippingAddress()->getShippingMethod(); + if (false === $this->checkoutSession->hasQuote()) { + return []; + } + + if (empty($shippingMethod)) { + $shippingMethod = $this->getShippingMethodFromQuote($this->checkoutSession->getQuote()); } if (empty($shippingMethod)) { @@ -57,16 +65,25 @@ public function get(): array } /** - * @param int $quoteId + * @param CartInterface $quote * @return string|null */ - public function getShippingMethodFromQuote(int $quoteId): ?string + public function getShippingMethodFromQuote(CartInterface $quote): ?string { - $shippingMethod = $this->shippingMethodManagement->get($quoteId); - if (empty($shippingMethod)) { - return null; + try { + $shippingMethod = $this->shippingMethodManagement->get($quote->getId()); + if ($shippingMethod instanceof ShippingMethodInterface) { + return $shippingMethod->getCarrierCode().'_'.$shippingMethod->getMethodCode(); + } + } catch (NoSuchEntityException $e) { + } catch (StateException $e) { + } + + try { + return $quote->getShippingAddress()->getShippingMethod(); + } catch (NoSuchEntityException $e) { } - return $shippingMethod->getCarrierCode().'_'.$shippingMethod->getMethodCode(); + return null; } } diff --git a/composer.json b/composer.json index 6165de82..9d1f93dd 100644 --- a/composer.json +++ b/composer.json @@ -1,6 +1,6 @@ { "name": "yireo/magento2-googletagmanager2", - "version": "3.7.1", + "version": "3.7.2", "license": "OSL-3.0", "type": "magento2-module", "homepage": "https://www.yireo.com/software/magento-extensions/googletagmanager2",