Skip to content

Commit

Permalink
magento#1029 Add Postcode as required depending of the country Mutati…
Browse files Browse the repository at this point in the history
…on createCustomerAddress
  • Loading branch information
osrecio committed Oct 27, 2019
1 parent 68d2137 commit 676cd1b
Showing 1 changed file with 17 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@
use Magento\Customer\Api\AddressRepositoryInterface;
use Magento\Customer\Api\Data\AddressInterface;
use Magento\Customer\Api\Data\AddressInterfaceFactory;
use Magento\Directory\Helper\Data as DirectoryData;
use Magento\Framework\Api\DataObjectHelper;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
use Magento\Framework\Api\DataObjectHelper;

/**
* Create customer address
Expand All @@ -38,23 +39,30 @@ class CreateCustomerAddress
* @var DataObjectHelper
*/
private $dataObjectHelper;
/**
* @var DirectoryData
*/
private $directoryData;

/**
* @param GetAllowedAddressAttributes $getAllowedAddressAttributes
* @param AddressInterfaceFactory $addressFactory
* @param AddressRepositoryInterface $addressRepository
* @param DataObjectHelper $dataObjectHelper
* @param DirectoryData $directoryData
*/
public function __construct(
GetAllowedAddressAttributes $getAllowedAddressAttributes,
AddressInterfaceFactory $addressFactory,
AddressRepositoryInterface $addressRepository,
DataObjectHelper $dataObjectHelper
DataObjectHelper $dataObjectHelper,
DirectoryData $directoryData
) {
$this->getAllowedAddressAttributes = $getAllowedAddressAttributes;
$this->addressFactory = $addressFactory;
$this->addressRepository = $addressRepository;
$this->dataObjectHelper = $dataObjectHelper;
$this->directoryData = $directoryData;
}

/**
Expand Down Expand Up @@ -102,6 +110,13 @@ public function validateData(array $addressData): void
$attributes = $this->getAllowedAddressAttributes->execute();
$errorInput = [];

//Add error for empty postcode with country with no optional ZIP
if (!in_array($addressData['country_id'], $this->directoryData->getCountriesWithOptionalZip())
&& (!isset($addressData['postcode']) || empty($addressData['postcode']))
) {
$errorInput[] = 'postcode';
}

foreach ($attributes as $attributeName => $attributeInfo) {
if ($attributeInfo->getIsRequired()
&& (!isset($addressData[$attributeName]) || empty($addressData[$attributeName]))
Expand Down

0 comments on commit 676cd1b

Please sign in to comment.