From 42e5fd2c579b759b39a5016ced50d6ebf33ff7c7 Mon Sep 17 00:00:00 2001 From: Mykola Silin Date: Sun, 25 Feb 2024 18:40:35 +0200 Subject: [PATCH 1/3] 38325 make city field optional in address --- .../web/template/billing-address/details.html | 6 +- .../address-renderer/default.html | 5 +- .../address-renderer/default.html | 5 +- .../Magento/Customer/Block/Widget/City.php | 181 ++++++++++++++++++ .../Model/Address/Validator/General.php | 21 +- .../AdminCustomerShowCityActionGroup.xml | 26 +++ .../Mftf/Data/AdminCustomerConfigData.xml | 5 + .../Section/AdminCustomerConfigSection.xml | 1 + .../Magento/Customer/etc/adminhtml/system.xml | 5 + app/code/Magento/Customer/etc/config.xml | 7 +- .../ui_component/customer_address_form.xml | 3 - .../frontend/templates/address/edit.phtml | 46 ++--- .../frontend/templates/form/register.phtml | 42 ++-- .../view/frontend/templates/widget/city.phtml | 31 +++ 14 files changed, 329 insertions(+), 55 deletions(-) create mode 100644 app/code/Magento/Customer/Block/Widget/City.php create mode 100644 app/code/Magento/Customer/Test/Mftf/ActionGroup/AdminCustomerShowCityActionGroup.xml create mode 100644 app/code/Magento/Customer/view/frontend/templates/widget/city.phtml diff --git a/app/code/Magento/Checkout/view/frontend/web/template/billing-address/details.html b/app/code/Magento/Checkout/view/frontend/web/template/billing-address/details.html index 9005e4816b43f..ef0e3b610e2ca 100644 --- a/app/code/Magento/Checkout/view/frontend/web/template/billing-address/details.html +++ b/app/code/Magento/Checkout/view/frontend/web/template/billing-address/details.html @@ -12,7 +12,11 @@

- , + + + , + +


diff --git a/app/code/Magento/Checkout/view/frontend/web/template/shipping-address/address-renderer/default.html b/app/code/Magento/Checkout/view/frontend/web/template/shipping-address/address-renderer/default.html index c5cc997d8cc85..0f7da2b01f58e 100644 --- a/app/code/Magento/Checkout/view/frontend/web/template/shipping-address/address-renderer/default.html +++ b/app/code/Magento/Checkout/view/frontend/web/template/shipping-address/address-renderer/default.html @@ -11,7 +11,10 @@

- ,
+ + , + +


diff --git a/app/code/Magento/Checkout/view/frontend/web/template/shipping-information/address-renderer/default.html b/app/code/Magento/Checkout/view/frontend/web/template/shipping-information/address-renderer/default.html index 292edbc13708c..21e1e430cf5b6 100644 --- a/app/code/Magento/Checkout/view/frontend/web/template/shipping-information/address-renderer/default.html +++ b/app/code/Magento/Checkout/view/frontend/web/template/shipping-information/address-renderer/default.html @@ -11,7 +11,10 @@

- ,
+ + , + +


