0

I've seen this question many times, but all the solutions I've tried didn't work. I've generated a module that creates three custom attributes in customer's information. Since I'm new to Magento (Magento 2 especially) I've done this trough Silk https://www.silksoftware.com/magento-module-creator/magento2-module-creator.php

I can see the three attributes fine in the backend, now I need to show them on the frontend so customers can edit them. How can I do that?

This is part of my CustomerSetup.php that defines one of the attributes.

    $customerSetup -> addAttribute(\Magento\Customer\Model\Customer::ENTITY,
        'cf',
        [
        'label' => 'Codice fiscale / p.IVA',
        'system' => 0,
        'position' => 104,
        'sort_order' =>104,
        'visible' =>  true,
        'note' => '',
        'type' => 'varchar',
        'input' => 'text',          
        ]
    );

    $customerSetup -> getEavConfig() -> getAttribute('customer', 'cf')->setData('is_user_defined',1)->setData('is_required',0)->setData('default_value','')->setData('used_in_forms', ['adminhtml_customer', 'checkout_register', 'customer_account_create', 'customer_account_edit', 'adminhtml_checkout']) -> save();

Now, on edit.phtml I've tried something like

<?php $block->getCustomer()->getCf() ?>

But I can't see anything. Can anyone help? Thanks!

EDIT

I've tried the suggestion. I've edited my CustomerSetup.php with provided code and created UpgradeData.php, coping pasting InstallData.php, changing Install with Upgrade and adding a 1 to the version (should I also change installAttributes in upgradeAttributes?)

<?php

namespace Alfabet\ClientiAttributi\Setup;

use Magento\Eav\Setup\EavSetup;
use Magento\Eav\Setup\EavSetupFactory;
use Magento\Framework\Setup\InstallDataInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;

/**
* @codeCoverageIgnore
*/
class UpgradeData implements UpgradeDataInterface
{
/**
 * EAV setup factory
 *
 * @var EavSetupFactory
 */
private $eavSetupFactory;

/**
 * Init
 *
 * @param EavSetupFactory $eavSetupFactory
 */
public function __construct(EavSetupFactory $eavSetupFactory)
{
    $this->eavSetupFactory = $eavSetupFactory;
}

/**
 * {@inheritdoc}
 * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
 */
public function upgrade(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
{
    /** @var EavSetup $eavSetup */
    $eavSetup = $this->eavSetupFactory->create(['setup' => $setup]);
    if (version_compare($context->getVersion(), '1.1.0') < 0){

            $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
            $customerSetup = $objectManager->create('Alfabet\ClientiAttributi\Setup\CustomerSetup');
            $customerSetup->installAttributes($customerSetup);



    }

}
}

but even after running php bin/magento setup:upgrade i can't see any difference. I've also tried changing module's version in module.xml, but it causes the site to crash.

2 Answers 2

0

check this

try to update cf attribute using UpgradeData with below code

$customerSetup->addAttribute(\Magento\Customer\Model\Customer::ENTITY, 'cf', [
    'type' => 'static',
    'label' => 'Codice fiscale / p.IVA',
    'input' => 'text',
    'required' => false,
    'visible' => true,
    'user_defined' => true,
    'sort_order' => 104,
    'position' => 104,
    'system' => 0,
]);

$eavSetup->addAttributeToSet(
    CustomerMetadataInterface::ENTITY_TYPE_CUSTOMER,
    CustomerMetadataInterface::ATTRIBUTE_SET_ID_CUSTOMER,
    null,
    'cf');

$customerSetup->getEavConfig()->getAttribute(\Magento\Customer\Model\Customer::ENTITY, 'cf')
    ->addData([
        'attribute_set_id' => $attributeSetId,
        'attribute_group_id' => $attributeGroupId,
        'used_in_forms' => ['adminhtml_customer','customer_address_edit','adminhtml_customer_address', 'customer_register_address','customer_account_edit', 'customer_account_create']
    ])->save();

Using customer session to get customer custom attribute

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$customerSession = $objectManager->create('Magento\Customer\Model\Session');

if ($customerSession->isLoggedIn()) {
    $cf = $customerSession->getCustomer()->getCf();
}
3
  • Many thanks, I've tried your solution but it doesn't seems to work. After replacing the piece of code with yours I've ran "php bin/magento setup:upgrade" on comand line, then added your second code in edit.phtml like this "<?php CODE ?>", but i can't see anything.
    – user70508
    Commented Jun 5, 2020 at 9:04
  • Using installData or UpgradeData to create customer attribute. If already created InstallData then use upgradeData to creat attribute with my above code.
    – Devidas
    Commented Jun 5, 2020 at 15:11
  • Hi, please see edited question.
    – user70508
    Commented Jun 8, 2020 at 8:47
0

I'm sure it is a typo but just in case. This won't output nothing:

<?php $block->getCustomer()->getCf() ?>

echo is missing.

Do either

<?php echo $block->getCustomer()->getCf() ?>

or

<?= $block->getCustomer()->getCf() ?>

Your Answer

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

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