0

I've created a custom customer attribute "Skype", for more detail you can visit here:-

http://nimb.ws/CrM0YF

I've displayed & saved(in the backend) successfully this attribute on the registration page.

but I want to add this attribute in customer edit account so that he can update this attribute.

Here is the path:-

app/design/frontend/Packagetheme/themename/Magento_Customer/templates/form/edit.phtml

 <div class="field">
            <label for="skype" class="label"><span><?= $block->escapeHtml(__('Skype')) ?></span></label>
            <div class="control">
                <input type="text" name="skype"  id="skype" value="<?= $block->escapeHtmlAttr($block->getCustomer()->getSkype()) ?>" title="<?= $block->escapeHtmlAttr(__('Skype')) ?>" class="input-text" >
            </div>
            </div>

But I'm not able to set value using(in customer collection custom attributes not included so I'm not able to set this value):-

<?= $block->escapeHtmlAttr($block->getCustomer()->getSkype()) ?>

I also tried using object manager but didn't get success.

Here is My script:-

1- CustomerSetup.php

<?php

namespace Test\Customerform\Setup;

use Magento\Eav\Model\Config;
use Magento\Eav\Model\Entity\Setup\Context;
use Magento\Eav\Setup\EavSetup;
use Magento\Framework\App\CacheInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Eav\Model\ResourceModel\Entity\Attribute\Group\CollectionFactory;

class CustomerSetup extends EavSetup {

    protected $eavConfig;

    public function __construct(
        ModuleDataSetupInterface $setup,
        Context $context,
        CacheInterface $cache,
        CollectionFactory $attrGroupCollectionFactory,
        Config $eavConfig
        ) {
        $this -> eavConfig = $eavConfig;
        parent :: __construct($setup, $context, $cache, $attrGroupCollectionFactory);
    } 

    public function installAttributes($customerSetup) {
        $this -> installCustomerAttributes($customerSetup);
        $this -> installCustomerAddressAttributes($customerSetup);
    } 

    public function installCustomerAttributes($customerSetup) {


        $customerSetup -> addAttribute(\Magento\Customer\Model\Customer::ENTITY,
            'skype',
            [
            'label' => 'Skype',
            'system' => 0,
            'position' => 100,
            'sort_order' =>100,
            'visible' =>  true,
            'note' => '',


                        'type' => 'varchar',
                        'input' => 'text',

            ]
            );

        $customerSetup -> getEavConfig() -> getAttribute('customer', 'skype')->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();

    } 

    public function installCustomerAddressAttributes($customerSetup) {

    } 

    public function getEavConfig() {
        return $this -> eavConfig;
    } 
} 

2- InstallData.php

<?php

namespace Test\Customerform\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 InstallData implements InstallDataInterface
{
    /**
     * 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 install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
    {
        /** @var EavSetup $eavSetup */
        $eavSetup = $this->eavSetupFactory->create(['setup' => $setup]);
        if (version_compare($context->getVersion(), '1.0.0') < 0){

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



        }

    }
}

How can I achieve this?

2
  • Do a indexing for a one more time. If you using extension then you attribute then webkul.com/blog/add-extension-attribute-magento2 then you need to use $extAttributes =$block->getCustomer()->getExtensionAttributes(); $selectedShipping = $extAttributes->getSkype()
    – Amit Bera
    Aug 6, 2018 at 13:29
  • thanks, can you please make it simpler? I I've updated my post, please review again. Aug 7, 2018 at 6:39

1 Answer 1

0

Use below code in installer script of custom customer attributes :

$used_in_forms=array();
$used_in_forms[]="adminhtml_customer";
$used_in_forms[]="checkout_register";
$used_in_forms[]="customer_account_create";
$used_in_forms[]="customer_account_edit";
$used_in_forms[]="adminhtml_checkout";
$attribute->setData("used_in_forms", $used_in_forms)
            ->setData("is_used_for_customer_segment", true)
            ->setData("is_system", 0)
            ->setData("is_user_defined", 1)
            ->setData("is_visible", 1)
            ->setData("sort_order", 100);
    $attribute->save();

Hope this helps you !!!

1
  • Thanks for your input, I think I already included your input.I've added my script please review my post & let me reply for the same. Aug 7, 2018 at 6:11

Your Answer

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

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