0

I have created 4 custom attributes attributes and i can see them in the register.phtml and i can create new customers but my question how to show those attributes in the edit account like first and last name and how to save them when submitting the form any help please Update here is the code that i used to create my ncustom attributes:

    <?php
namespace Mdweb\Inscription\Setup;

use Magento\Customer\Setup\CustomerSetupFactory;
use Magento\Customer\Model\Customer;
use Magento\Eav\Model\Entity\Attribute\Set as AttributeSet;
use Magento\Eav\Model\Entity\Attribute\SetFactory as AttributeSetFactory;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Framework\Setup\UpgradeDataInterface;

class UpgradeData implements UpgradeDataInterface
{
    /**
     * @var CustomerSetupFactory
     */
    protected $customerSetupFactory;

    /**
     * @var AttributeSetFactory
     */
    private $attributeSetFactory;

    /**
     * @param CustomerSetupFactory $customerSetupFactory
     * @param AttributeSetFactory $attributeSetFactory
     */
    public function __construct(
        CustomerSetupFactory $customerSetupFactory,
        AttributeSetFactory $attributeSetFactory
    ) {
        $this->customerSetupFactory = $customerSetupFactory;
        $this->attributeSetFactory = $attributeSetFactory;
    }

    /**
     * put your comment there...
     *
     * @param ModuleDataSetupInterface $setup
     * @param ModuleContextInterface $context
     */
    public function upgrade(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
    {
        $setup->startSetup();

        /** @var CustomerSetup $customerSetup */
        $customerSetup = $this->customerSetupFactory->create(['setup' => $setup]);
        $customerEntity = $customerSetup->getEavConfig()->getEntityType('customer');
        $attributeSetId = $customerEntity->getDefaultAttributeSetId();
        /** @var $attributeSet AttributeSet */
        $attributeSet = $this->attributeSetFactory->create();
        $attributeGroupId = $attributeSet->getDefaultGroupId($attributeSetId);



        $attributeCode = "type_of_activity";
        $customerSetup->addAttribute(Customer::ENTITY, $attributeCode, [
            'type'              => 'varchar',
            'label'             => 'Type of activity',
            'input'             => 'select',
            'source'            => 'Mdweb\Inscription\Model\Attribute\Source\CustomerActivity',
            'required'          => true,
            'visible'           => true,
            'user_defined'      => true,
            'sort_order'        => 1000,
            'position'          => 1000,
            'system'            => false,
            'is_used_in_grid' => true,
            'is_visible_in_grid' => true,
            'is_filterable_in_grid' => true,
            'is_searchable_in_grid' => false,

        ]);

        $attribute = $customerSetup->getEavConfig()->getAttribute(Customer::ENTITY, $attributeCode)
            ->addData([
                'attribute_set_id' => $attributeSetId,
                'attribute_group_id' => $attributeGroupId,
                'used_in_forms' =>  ['adminhtml_customer', 'customer_account_create', 'customer_account_edit']
            ]);
        $attribute->save();








        $attributeCode1 = "social_reason";
        $customerSetup->addAttribute(Customer::ENTITY, $attributeCode1, [
            'type'              => 'varchar',
            'label'             => 'Social reason',
            'input'             => 'text',
            'required'              => true,
            'sort_order'            => 11,
            'visible'               => true,
            'system'                => false,
            'is_used_in_grid'       => true,
            'is_visible_in_grid'    => true,



        ]);


        $attribute1 = $customerSetup->getEavConfig()->getAttribute(Customer::ENTITY, $attributeCode1)
            ->addData([
                'attribute_set_id' => $attributeSetId,
                'attribute_group_id' => $attributeGroupId,
                'used_in_forms' =>  ['adminhtml_customer', 'customer_account_create', 'customer_account_edit']
            ]);
        $attribute1->save();



        $setup->endSetup();
    }
}
  • Can you provide the code you used to create the attributes? – Dominic Xigen Jul 10 at 19:36
  • check my updated question please – Developper Magento Jul 11 at 7:05
0

Your setup file looks good

Take a look at the following extension

https://github.com/DominicWatts/EmailCC

view/frontend/layout/customer_account_edit.xml

<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <update handle="customer_account"/>
    <body>
        <referenceContainer name="content">
            <block class="Magento\Customer\Block\Form\Edit" name="customer_edit" template="Xigen_CC::form/edit.phtml" cacheable="false">
                <container name="form.additional.info" as="form_additional_info"/>
                <block class="Magento\Customer\Block\Form\Edit" name="email_cc" template="Xigen_CC::form/email_cc.phtml"/>
            </block>
        </referenceContainer>
    </body>
</page>

https://github.com/DominicWatts/EmailCC/blob/master/view/frontend/templates/form/edit.phtml#L80

view/frontend/templates/form/email_cc.phtml

<?php
$emailCc = $block->getCustomer()->getCustomAttribute('email_cc');
$customerEmailCC = $emailCc ? $emailCc->getValue() : null;
?>
<fieldset class="fieldset create account" >
    <legend class="legend">
        <span><?=$block->escapeHtmlAttr(__('Email Copy To'))?></span>
    </legend>
    <br>
    <div class="field email_cc">
        <label for="email_cc" class="label"><span><?=$block->escapeHtmlAttr(__('Email Address (Comma separated)'));?></span></label>
        <div class="control">
            <input id="email_cc" class="input-text" name="email_cc" value="<?=$block->escapeHtmlAttr($customerEmailCC);?>" title="<?=$block->escapeHtmlAttr(__('Email Copy To'));?>" type="text" autocomplete="off" />
        </div>
    </div>
</fieldset>
  • but how to save them because i tried and i still can't save the form – Developper Magento Jul 11 at 10:58
  • magento.stackexchange.com/questions/281705/… i also asked a question here – Developper Magento Jul 11 at 11:01
  • Works for me. As you can see no controller logic updates. Just name input same name and id as attribute code – Dominic Xigen Jul 11 at 13:10
  • but i have two attributes that are required for customer of group 1 and not group 2 so i have to fill them for the second group as string value to nskip probleme of required error – Developper Magento Jul 11 at 13:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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