Skip to content

Commit 42e5fd2

Browse files
committed
38325 make city field optional in address
1 parent 078c387 commit 42e5fd2

File tree

14 files changed

+329
-55
lines changed

14 files changed

+329
-55
lines changed

app/code/Magento/Checkout/view/frontend/web/template/billing-address/details.html

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,11 @@
1212
<text args="currentBillingAddress().company"></text><br>
1313
</if>
1414
<text args="_.values(_.compact(currentBillingAddress().street)).join(', ')"></text><br>
15-
<text args="currentBillingAddress().city "></text>, <span text="currentBillingAddress().region"></span>
15+
16+
<if args="currentBillingAddress().city">
17+
<text args="currentBillingAddress().city "></text>,
18+
</if>
19+
<span text="currentBillingAddress().region"></span>
1620
<text args="currentBillingAddress().postcode"></text><br>
1721
<text args="getCountryName(currentBillingAddress().countryId)"></text><br>
1822
<a if="currentBillingAddress().telephone" attr="'href': 'tel:' + currentBillingAddress().telephone" text="currentBillingAddress().telephone"></a><br>

app/code/Magento/Checkout/view/frontend/web/template/shipping-address/address-renderer/default.html

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,10 @@
1111
<text args="address().company"></text><br>
1212
</if>
1313
<text args="_.values(_.compact(address().street)).join(', ')"></text><br>
14-
<text args="address().city "></text>, <span text="address().region"></span> <text args="address().postcode"></text><br>
14+
<if args="address().city">
15+
<text args="address().city "></text>,
16+
</if>
17+
<span text="address().region"></span> <text args="address().postcode"></text><br>
1518
<text args="getCountryName(address().countryId)"></text><br>
1619
<a if="address().telephone" attr="'href': 'tel:' + address().telephone" text="address().telephone"></a><br>
1720
<if args="address().vatId">

