Skip to content

Commit

Permalink
Improve loading of shipping method (so add_shipping_info event)
Browse files Browse the repository at this point in the history
  • Loading branch information
jissereitsma committed Dec 13, 2023
1 parent 8a251c2 commit 5fdf8f4
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 18 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
51 changes: 34 additions & 17 deletions DataLayer/Event/AddShippingInfo.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,44 +3,52 @@
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;
}

/**
* @return string[]
*/
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)) {
Expand All @@ -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());

Check failure on line 74 in DataLayer/Event/AddShippingInfo.php

View workflow job for this annotation

GitHub Actions / PHPStan

Call to an undefined method Magento\Quote\Api\ShippingMethodManagementInterface::get().
if ($shippingMethod instanceof ShippingMethodInterface) {
return $shippingMethod->getCarrierCode().'_'.$shippingMethod->getMethodCode();
}
} catch (NoSuchEntityException $e) {
} catch (StateException $e) {
}

try {
return $quote->getShippingAddress()->getShippingMethod();

Check failure on line 83 in DataLayer/Event/AddShippingInfo.php

View workflow job for this annotation

GitHub Actions / PHPStan

Call to an undefined method Magento\Quote\Api\Data\CartInterface::getShippingAddress().
} catch (NoSuchEntityException $e) {
}

return $shippingMethod->getCarrierCode().'_'.$shippingMethod->getMethodCode();
return null;
}
}
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down

0 comments on commit 5fdf8f4

Please sign in to comment.