Skip to content

Commit 63ac4bf

Browse files
committed
MAGETWO-91673: Not Visible Custom Address Attributes Showing on Checkout
1 parent 3d53578 commit 63ac4bf

File tree

1 file changed

+34
-1
lines changed

1 file changed

+34
-1
lines changed

app/code/Magento/Checkout/Model/DefaultConfigProvider.php

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,14 @@
88
use Magento\Catalog\Helper\Product\ConfigurationPool;
99
use Magento\Checkout\Helper\Data as CheckoutHelper;
1010
use Magento\Checkout\Model\Session as CheckoutSession;
11+
use Magento\Customer\Api\AddressMetadataInterface;
1112
use Magento\Customer\Api\CustomerRepositoryInterface as CustomerRepository;
1213
use Magento\Customer\Model\Context as CustomerContext;
1314
use Magento\Customer\Model\Session as CustomerSession;
1415
use Magento\Customer\Model\Url as CustomerUrlManager;
1516
use Magento\Framework\App\Config\ScopeConfigInterface;
1617
use Magento\Framework\App\Http\Context as HttpContext;
18+
use Magento\Framework\App\ObjectManager;
1719
use Magento\Framework\Data\Form\FormKey;
1820
use Magento\Framework\Locale\FormatInterface as LocaleFormat;
1921
use Magento\Framework\UrlInterface;
@@ -159,6 +161,11 @@ class DefaultConfigProvider implements ConfigProviderInterface
159161
*/
160162
protected $urlBuilder;
161163

164+
/**
165+
* @var AddressMetadataInterface
166+
*/
167+
private $addressMetadata;
168+
162169
/**
163170
* @param CheckoutHelper $checkoutHelper
164171
* @param Session $checkoutSession
@@ -186,6 +193,7 @@ class DefaultConfigProvider implements ConfigProviderInterface
186193
* @param \Magento\Store\Model\StoreManagerInterface $storeManager
187194
* @param \Magento\Quote\Api\PaymentMethodManagementInterface $paymentMethodManagement
188195
* @param UrlInterface $urlBuilder
196+
* @param AddressMetadataInterface $addressMetadata
189197
* @codeCoverageIgnore
190198
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
191199
*/
@@ -215,7 +223,8 @@ public function __construct(
215223
\Magento\Shipping\Model\Config $shippingMethodConfig,
216224
\Magento\Store\Model\StoreManagerInterface $storeManager,
217225
\Magento\Quote\Api\PaymentMethodManagementInterface $paymentMethodManagement,
218-
UrlInterface $urlBuilder
226+
UrlInterface $urlBuilder,
227+
AddressMetadataInterface $addressMetadata = null
219228
) {
220229
$this->checkoutHelper = $checkoutHelper;
221230
$this->checkoutSession = $checkoutSession;
@@ -243,6 +252,7 @@ public function __construct(
243252
$this->storeManager = $storeManager;
244253
$this->paymentMethodManagement = $paymentMethodManagement;
245254
$this->urlBuilder = $urlBuilder;
255+
$this->addressMetadata = $addressMetadata ?: ObjectManager::getInstance()->get(AddressMetadataInterface::class);
246256
}
247257

248258
/**
@@ -324,11 +334,34 @@ private function getCustomerData()
324334
$customerData = $customer->__toArray();
325335
foreach ($customer->getAddresses() as $key => $address) {
326336
$customerData['addresses'][$key]['inline'] = $this->getCustomerAddressInline($address);
337+
if ($address->getCustomAttributes()) {
338+
$customerData['addresses'][$key]['custom_attributes'] = $this->filterNotVisibleAttributes(
339+
$customerData['addresses'][$key]['custom_attributes']
340+
);
341+
}
327342
}
328343
}
329344
return $customerData;
330345
}
331346

347+
/**
348+
* Filter not visible on storefront custom attributes.
349+
*
350+
* @param array $attributes
351+
* @return array
352+
*/
353+
private function filterNotVisibleAttributes(array $attributes)
354+
{
355+
$attributesMetadata = $this->addressMetadata->getAllAttributesMetadata();
356+
foreach ($attributesMetadata as $attributeMetadata) {
357+
if (!$attributeMetadata->isVisible()) {
358+
unset($attributes[$attributeMetadata->getAttributeCode()]);
359+
}
360+
}
361+
362+
return $attributes;
363+
}
364+
332365
/**
333366
* Set additional customer address data
334367
*

0 commit comments

Comments
 (0)