1

I have added 3 attribute to customer account and i want to diplay them in account information I just overrided info.phtml but i just have name and email. Any Idea of how to display this new attributes!!?? and thanks in advance

<div class="box box-information">
        <strong class="box-title">
            <span><?= $block->escapeHtml(__('Contact Information')) ?></span>
        </strong>
        <div class="box-content">
            <p>
                <?= $block->escapeHtml($block->getName()) ?><br>
                <?= $block->escapeHtml($block->getAdresse()) ?><br>
                <?= $block->escapeHtml($block->getPhone()) ?><br>
                <?= $block->escapeHtml($block->getCustomer()->getEmail()) ?><br>
            </p>
        </div>
        <div class="box-actions">
            <a class="action edit" href="<?= $block->escapeUrl($block->getUrl('customer/account/edit')) ?>">
                <span><?= $block->escapeHtml(__('Edit')) ?></span>
            </a>
            <a href="<?= $block->escapeUrl($block->getChangePasswordUrl()) ?>" class="action change-password">
                <?= $block->escapeHtml(__('Change Password')) ?>
            </a>
        </div>
    </div>

This is what i put in InstallData.php

$customerSetup->addAttribute(Customer::ENTITY, 'adresse', [
        'type' => 'varchar',
        'label' => 'Adresse',
        'input' => 'text',
        'required' => false,
        'visible' => true,
        'is_used_in_grid' => true,
        'user_defined' => true,
        'sort_order' => 1001,
        'position' => 1001,
        'system' => 0,
    ]);

    $customerSetup->addAttribute(Customer::ENTITY, 'phone', [
        'type' => 'varchar',
        'label' => 'Phone Number',
        'input' => 'text',
        'required' => false,
        'visible' => true,
        'is_used_in_grid' => true,
        'user_defined' => true,
        'validate_rules' => '{"max_text_length":255,"min_text_length":1}',
        'sort_order' => 1002,
        'position' => 1002,
        'system' => 0,
    ]);
1
  • Do you want to just show that value? Or You are talking about displaying it in the form here http://127.0.0.1/magento/customer/account/edit/ Aug 24 '18 at 13:15
1

The functions getAdresse() and getPhone() do not exist for the block (\Magento\Customer\Block\Account\Dashboard\Info) but you can make use of the block's function getCustomer() the same way the template is already getting the customer's email address.

<div class="box box-information">
    <strong class="box-title">
        <span><?= $block->escapeHtml(__('Contact Information')) ?></span>
    </strong>
    <div class="box-content">
        <p>
            <?= $block->escapeHtml($block->getName()) ?><br>
            <?= $block->escapeHtml($block->getCustomer()->getCustomAttribute('addresse')->getValue()) ?><br>
            <?= $block->escapeHtml($block->getCustomer()->getCustomAttribute('phone')->getValue()) ?><br>
            <?= $block->escapeHtml($block->getCustomer()->getEmail()) ?><br>
        </p>
    </div>
    <div class="box-actions">
        <a class="action edit" href="<?= $block->escapeUrl($block->getUrl('customer/account/edit')) ?>">
            <span><?= $block->escapeHtml(__('Edit')) ?></span>
        </a>
        <a href="<?= $block->escapeUrl($block->getChangePasswordUrl()) ?>" class="action change-password">
            <?= $block->escapeHtml(__('Change Password')) ?>
        </a>
    </div>
</div>
7
  • Uncaught Error: Call to a member function getValue() on null in /var/www/html/sajjada/app/design/frontend/Magenticians/Mytheme/Magento_Customer/templates/account/dashboard/info.phtml Aug 24 '18 at 9:57
  • The custom customer attributes can't have been setup properly then. Check out the answer to this question magento.stackexchange.com/questions/162087/… Aug 24 '18 at 10:03
  • i don't understand where is the error exactly when setting up attributes, please check up my InstallData.php and thanks for your replay Aug 24 '18 at 10:14
  • The bit where it adds values for attribute_set_id, attribute_group_id and used_in_forms is needed Aug 24 '18 at 10:17
  • 1
    Have you removed the record for your module in setup_module and ran setup:upgrade again? The installation script will only if the module hasn't been installed yet. Aug 24 '18 at 10:54
0
<?php

namespace Magenticians\CustomerAttribute\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\InstallDataInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;

/**
 * @codeCoverageIgnore
 */
class InstallData implements InstallDataInterface
{

    /**
     * @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;
    }


    /**
     * {@inheritdoc}
     */
    public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
    {

        /** @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);

        $customerSetup->addAttribute(Customer::ENTITY, 'birth', [
            'type' => 'datetime',
            'label' => 'Birth',
            'input' => 'date',
            'required' => false,
            'visible' => true,
            'user_defined' => true,
            'sort_order' => 1000,
            'position' => 1000,
            'system' => 0,
        ]);

        $customerSetup->addAttribute(Customer::ENTITY, 'adresse', [
            'type' => 'varchar',
            'label' => 'Adresse',
            'input' => 'text',
            'required' => false,
            'visible' => true,
            'is_used_in_grid' => true,
            'user_defined' => true,
            'sort_order' => 1001,
            'position' => 1001,
            'system' => 0,
        ]);

        $customerSetup->addAttribute(Customer::ENTITY, 'phone', [
            'type' => 'varchar',
            'label' => 'Phone Number',
            'input' => 'text',
            'required' => false,
            'visible' => true,
            'is_used_in_grid' => true,
            'user_defined' => true,
            'validate_rules' => '{"max_text_length":255,"min_text_length":1}',
            'sort_order' => 1002,
            'position' => 1002,
            'system' => 0,
        ]);



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

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


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

        $attribute->save();
        $secondattribute->save();
        $thirdattribute->save();



    }
}
1
  • Now i removed allo the attributes and upgrade reindex flush but i can't create a new account :(i even tried with a new project but i can't create an account what's te problem any idea ?? Aug 24 '18 at 13:20

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.