skip to Main Content

I have been attempting to create a custom customer attribute that would allow the customer to save an attribute to their profile. When I update my Magento site with the code, I see no front end change nor do I see any update in my Database. What am I doing incorrectly that is causing both of these issues? Do I need to add some .phtml change?

InstallData.php

<?php
namespace SRDeliveryDateSetup;

use MagentoCustomerSetupCustomerSetupFactory;
use MagentoCustomerModelCustomer;
use MagentoEavModelEntityAttributeSet as AttributeSet;
use MagentoEavModelEntityAttributeSetFactory as AttributeSetFactory;
use MagentoFrameworkSetupInstallDataInterface;
use MagentoFrameworkSetupModuleContextInterface;
use MagentoFrameworkSetupModuleDataSetupInterface;

/**
 * @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;
        }
    
    
        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, 'custom_attribute', [
                'type' => 'varchar',
                'label' => 'Custom Attributeeee',
                'input' => 'text',
                'required' => false,
                'visible' => true,
                'user_defined' => true,
                'position' =>999,
                'system' => 0,
            ]);
    
            $attribute = $customerSetup->getEavConfig()->getAttribute(Customer::ENTITY, 'custom_attribute')
            ->addData([
                'attribute_set_id' => $attributeSetId,
                'attribute_group_id' => $attributeGroupId,
                'used_in_forms' => ['adminhtml_customer', 'customer_address_edit'],//you can use other forms also ['adminhtml_customer_address', 'customer_address_edit', 'customer_register_address']
            ]);
    
            $attribute->save();
        }
    }

2

Answers


  1. Try to run the following commands after you make any major changes to your code. For example, if you create an extension, you might run the upgrade command.

    php bin/magento setup:upgrade
    
    php bin/magento setup:static-content:deploy
    
    php bin/magento cache:clean or php bin/magento cache:flush
    

    The above commands should be run in the console.

    Login or Signup to reply.
  2. Please try this:

    magento2appcodeCustomCustomerAttributeBlockWidgetPassport.php
    /**
     * @return bool
     */
     
    public function isEnabled() {
        $attributeMetadata = $this->_getAttribute('passport');
        return $attributeMetadata ? (bool) $attributeMetadata->isVisible() : false;
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search