1

We are using Magento 2 Enterprise Edition and created some of the custom customer attributes via admin panel (it's one of the feature in enterprise edition though it's not available in community edition).

I can see all the new custom attributes in frontend. However, when I fill in the data and try to save, it's not getting saving but when I do the same process that is saving the data via the admin panel, it's saving without any issues.

So why am I not able to save the data from the frontend?

Any help will be appreciated!

<?php 
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$customerSession = $objectManager->create('Magento\Customer\Model\Session');
$customergroupid = '';
if ($customerSession->isLoggedIn()) {
     $customergroupid = $customerSession->getCustomer()->getGroupId();    
}
?>
<?php if(($customergroupid == x) || ($customergroupid == y)): ?>
<?php if ($block->hasUserDefinedAttributes()): ?>
    <?php if ($block->isShowContainer()): ?>
    <fieldset class="fieldset">
    <?php endif;?>
    <?php foreach ($block->getUserDefinedAttributes() as $attribute):?> 
        <?php $attributeContent = $block->getAttributeHtml($attribute);?>
        <?php if ($attributeContent): ?>
            <?= /* @escapeNotVerified */ $attributeContent ?>
        <?php endif;?>
    <?php endforeach;?>
    <?php if ($block->isShowContainer()): ?>
    </fieldset>
    <?php endif;?>
<?php endif; ?>
<?php endif; ?>
6
  • How you create custom attribute? Mar 3, 2020 at 13:47
  • In enterprise edition, we have an option to create customer attributes which is not available in community edition. Pleased see the screenshot in question
    – MagentoDev
    Mar 3, 2020 at 13:49
  • I am asking the value which you field during creation of the attibute. Mar 3, 2020 at 13:51
  • I have created an attribute called Marital Status and selected the option Yes for show on frontend. But still its not displaying.
    – MagentoDev
    Mar 3, 2020 at 14:05
  • But I can see that attribute in backend
    – MagentoDev
    Mar 3, 2020 at 14:09

2 Answers 2

2
+100

EE provides two set of attributes.

Customer and Customer Address Attributes attributes.

As per question, it looks like you have created Martial status attribute typed drop-down on the Customer section.

I can see all the new custom attributes in frontend. However, when I fill in the data and try to save, its not getting saving but when I do the same process that is saving the data via admin panel, its saving without any issues.

If you are facing an issue(fill in the data) while Registration or Edit account page, there may be something custom code breaking on customer_save related events.

As i have checked the boolean, drop-down and text field type attributes works very well and reflected in the Admin as well as Front-end section.

  • Used in forms should be selected as all the options

Custom and Customer address attributes are native feature, even you don't have to put any template whether in the admin address template or customer related templates files.

Only needs to create the attribute from the Magento admin.

EDIT:2 FOR CUSTOMER GROUP ID

Copy file vendor/magento/module-customer-custom-attributes/view/frontend/templates/form/userattributes.phtml and paste into the theme folder.

So file location would be follows: app/design/frontend/Namespace/Themename/Magento_CustomerCustomAttributes/templates/customer/form/userattributes.phtml

Above file will be using the class -

$block \Magento\CustomerCustomAttributes\Block\Form and

$block \Magento\CustomAttributeManagement\Block\Form

FILE: userattributes.phtml

<?php 
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$customerSession = $objectManager->create('Magento\Customer\Model\Session');
$customergroupid = '';
if ($customerSession->isLoggedIn()) {
     $customergroupid = $customerSession->getCustomer()->getGroupId();    
}

$allowedAttributes = array('allowed_for_retailer','maritial_status');
?>

<?php foreach ($block->getUserDefinedAttributes() as $attribute):?>
        <?php $attributeContent = $block->getAttributeHtml($attribute);?>
        <?php $code = $attribute->getCode('allowed_for_retailer');?>

        //Checking if attribute is in allowed list or not.
        <?php if(in_array($code, $allowedAttributes)): ?>
            <?php if ($attributeContent): ?>
                <?= /* @escapeNotVerified */ $attributeContent ?>
            <?php endif;?>
        <?php endif;?>        
<?php endforeach;?>
4
  • 1
    Thanks for the detailed information. When I try it in local, it wasn't working but in the server its, working fine. Apart from this, I'm having one query that as of now, whatever attributes I'm creating via admin, they are displaying in all the customer groups but I don't want to display some customer attribute in some customer group. I have the code and put it in question. Can you please help me on that?
    – MagentoDev
    Mar 10, 2020 at 7:27
  • @Siva - Check my updated answer. It may help
    – Softec
    Mar 10, 2020 at 14:53
  • 1
    Thanks. I'm mark this as answer
    – MagentoDev
    Mar 11, 2020 at 13:26
  • 1
    Glad to help you. Thanks
    – Softec
    Mar 11, 2020 at 16:49
0

if you are trying to display Marital Status customer attribute in address of the customer you need to change it in store->configurations->customers->configurations and Address Templates

{{depend attribute_code}}Marital Status: {{var attribute_code}}
2
  • Thanks. Could you please explain a bit?
    – MagentoDev
    Mar 3, 2020 at 16:26
  • If you want to show customer attribute on frontend addresses shipping and billing address you need to update address templates in backend Mar 3, 2020 at 18:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.