1

I have created a custom attribute for customer https://prnt.sc/tb68lv The values are getting saved in the database. i have cross checked it again after opening the customer for edit and the values were present. I want this information to be present when admin creates a new order for the customer. See https://prnt.sc/tb8ak7

Here is my code for custom attribute creation

<?php

namespace Vidushi\Customattribute\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;

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)
    {
        /*customersetupfactory instead of eavsetupfactory */
        $customerSetup = $this->customerSetupFactory->create(['setup' => $setup]);

        // for remove attribute
        //$customerSetup->removeAttribute(\Magento\Customer\Model\Customer::ENTITY,'account_notes');

        $customerEntity = $customerSetup->getEavConfig()->getEntityType('customer');
        $attributeSetId = $customerEntity->getDefaultAttributeSetId();
        /** @var $attributeSet AttributeSet */
        $attributeSet = $this->attributeSetFactory->create();
        $attributeGroupId = $attributeSet->getDefaultGroupId($attributeSetId);
        /* create customer Address book attribute */

        $customerSetup->addAttribute(Customer::ENTITY,'internal_account_notes', [
            'type' => 'varchar', // attribute with varchar type
            'label' => 'Internal Account Notes',
            'input' => 'textarea', // attribute input field is textarea
            'required' => false, // field is not required
            'visible' => true,
            'user_defined' => true,
            'position' => 9999,
            'system'=>0,
        ]);

        $sampleAttribute = $customerSetup->getEavConfig()->getAttribute(Customer::ENTITY, 'internal_account_notes')->addData([
            'attribute_set_id' => $attributeSetId,
            'attribute_group_id' => $attributeGroupId,
            'used_in_forms' => ['adminhtml_customer','customer_account_edit','customer_account_create'],
        ]);
        // more used_in_forms ['adminhtml_checkout','adminhtml_customer','adminhtml_customer_address','customer_account_edit','customer_address_edit','customer_register_address']

        $sampleAttribute->save();
    }

}

0

Your Answer

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