diff --git a/app/code/Magento/Customer/Block/Widget/City.php b/app/code/Magento/Customer/Block/Widget/City.php new file mode 100644 index 0000000000000..26f32634880c0 --- /dev/null +++ b/app/code/Magento/Customer/Block/Widget/City.php @@ -0,0 +1,181 @@ +options = $options; + parent::__construct($context, $addressHelper, $customerMetadata, $data); + $this->addressMetadata = $addressMetadata; + $this->_isScopePrivate = true; + } + + /** + * @return void + */ + public function _construct() + { + parent::_construct(); + + // default city location + $this->setTemplate('Magento_Customer::widget/city.phtml'); + } + + /** + * Can show config value + * + * @param string $key + * + * @return bool + */ + protected function _showConfig($key) + { + return (bool)$this->getConfig($key); + } + + /** + * Can show prefix + * + * @return bool + */ + public function showCity() + { + return $this->_isAttributeVisible(self::ATTRIBUTE_CODE); + } + + /** + * @inheritdoc + */ + protected function _getAttribute($attributeCode) + { + if ($this->getForceUseCustomerAttributes() || $this->getObject() instanceof CustomerInterface) { + return parent::_getAttribute($attributeCode); + } + + try { + $attribute = $this->addressMetadata->getAttributeMetadata($attributeCode); + } catch (\Magento\Framework\Exception\NoSuchEntityException $e) { + return null; + } + + if ($this->getForceUseCustomerRequiredAttributes() && $attribute && !$attribute->isRequired()) { + $customerAttribute = parent::_getAttribute($attributeCode); + if ($customerAttribute && $customerAttribute->isRequired()) { + $attribute = $customerAttribute; + } + } + + return $attribute; + } + + /** + * Retrieve store attribute label + * + * @param string $attributeCode + * + * @return string + */ + public function getStoreLabel(string $attributeCode) + { + $attribute = $this->_getAttribute($attributeCode); + return $attribute ? __($attribute->getStoreLabel()) : ''; + } + + /** + * Get string with frontend validation classes for attribute + * + * @param string $attributeCode + * + * @return string + * @throws LocalizedException + */ + public function getAttributeValidationClass(string $attributeCode) + { + return $this->_addressHelper->getAttributeValidationClass($attributeCode); + } + + /** + * @param string $attributeCode + * + * @return bool + */ + private function _isAttributeVisible(string $attributeCode) + { + $attributeMetadata = $this->_getAttribute($attributeCode); + return $attributeMetadata ? (bool)$attributeMetadata->isVisible() : false; + } + + /** + * Check if city attribute enabled in system + * + * @return bool + */ + public function isEnabled() + { + return $this->_getAttribute(self::ATTRIBUTE_CODE) + ? (bool)$this->_getAttribute(self::ATTRIBUTE_CODE)->isVisible() : false; + } + + /** + * Check if city attribute marked as required + * + * @return bool + */ + public function isRequired(): bool + { + return $this->_getAttribute(self::ATTRIBUTE_CODE) + ? (bool)$this->_getAttribute(self::ATTRIBUTE_CODE)->isRequired() : false; + } +} diff --git a/app/code/Magento/Customer/Model/Address/Validator/General.php b/app/code/Magento/Customer/Model/Address/Validator/General.php index 23c6d687328f3..61d79c65bbaf4 100644 --- a/app/code/Magento/Customer/Model/Address/Validator/General.php +++ b/app/code/Magento/Customer/Model/Address/Validator/General.php @@ -75,10 +75,6 @@ private function checkRequiredFields(AbstractAddress $address) $errors[] = __('"%fieldName" is required. Enter and try again.', ['fieldName' => 'street']); } - if (!ValidatorChain::is($address->getCity(), NotEmpty::class)) { - $errors[] = __('"%fieldName" is required. Enter and try again.', ['fieldName' => 'city']); - } - return $errors; } @@ -111,6 +107,12 @@ private function checkOptionalFields(AbstractAddress $address) $errors[] = __('"%fieldName" is required. Enter and try again.', ['fieldName' => 'company']); } + if ($this->isCityRequired() + && !ValidatorChain::is($address->getCity(), NotEmpty::class)) + { + $errors[] = __('"%fieldName" is required. Enter and try again.', ['fieldName' => 'city']); + } + $havingOptionalZip = $this->directoryData->getCountriesWithOptionalZip(); if (!in_array($address->getCountryId(), $havingOptionalZip) && !ValidatorChain::is($address->getPostcode(), NotEmpty::class) @@ -154,6 +156,17 @@ private function isFaxRequired() return $this->eavConfig->getAttribute('customer_address', 'fax')->getIsRequired(); } + /** + * Check if city field required in configuration. + * + * @return bool + * @throws LocalizedException + */ + private function isCityRequired() + { + return $this->eavConfig->getAttribute('customer_address', 'city')->getIsRequired(); + } + /** * Reload address attributes for the certain store * diff --git a/app/code/Magento/Customer/Test/Mftf/ActionGroup/AdminCustomerShowCityActionGroup.xml b/app/code/Magento/Customer/Test/Mftf/ActionGroup/AdminCustomerShowCityActionGroup.xml new file mode 100644 index 0000000000000..fbb429a8ac5bd --- /dev/null +++ b/app/code/Magento/Customer/Test/Mftf/ActionGroup/AdminCustomerShowCityActionGroup.xml @@ -0,0 +1,26 @@ + + + + + + + Goes to the customer configuration. Set "Show City" with provided value. + + + + + + + + + + + + + diff --git a/app/code/Magento/Customer/Test/Mftf/Data/AdminCustomerConfigData.xml b/app/code/Magento/Customer/Test/Mftf/Data/AdminCustomerConfigData.xml index 47b37a4a8e264..8a9690a88c86f 100644 --- a/app/code/Magento/Customer/Test/Mftf/Data/AdminCustomerConfigData.xml +++ b/app/code/Magento/Customer/Test/Mftf/Data/AdminCustomerConfigData.xml @@ -18,6 +18,11 @@ Optional Required + + No + Optional + Required + No Optional diff --git a/app/code/Magento/Customer/Test/Mftf/Section/AdminCustomerConfigSection.xml b/app/code/Magento/Customer/Test/Mftf/Section/AdminCustomerConfigSection.xml index 671bdde86432f..28dc746a122c3 100644 --- a/app/code/Magento/Customer/Test/Mftf/Section/AdminCustomerConfigSection.xml +++ b/app/code/Magento/Customer/Test/Mftf/Section/AdminCustomerConfigSection.xml @@ -17,6 +17,7 @@ + diff --git a/app/code/Magento/Customer/etc/adminhtml/system.xml b/app/code/Magento/Customer/etc/adminhtml/system.xml index ec76e09fdf459..e43944b1c7c54 100644 --- a/app/code/Magento/Customer/etc/adminhtml/system.xml +++ b/app/code/Magento/Customer/etc/adminhtml/system.xml @@ -266,6 +266,11 @@ Magento\Config\Model\Config\Source\Nooptreq Magento\Customer\Model\Config\Backend\Show\AddressOnly + + + Magento\Config\Model\Config\Source\Nooptreq + Magento\Customer\Model\Config\Backend\Show\AddressOnly + diff --git a/app/code/Magento/Customer/etc/config.xml b/app/code/Magento/Customer/etc/config.xml index 23a7c9ebb4034..53e981f6acf19 100644 --- a/app/code/Magento/Customer/etc/config.xml +++ b/app/code/Magento/Customer/etc/config.xml @@ -59,6 +59,7 @@ req opt + opt 0 @@ -71,7 +72,7 @@ {{depend street2}}{{var street2}}{{/depend}} {{depend street3}}{{var street3}}{{/depend}} {{depend street4}}{{var street4}}{{/depend}} -{{if city}}{{var city}}, {{/if}}{{if region}}{{var region}}, {{/if}}{{if postcode}}{{var postcode}}{{/if}} +{{depend city}}{{var city}},{{/depend}} {{if region}}{{var region}}, {{/if}}{{if postcode}}{{var postcode}}{{/if}} {{var country}} {{depend telephone}}T: {{var telephone}}{{/depend}} {{depend fax}}F: {{var fax}}{{/depend}} @@ -83,7 +84,7 @@ {{depend street2}}{{var street2}}
{{/depend}} {{depend street3}}{{var street3}}
{{/depend}} {{depend street4}}{{var street4}}
{{/depend}} -{{if city}}{{var city}}, {{/if}}{{if region}}{{var region}}, {{/if}}{{if postcode}}{{var postcode}}{{/if}}
+{{depend city}}{{var city}},{{/depend}} {{if region}}{{var region}}, {{/if}}{{if postcode}}{{var postcode}}{{/if}}
{{var country}}
{{depend telephone}}T: {{var telephone}}{{/depend}} {{depend fax}}
F: {{var fax}}{{/depend}} @@ -94,7 +95,7 @@ {{depend street2}}{{var street2}}|{{/depend}} {{depend street3}}{{var street3}}|{{/depend}} {{depend street4}}{{var street4}}|{{/depend}} -{{if city}}{{var city}}, {{/if}}{{if region}}{{var region}}, {{/if}}{{if postcode}}{{var postcode}}{{/if}}| +{{depend city}}{{var city}},|{{/depend}} {{if region}}{{var region}}, {{/if}}{{if postcode}}{{var postcode}}{{/if}}| {{var country}}| {{depend telephone}}T: {{var telephone}}|{{/depend}} {{depend fax}}F: {{var fax}}|{{/depend}}| diff --git a/app/code/Magento/Customer/view/adminhtml/ui_component/customer_address_form.xml b/app/code/Magento/Customer/view/adminhtml/ui_component/customer_address_form.xml index 517d8151b5f10..ead67b3cbde34 100644 --- a/app/code/Magento/Customer/view/adminhtml/ui_component/customer_address_form.xml +++ b/app/code/Magento/Customer/view/adminhtml/ui_component/customer_address_form.xml @@ -207,9 +207,6 @@ text true - - true - diff --git a/app/code/Magento/Customer/view/frontend/templates/address/edit.phtml b/app/code/Magento/Customer/view/frontend/templates/address/edit.phtml index a958f831e18a8..e8fd07b106756 100644 --- a/app/code/Magento/Customer/view/frontend/templates/address/edit.phtml +++ b/app/code/Magento/Customer/view/frontend/templates/address/edit.phtml @@ -4,20 +4,30 @@ * See COPYING.txt for license details. */ -/** @var \Magento\Customer\Block\Address\Edit $block */ -/** @var \Magento\Customer\ViewModel\Address $viewModel */ -/** @var \Magento\Framework\Escaper $escaper */ -/** @var \Magento\Framework\View\Helper\SecureHtmlRenderer $secureRenderer */ -/** @var \Magento\Customer\ViewModel\Address\RegionProvider $regionProvider */ +use Magento\Customer\Block\Address\Edit; +use Magento\Customer\Block\Widget\City; +use Magento\Customer\Block\Widget\Company; +use Magento\Customer\Block\Widget\Fax; +use Magento\Customer\Block\Widget\Telephone; +use Magento\Customer\ViewModel\Address; +use Magento\Customer\ViewModel\Address\RegionProvider; +use Magento\Framework\Escaper; +use Magento\Framework\View\Helper\SecureHtmlRenderer; + +/** @var Edit $block */ +/** @var Address $viewModel */ +/** @var Escaper $escaper */ +/** @var SecureHtmlRenderer $secureRenderer */ +/** @var RegionProvider $regionProvider */ $viewModel = $block->getViewModel(); $regionProvider = $block->getRegionProvider(); ?> -getLayout()->createBlock(\Magento\Customer\Block\Widget\Company::class) ?> -getLayout()->createBlock(\Magento\Customer\Block\Widget\Telephone::class) ?> -getLayout()->createBlock(\Magento\Customer\Block\Widget\Fax::class) ?> +getLayout()->createBlock(Company::class) ?> +getLayout()->createBlock(Telephone::class) ?> +getLayout()->createBlock(Fax::class) ?> getAttributeData()->getFrontendLabel('country_id'); ?> getAttributeData()->getFrontendLabel('street'); ?> -getAttributeData()->getFrontendLabel('city'); ?> +getLayout()->createBlock(City::class); ?> getAttributeData()->getFrontendLabel('region'); ?> getConfig('general/region/display_all'); ?> @@ -138,19 +148,11 @@ $regionProvider = $block->getRegionProvider(); /> -
- -
- -
-
+ + isEnabled()): ?> + setCity($block->getAddress()->getCity())->toHtml() ?> + +
-
- -
- -
-
+ getLayout()->createBlock(City::class) ?> + isEnabled()): ?> + setCity($formData->getCity())->toHtml() ?> +