app/code/Magento/Checkout/view/frontend/web/template/shipping-information/address-renderer/default.html

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,10 @@
1111
<text args="address().company"></text><br>
1212
</if>
1313
<text args="_.values(_.compact(address().street)).join(', ')"></text><br>
14-
<text args="address().city "></text>, <span text="address().region"></span> <text args="address().postcode"></text><br>
14+
<if args="address().city">
15+
<text args="address().city "></text>,
16+
</if>
17+
<span text="address().region"></span> <text args="address().postcode"></text><br>
1518
<text args="getCountryName(address().countryId)"></text><br>
1619
<a if="address().telephone" attr="'href': 'tel:' + address().telephone" text="address().telephone"></a><br>
1720
<if args="address().vatId">
Lines changed: 181 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,181 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
namespace Magento\Customer\Block\Widget;
8+
9+
use Magento\Customer\Api\AddressMetadataInterface;
10+
use Magento\Customer\Api\CustomerMetadataInterface;
11+
use Magento\Customer\Api\Data\CustomerInterface;
12+
use Magento\Customer\Helper\Address as AddressHelper;
13+
use Magento\Customer\Model\Options;
14+
use Magento\Framework\Exception\LocalizedException;
15+
use Magento\Framework\View\Element\Template\Context;
16+
17+
/**
18+
* Widget for showing customer city.
19+
*
20+
* @method CustomerInterface getObject()
21+
* @method Name setObject(CustomerInterface $customer)
22+
*
23+
* @SuppressWarnings(PHPMD.DepthOfInheritance)
24+
*/
25+
class City extends AbstractWidget
26+
{
27+
28+
/**
29+
* the attribute code
30+
*/
31+
const ATTRIBUTE_CODE = 'city';
32+
33+
/**
34+
* @var AddressMetadataInterface
35+
*/
36+
protected $addressMetadata;
37+
38+
/**
39+
* @var Options
40+
*/
41+
protected $options;
42+
43+
/**
44+
* @param Context $context
45+
* @param AddressHelper $addressHelper
46+
* @param CustomerMetadataInterface $customerMetadata
47+
* @param Options $options
48+
* @param AddressMetadataInterface $addressMetadata
49+
* @param array $data
50+
*/
51+
public function __construct(
52+
Context $context,
53+
AddressHelper $addressHelper,
54+
CustomerMetadataInterface $customerMetadata,
55+
Options $options,
56+
AddressMetadataInterface $addressMetadata,
57+
array $data = []
58+
) {
59+
$this->options = $options;
60+
parent::__construct($context, $addressHelper, $customerMetadata, $data);
61+
$this->addressMetadata = $addressMetadata;
62+
$this->_isScopePrivate = true;
63+
}
64+
65+
/**
66+
* @return void
67+
*/
68+
public function _construct()
69+
{
70+
parent::_construct();
71+
72+
// default city location
73+
$this->setTemplate('Magento_Customer::widget/city.phtml');
74+
}
75+
76+
/**
77+
* Can show config value
78+
*
79+
* @param string $key
80+
*
81+
* @return bool
82+
*/
83+
protected function _showConfig($key)
84+
{
85+
return (bool)$this->getConfig($key);
86+
}
87+
88+
/**
89+
* Can show prefix
90+
*
91+
* @return bool
92+
*/
93+
public function showCity()
94+
{
95+
return $this->_isAttributeVisible(self::ATTRIBUTE_CODE);
96+
}
97+
98+
/**
99+
* @inheritdoc
100+
*/
101+
protected function _getAttribute($attributeCode)
102+
{
103+
if ($this->getForceUseCustomerAttributes() || $this->getObject() instanceof CustomerInterface) {
104+
return parent::_getAttribute($attributeCode);
105+
}
106+
107+
try {
108+
$attribute = $this->addressMetadata->getAttributeMetadata($attributeCode);
109+
} catch (\Magento\Framework\Exception\NoSuchEntityException $e) {
110+
return null;
111+
}
112+
113+
if ($this->getForceUseCustomerRequiredAttributes() && $attribute && !$attribute->isRequired()) {
114+
$customerAttribute = parent::_getAttribute($attributeCode);
115+
if ($customerAttribute && $customerAttribute->isRequired()) {
116+
$attribute = $customerAttribute;
117+
}
118+
}
119+
120+
return $attribute;
121+
}
122+
123+
/**
124+
* Retrieve store attribute label
125+
*
126+
* @param string $attributeCode
127+
*
128+
* @return string
129+
*/
130+
public function getStoreLabel(string $attributeCode)
131+
{
132+
$attribute = $this->_getAttribute($attributeCode);
133+
return $attribute ? __($attribute->getStoreLabel()) : '';
134+
}
135+
136+
/**
137+
* Get string with frontend validation classes for attribute
138+
*
139+
* @param string $attributeCode
140+
*
141+
* @return string
142+
* @throws LocalizedException
143+
*/
144+
public function getAttributeValidationClass(string $attributeCode)
145+
{
146+
return $this->_addressHelper->getAttributeValidationClass($attributeCode);
147+
}
148+
149+
/**
150+
* @param string $attributeCode
151+
*
152+
* @return bool
153+
*/
154+
private function _isAttributeVisible(string $attributeCode)
155+
{
156+
$attributeMetadata = $this->_getAttribute($attributeCode);
157+
return $attributeMetadata ? (bool)$attributeMetadata->isVisible() : false;
158+
}
159+
160+
/**
161+
* Check if city attribute enabled in system
162+
*
163+
* @return bool
164+
*/
165+
public function isEnabled()
166+
{
167+
return $this->_getAttribute(self::ATTRIBUTE_CODE)
168+
? (bool)$this->_getAttribute(self::ATTRIBUTE_CODE)->isVisible() : false;
169+
}
170+
171+
/**
172+
* Check if city attribute marked as required
173+
*
174+
* @return bool
175+
*/
176+
public function isRequired(): bool
177+
{
178+
return $this->_getAttribute(self::ATTRIBUTE_CODE)
179+
? (bool)$this->_getAttribute(self::ATTRIBUTE_CODE)->isRequired() : false;
180+
}
181+
}

app/code/Magento/Customer/Model/Address/Validator/General.php

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -75,10 +75,6 @@ private function checkRequiredFields(AbstractAddress $address)
7575
$errors[] = __('"%fieldName" is required. Enter and try again.', ['fieldName' => 'street']);
7676
}
7777

