| Current Path : /var/www/www.indacotrentino.com/www/app/code/Torresani/Indaco/Model/ |
| Current File : //var/www/www.indacotrentino.com/www/app/code/Torresani/Indaco/Model/QuoteAddressValidator.php |
<?php
namespace Torresani\Indaco\Model;
use Magento\Customer\Api\AddressRepositoryInterface;
use Magento\Customer\Api\CustomerRepositoryInterface;
use Magento\Customer\Model\Session;
use Magento\Framework\Exception\InputException;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Quote\Api\Data\AddressInterface;
use Magento\Quote\Api\Data\CartInterface;
class QuoteAddressValidator extends \Magento\Quote\Model\QuoteAddressValidator
{
/**
* Validate address to be used for cart.
*
* @param \Magento\Quote\Api\Data\CartInterface $cart
* @param \Magento\Quote\Api\Data\AddressInterface $address
* @return void
* @throws InputException The specified address belongs to another customer.
* @throws NoSuchEntityException|LocalizedException The specified customer ID or address ID is not valid.
*/
public function validateForCart(\Magento\Quote\Api\Data\CartInterface $cart, \Magento\Quote\Api\Data\AddressInterface $address): void
{
if ($cart->getCustomer() && $cart->getCustomer()->getId()) {
$cart->setCustomerIsGuest(0);
}
$this->doValidate($address, $cart->getCustomerIsGuest() ? null : (int) $cart->getCustomer()->getId());
/*
if ($cart->getCustomerIsGuest()) {
$this->doValidateForGuestQuoteAddress($address, $cart);
}
$this->doValidate($address, $cart->getCustomerIsGuest() ? null : (int) $cart->getCustomer()->getId());
*/
}
/**
* Validate address.
*
* @param AddressInterface $address
* @param int|null $customerId
* @return void
* @throws LocalizedException The specified customer ID or address ID is not valid.
* @throws NoSuchEntityException The specified customer ID or address ID is not valid.
*/
private function doValidate(AddressInterface $address, ?int $customerId): void
{
//validate customer id
if ($customerId) {
$customer = $this->customerRepository->getById($customerId);
if (!$customer->getId()) {
throw new NoSuchEntityException(
__('Invalid customer id %1', $customerId)
);
}
}
if ($address->getCustomerAddressId()) {
//Existing address cannot belong to a guest
if (!$customerId) {
throw new NoSuchEntityException(
__('Invalid customer address id %1', $address->getCustomerAddressId())
);
}
//Validating address ID
try {
$this->addressRepository->getById($address->getCustomerAddressId());
} catch (NoSuchEntityException $e) {
throw new NoSuchEntityException(
__('Invalid address id %1', $address->getId())
);
}
//Finding available customer's addresses
$applicableAddressIds = array_map(function ($address) {
/** @var \Magento\Customer\Api\Data\AddressInterface $address */
return $address->getId();
}, $this->customerRepository->getById($customerId)->getAddresses());
if (!in_array($address->getCustomerAddressId(), $applicableAddressIds)) {
throw new NoSuchEntityException(
__('Invalid customer address id %1', $address->getCustomerAddressId())
);
}
}
}
}