From 96e1ed9d5349d5deab611cb985a835666c0979fb Mon Sep 17 00:00:00 2001 From: James Flacks Date: Thu, 26 Oct 2023 12:51:08 +0100 Subject: [PATCH] Update AddConfigDataToCart.php fix 400 error on cart when quote hasn't been saved. --- Plugin/AddConfigDataToCart.php | 29 ++++++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/Plugin/AddConfigDataToCart.php b/Plugin/AddConfigDataToCart.php index ba75c840..bd462ba2 100644 --- a/Plugin/AddConfigDataToCart.php +++ b/Plugin/AddConfigDataToCart.php @@ -7,24 +7,51 @@ use CheckoutCom\Magento2\Gateway\Config\Config; use Magento\Checkout\CustomerData\Cart; use Magento\Checkout\Model\CompositeConfigProvider; +use Magento\Checkout\Model\Session as CheckoutSession; class AddConfigDataToCart { private Config $config; private CompositeConfigProvider $compositeConfigProvider; + private CheckoutSession $checkoutSession; + + /** + * @var \Magento\Quote\Model\Quote|null + */ + protected $quote = null; public function __construct( + \Magento\Checkout\Model\Session $checkoutSession, Config $config, CompositeConfigProvider $compositeConfigProvider ) { + $this->checkoutSession = $checkoutSession; $this->config = $config; $this->compositeConfigProvider = $compositeConfigProvider; } public function afterGetSectionData(Cart $subject, array $result): array { + $hasQuote = $this->getQuote() && $this->getQuote()->getId(); + if(!$hasQuote) { + // no quote yet, preventing 400 error on new session + return $result; + } $configProvider = ['checkoutConfigProvider' => $this->compositeConfigProvider->getConfig()]; - return array_merge($this->config->getMethodsConfig(), $configProvider, $result); + + } + + /** + * Get active quote + * + * @return \Magento\Quote\Model\Quote + */ + protected function getQuote() + { + if (null === $this->quote) { + $this->quote = $this->checkoutSession->getQuote(); + } + return $this->quote; } }