78-
if (!ValidatorChain::is($address->getCity(), NotEmpty::class)) {
79-
$errors[] = __('"%fieldName" is required. Enter and try again.', ['fieldName' => 'city']);
80-
}
81-
8278
return $errors;
8379
}
8480

@@ -111,6 +107,12 @@ private function checkOptionalFields(AbstractAddress $address)
111107
$errors[] = __('"%fieldName" is required. Enter and try again.', ['fieldName' => 'company']);
112108
}
113109

110+
if ($this->isCityRequired()
111+
&& !ValidatorChain::is($address->getCity(), NotEmpty::class))
112+
{
113+
$errors[] = __('"%fieldName" is required. Enter and try again.', ['fieldName' => 'city']);
114+
}
115+
114116
$havingOptionalZip = $this->directoryData->getCountriesWithOptionalZip();
115117
if (!in_array($address->getCountryId(), $havingOptionalZip)
116118
&& !ValidatorChain::is($address->getPostcode(), NotEmpty::class)
@@ -154,6 +156,17 @@ private function isFaxRequired()
154156
return $this->eavConfig->getAttribute('customer_address', 'fax')->getIsRequired();
155157
}
156158

159+
/**
160+
* Check if city field required in configuration.
161+
*
162+
* @return bool
163+
* @throws LocalizedException
164+
*/
165+
private function isCityRequired()
166+
{
167+
return $this->eavConfig->getAttribute('customer_address', 'city')->getIsRequired();
168+
}
169+
157170
/**
158171
* Reload address attributes for the certain store
159172
*
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
/**
4+
* Copyright © Magento, Inc. All rights reserved.
5+
* See COPYING.txt for license details.
6+
*/
7+
-->
8+
9+
<actionGroups xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
10+
xsi:noNamespaceSchemaLocation="urn:magento:mftf:Test/etc/actionGroupSchema.xsd">
11+
<actionGroup name="AdminCustomerShowCityActionGroup">
12+
<annotations>
13+
<description>Goes to the customer configuration. Set "Show City" with provided value.</description>
14+
</annotations>
15+
<arguments>
16+
<argument name="value" type="string" defaultValue="{{ShowCity.optional}}"/>
17+
</arguments>
18+
<amOnPage url="{{AdminCustomerConfigPage.url('#customer_address-link')}}" stepKey="openCustomerConfigPage"/>
19+
<waitForPageLoad stepKey="waitCustomerConfigPage"/>
20+
<scrollTo selector="{{AdminCustomerConfigSection.showCity}}" x="0" y="-100" stepKey="scrollToShowCity"/>
21+
<uncheckOption selector="{{AdminCustomerConfigSection.showCityInherit}}" stepKey="uncheckUseSystem"/>
22+
<selectOption selector="{{AdminCustomerConfigSection.showCity}}" userInput="{{value}}" stepKey="fillShowCity"/>
23+
<click selector="{{AdminMainActionsSection.save}}" stepKey="clickSave"/>
24+
<seeElement selector="{{AdminMessagesSection.success}}" stepKey="seeSuccessMessage"/>
25+
</actionGroup>
26+
</actionGroups>

app/code/Magento/Customer/Test/Mftf/Data/AdminCustomerConfigData.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,11 @@
1818
<data key="optional">Optional</data>
1919
<data key="required">Required</data>
2020
</entity>
21+
<entity name="ShowCity">
22+
<data key="no">No</data>
23+
<data key="optional">Optional</data>
24+
<data key="required">Required</data>
25+
</entity>
2126
<entity name="ShowCompany">
2227
<data key="no">No</data>
2328
<data key="optional">Optional</data>

app/code/Magento/Customer/Test/Mftf/Section/AdminCustomerConfigSection.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
<element name="showTelephoneInherit" type="checkbox" selector="#customer_address_telephone_show_inherit"/>
1818
<element name="showCompany" type="select" selector="#customer_address_company_show"/>
1919
<element name="showCompanyInherit" type="select" selector="#customer_address_company_show_inherit"/>
20+
<element name="showCity" type="select" selector="#customer_address_city_show"/>
2021
<element name="passwordOptions" type="select" selector="#customer_password-head"/>
2122
<element name="passwordResetProtectionType" type="select" selector="#customer_password_password_reset_protection_type"/>
2223
<element name="passwordResetProtectionTypeOptions" type="select" selector="#customer_password_password_reset_protection_type option:nth-child({{option}})" parameterized="true"/>

