How order magento 2 EE customer custom attributes?
-
12-01-2021 - |
Question
I'm using magento 2.1.7 EE and customer custom attributes creation in built, so I have added three attributes customer_title, customer_type and industry_type
So I want to display all three categories separately in my desired places. Still didn't find a way to show it.
Following section shows the all customer custom attributes together.
<?php $customerAttributes = $block->getChildBlock('customer_form_user_attributes'); ?>
<?php if ($customerAttributes): ?>
<?php $customerAttributes->setEntityType('customer')->setShowContainer(false);?>
<?php $block->restoreSessionData($customerAttributes->getMetadataForm());?>
<?php echo $customerAttributes->toHtml()?>
<?php endif;?>
Need help.
Solution
I'm waiting 5 days for an answer, finally I found a shortcut. So I think better to post it here.
This will helpful to developer who needs to customize the register post.
Register page show Customer attribute as follows,
$customerAttributes = $block->getChildBlock('customer_form_user_attributes');
if ($customerAttributes):
$customerAttributes->setEntityType('customer')->setShowContainer(false);
$block->restoreSessionData($customerAttributes->getMetadataForm());
echo $customerAttributes->toHtml(); // Here display the all customer attributes
endif;
So I found the getUserDefinedAttributes() method checking the module block,
then modified the code as,
if ($customerAttributes):
// Read the all customer attributes.
foreach ($customerAttributes->getUserDefinedAttributes() as $attribute):
// Check the attribute code and I want to show customer_title so filter it using if.
if($attribute->getAttributeCode() == "customer_title"):
// get attribute content using filtered $attribute
$attributeContent = $customerAttributes->getAttributeHtml($attribute);
if ($attributeContent):
echo $attributeContent; // Display customer_title attribute content
endif;
endif;
endforeach;
endif;
This helps for customer address attributes also.
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange