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.