app/code/Magento/Customer/etc/adminhtml/system.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,11 @@
266266
<source_model>Magento\Config\Model\Config\Source\Nooptreq</source_model>
267267
<backend_model>Magento\Customer\Model\Config\Backend\Show\AddressOnly</backend_model>
268268
</field>
269+
<field id="city_show" translate="label" type="select" sortOrder="130" showInDefault="1" showInWebsite="1" canRestore="1">
270+
<label>Show City</label>
271+
<source_model>Magento\Config\Model\Config\Source\Nooptreq</source_model>
272+
<backend_model>Magento\Customer\Model\Config\Backend\Show\AddressOnly</backend_model>
273+
</field>
269274
</group>
270275
<group id="startup" translate="label" sortOrder="90" showInDefault="1" showInWebsite="1" showInStore="1">
271276
<label>Login Options</label>

app/code/Magento/Customer/etc/config.xml

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@
5959
<telephone_show>req</telephone_show>
6060
<company_show>opt</company_show>
6161
<fax_show/>
62+
<city_show>opt</city_show>
6263
</address>
6364
<startup>
6465
<redirect_dashboard>0</redirect_dashboard>
@@ -71,7 +72,7 @@
7172
{{depend street2}}{{var street2}}{{/depend}}
7273
{{depend street3}}{{var street3}}{{/depend}}
7374
{{depend street4}}{{var street4}}{{/depend}}
74-
{{if city}}{{var city}}, {{/if}}{{if region}}{{var region}}, {{/if}}{{if postcode}}{{var postcode}}{{/if}}
75+
{{depend city}}{{var city}},{{/depend}} {{if region}}{{var region}}, {{/if}}{{if postcode}}{{var postcode}}{{/if}}
7576
{{var country}}
7677
{{depend telephone}}T: {{var telephone}}{{/depend}}
7778
{{depend fax}}F: {{var fax}}{{/depend}}
@@ -83,7 +84,7 @@
8384
{{depend street2}}{{var street2}}<br />{{/depend}}
8485
{{depend street3}}{{var street3}}<br />{{/depend}}
8586
{{depend street4}}{{var street4}}<br />{{/depend}}
86-
{{if city}}{{var city}}, {{/if}}{{if region}}{{var region}}, {{/if}}{{if postcode}}{{var postcode}}{{/if}}<br />
87+
{{depend city}}{{var city}},{{/depend}} {{if region}}{{var region}}, {{/if}}{{if postcode}}{{var postcode}}{{/if}}<br />
8788
{{var country}}<br />
8889
{{depend telephone}}T: <a href="tel:{{var telephone}}">{{var telephone}}</a>{{/depend}}
8990
{{depend fax}}<br />F: {{var fax}}{{/depend}}
@@ -94,7 +95,7 @@
9495
{{depend street2}}{{var street2}}|{{/depend}}
9596
{{depend street3}}{{var street3}}|{{/depend}}
9697
{{depend street4}}{{var street4}}|{{/depend}}
97-
{{if city}}{{var city}}, {{/if}}{{if region}}{{var region}}, {{/if}}{{if postcode}}{{var postcode}}{{/if}}|
98+
{{depend city}}{{var city}},|{{/depend}} {{if region}}{{var region}}, {{/if}}{{if postcode}}{{var postcode}}{{/if}}|
9899
{{var country}}|
99100
{{depend telephone}}T: {{var telephone}}|{{/depend}}
100101
{{depend fax}}F: {{var fax}}|{{/depend}}|

app/code/Magento/Customer/view/adminhtml/ui_component/customer_address_form.xml

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -207,9 +207,6 @@
207207
<dataType>text</dataType>
208208
<label translate="true">City</label>
209209
<visible>true</visible>
210-
<validation>
211-
<rule name="required-entry" xsi:type="boolean">true</rule>
212-
</validation>
213210
</settings>
214211
</field>
215212
<field name="postcode" component="Magento_Ui/js/form/element/post-code" sortOrder="120" formElement="input">

0 commit comments

Comments
 (0)