Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 19 additions & 3 deletions Model/Convert/CartToFulfillmentAddress.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
3 changes: 1 addition & 2 deletions Model/Convert/CartToFulfillmentOptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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();
Expand Down
10 changes: 8 additions & 2 deletions Model/Mapping/Source/Category.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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();
}
}
37 changes: 34 additions & 3 deletions Service/CheckoutSessionService.php
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@ public function complete(string $sessionId, CompleteCheckoutSessionRequestInterf

if ($checkoutSessionsRequest->getPaymentData()->getBillingAddress()) {
$this->addBillingAddressToCart($cart, $checkoutSessionsRequest->getPaymentData()->getBillingAddress());

} else {
$this->copyShippingAddressToBillingAddress($cart);
}
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
}

Expand Down Expand Up @@ -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);
Expand All @@ -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());
}
}

/**
Expand All @@ -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()]);
Expand Down Expand Up @@ -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;
}
}