Skip to content

Commit 20cbe6b

Browse files
committed
MAGETWO-89034: Not Visible Custom Address Attributes Showing on Checkout
1 parent 2b1bf75 commit 20cbe6b

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
/**
@@ -323,11 +333,34 @@ private function getCustomerData()
323333
$customerData = $customer->__toArray();
324334
foreach ($customer->getAddresses() as $key => $address) {
325335
$customerData['addresses'][$key]['inline'] = $this->getCustomerAddressInline($address);
336+
if ($address->getCustomAttributes()) {
337+
$customerData['addresses'][$key]['custom_attributes'] = $this->filterNotVisibleAttributes(
338+
$customerData['addresses'][$key]['custom_attributes']
339+
);
340+
}
326341
}
327342
}
328343
return $customerData;
329344
}
330345

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

0 commit comments

Comments
 (0)