diff --git a/Model/Convert/CartToFulfillmentAddress.php b/Model/Convert/CartToFulfillmentAddress.php index 9f66083..5291e75 100644 --- a/Model/Convert/CartToFulfillmentAddress.php +++ b/Model/Convert/CartToFulfillmentAddress.php @@ -26,12 +26,28 @@ public function __construct( /** * @param Quote $cart - * @return AddressInterface + * @return AddressInterface|null */ - public function execute(Quote $cart): AddressInterface + public function execute(Quote $cart): ?AddressInterface { $shippingAddress = $cart->getShippingAddress(); - $name = $shippingAddress->getFirstname() . ' ' . $shippingAddress->getLastname(); + + // Check if required address fields are present + if (!$shippingAddress->getCity() + || !$shippingAddress->getCountry() + || !$shippingAddress->getPostcode() + || !$shippingAddress->getStreet() + ) { + return null; + } + + $name = trim($shippingAddress->getFirstname() . ' ' . $shippingAddress->getLastname()); + + // If name is empty, use a placeholder or return null + if (empty($name)) { + return null; + } + /** @var AddressInterface $address */ $address = $this->addressInterfaceFactory->create(); $address->setName($name); diff --git a/Model/Convert/CartToFulfillmentOptions.php b/Model/Convert/CartToFulfillmentOptions.php index e53cf30..3490e3f 100644 --- a/Model/Convert/CartToFulfillmentOptions.php +++ b/Model/Convert/CartToFulfillmentOptions.php @@ -15,7 +15,6 @@ use Magento\Quote\Model\Quote; use Magebit\AgenticCommerce\Model\Convert\ConvertPrice; use Magento\Quote\Api\ShippingMethodManagementInterface; -use Magento\Framework\Exception\LocalizedException; use Magento\Quote\Model\Cart\ShippingMethodConverter; use Magento\Quote\Api\Data\ShippingMethodInterface; @@ -70,7 +69,7 @@ public function getShippingMethods(Quote $cart): array { $shippingAddress = $cart->getShippingAddress(); if (!$shippingAddress->getCountryId()) { - throw new LocalizedException(__('The shipping address is missing. Set the address and try again.')); + return []; } $shippingAddress->collectShippingRates(); $shippingRates = $shippingAddress->getGroupedAllShippingRates(); diff --git a/Model/Mapping/Source/Category.php b/Model/Mapping/Source/Category.php index 4063da1..82b83fd 100644 --- a/Model/Mapping/Source/Category.php +++ b/Model/Mapping/Source/Category.php @@ -16,6 +16,7 @@ use Magento\Catalog\Api\Data\ProductInterface; use Magento\Catalog\Api\CategoryRepositoryInterface; use Magento\Catalog\Model\Product; +use Magento\Framework\Exception\NoSuchEntityException; class Category implements SourceInterface { @@ -36,9 +37,14 @@ public function getValue(ProductInterface $product, ?ProductInterface $parentPro { /** @var Product $product */ $categoryIds = $product->getCategoryIds(); - $category = array_shift($categoryIds); - return $this->categoryRepository->get($category, $product->getStoreId())->getName(); + try { + $category = $this->categoryRepository->get($category, $product->getStoreId()); + } catch (NoSuchEntityException $e) { + return null; + } + + return $category->getName(); } } diff --git a/Service/CheckoutSessionService.php b/Service/CheckoutSessionService.php index d52b964..f9701ea 100644 --- a/Service/CheckoutSessionService.php +++ b/Service/CheckoutSessionService.php @@ -174,6 +174,7 @@ public function complete(string $sessionId, CompleteCheckoutSessionRequestInterf if ($checkoutSessionsRequest->getPaymentData()->getBillingAddress()) { $this->addBillingAddressToCart($cart, $checkoutSessionsRequest->getPaymentData()->getBillingAddress()); + } else { $this->copyShippingAddressToBillingAddress($cart); } @@ -314,7 +315,11 @@ public function assignCartDataToResponse(CartInterface $cart, CheckoutSessionRes $currency = $cart->getCurrency()?->getStoreCurrencyCode(); $response->setLineItems($lineItems); - $response->setFulfillmentAddress($fulfillmentAddress); + + if ($fulfillmentAddress) { + $response->setFulfillmentAddress($fulfillmentAddress); + } + $response->setTotals($totals); $response->setFulfillmentOptions($fulfillmentOptions); $response->setPaymentProvider($paymentProvider); @@ -422,8 +427,13 @@ public function getLinks(): array { $linksConfig = $this->config->getCheckoutSessionLinks(); - return array_map(function ($link) { - return $this->linkInterfaceFactory->create(['data' => $link]); + return array_map(function (array $link) : LinkInterface { + $linkData = [ + 'type' => $link['type'], + 'url' => $link['link'] + ]; + + return $this->linkInterfaceFactory->create(['data' => $linkData]); }, $linksConfig); } @@ -477,6 +487,10 @@ public function addFulfillmentAddressToCart(CartInterface $cart, AddressInterfac $this->addDataToQuoteAddress($shippingAddress, $address); if (!$cart->getCustomerFirstname() || !$cart->getCustomerLastname()) { + if (!$this->isValidName($address->getName())) { + return; + } + [$firstName, $lastName] = explode(' ', $address->getName(), 2); $cart->setCustomerFirstname($firstName); $cart->setCustomerLastname($lastName); @@ -493,6 +507,10 @@ public function addBillingAddressToCart(CartInterface $cart, AddressInterface $a /** @var Quote $cart */ $billingAddress = $cart->getBillingAddress(); $this->addDataToQuoteAddress($billingAddress, $address); + + if ($billingAddress && !$billingAddress->getTelephone() && $cart->getShippingAddress()) { + $billingAddress->setTelephone($cart->getShippingAddress()->getTelephone()); + } } /** @@ -516,6 +534,10 @@ protected function setCartEmailAddress(CartInterface $cart): void */ protected function addDataToQuoteAddress(QuoteAddressInterface $cartAddress, AddressInterface $address): void { + if (!$this->isValidName($address->getName())) { + return; + } + [$firstName, $lastName] = explode(' ', $address->getName(), 2); $street = array_filter([$address->getLineOne(), $address->getLineTwo()]); @@ -549,4 +571,13 @@ protected function copyShippingAddressToBillingAddress(CartInterface $cart): voi $billingAddress->setTelephone($shippingAddress->getTelephone()); $billingAddress->setEmail($shippingAddress->getEmail()); } + + /** + * @param string|null $name + * @return bool + */ + protected function isValidName(?string $name): bool + { + return !empty($name) && strpos($name, ' ') !== false; + } }