Skip to content

Commit

Permalink
Fatal error: Uncaught Error: Call to a member function getCarrierCode…
Browse files Browse the repository at this point in the history
…() on null in /vendor/yireo/magento2-googletagmanager2/DataLayer/Event/AddShippingInfo.php: 44
  • Loading branch information
bvmax committed Sep 26, 2023
1 parent 3c5123a commit 515e6f7
Showing 1 changed file with 19 additions and 2 deletions.
21 changes: 19 additions & 2 deletions DataLayer/Event/AddShippingInfo.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,7 @@ public function get(): array

if (!$shippingMethod) {
$quoteId = $this->checkoutSession->getQuote()->getId();
$shippingMethod = $this->shippingMethodManagement->get($quoteId);
$shippingMethod = $shippingMethod->getCarrierCode().'_'.$shippingMethod->getMethodCode();
$shippingMethod = $this->getShippingMethodFromQuote($quoteId);
}

if (!$shippingMethod) {
Expand All @@ -56,4 +55,22 @@ public function get(): array
],
];
}

/**
* Cart2Quote compatibility. When creating a quote there is no shipping info.
* shippingMethodManagement returns null and causes error on function getCarrierCode()
*
* @param int $quoteId
* @return string|null
*/
public function getShippingMethodFromQuote(int $quoteId): ?string
{
$shippingMethod = $this->shippingMethodManagement->get($quoteId);
if(!is_null($shippingMethod)) {

return $shippingMethod->getCarrierCode().'_'.$shippingMethod->getMethodCode();
}

return null;
}
}

0 comments on commit 515e6f7

Please sign in to comment.