3

I have created a new module to create customer attribute. The same was working perfectly in previous magento 2 versions. But in magento 2.2 its not working. Could you pls check into that.

<?php
namespace Mymodule\AttributeManager\Setup;


use Magento\Customer\Setup\CustomerSetupFactory;

use Magento\Customer\Model\Customer;

use Magento\Framework\Module\Setup\Migration;

use Magento\Framework\Setup\InstallDataInterface;

use Magento\Framework\Setup\ModuleContextInterface;

use Magento\Framework\Setup\ModuleDataSetupInterface;

class InstallData implements InstallDataInterface
{

    private $customerSetupFactory;

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

    /**
     * {@inheritdoc}
     * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
     */
    public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
    {

        /** @var CustomerSetup $customerSetup */
        $customerSetup = $this->customerSetupFactory->create(['setup' => $setup]);

        $setup->startSetup();


        $attributesInfo = [
            'nettermspo' => [
                'user_defined' => true,
                'position' =>99,
                'type' => 'int',
                'label' => 'Is eligible for NET TERMS PO ?',
                'input' => 'select',
                'sort_order' => 99,
                'source' => 'Magento\Eav\Model\Entity\Attribute\Source\Boolean',
                'global' => 1,
                'default' => 0,
                'visible' => true,
                'required' => false,
                'system' => 0,
                'group' => 'Account Information',
            ]
        ];

        foreach ($attributesInfo as $attributeCode => $attributeParams) {
            $customerSetup->addAttribute(Customer::ENTITY, $attributeCode, $attributeParams);
        }
        $nettermspoAttribute = $customerSetup->getEavConfig()->getAttribute(Customer::ENTITY, 'nettermspo');
        $nettermspoAttribute->setData(
            'used_in_forms',
            ['adminhtml_customer']
        );
        $nettermspoAttribute->save();
        $setup->endSetup();
    }
}
2

I really recommend you to use simple online module creator for Magento 2 that allowes you to create customer attribute and choose its details. http://www.silksoftware.com/magento-module-creator/magento2x.php

After creating module check the following:

After all that the installed attribute should be visible in customer forms selected in setup script.

UPDATE:

In Magento 2.2 I'm using following approach with CustomerSetupFactory for example in app/code/Vendor/CustomModule/Setup/UpgradeSchema.php.

...
use Magento\Customer\Setup\CustomerSetupFactory;
...
protected $customerSetupFactory;

public function __construct(CustomerSetupFactory $customerSetupFactory) {
    $this->customerSetupFactory = $customerSetupFactory;
}
...
public function upgrade(SchemaSetupInterface $setup, ModuleContextInterface $context){

    $setup->startSetup();

    if (version_compare($context->getVersion(), '1.0.8') < 0) {
        $customerSetup = $this->customerSetupFactory->create();
        $customerSetup->addAttribute(Customer::ENTITY,
            'is_buzzshopper', [
                'type' => 'int',
                'label' => 'Label',
                'input' => 'boolean',
                'note' => 'Some note',
                'length' => 1,
                'sort_order' => 106,
                'position' => 106,
                'system' => false,
                'adminhtml_only' => 1,
                'is_used_in_grid' => false,
                'is_visible_in_grid' => false,
                'is_filterable_in_grid' => false,
                'is_searchable_in_grid' => false
            ]
        );
        $attribute = $customerSetup->getEavConfig()->getAttribute(Customer::ENTITY, 'is_buzzshopper');
        $attribute->setData(
            'used_in_forms',
            ['adminhtml_customer', 'customer_account_create', 'customer_account_edit']
        );
        $attribute->setData('is_user_defined', true);
        $attribute->setData('is_required', false);
        $attribute->save();
    }

    $setup->endSetup();
}

Attribute created that way is shown in admin forms only as boolean "switch" button. See "used_in_forms" data for forms list.
Just notice. Call save() function on attribute it self isn't best practice. Its maybe even depraceted approach, because entities must not be responsible for their own persistence - however it works for simple demonstration.

| improve this answer | |
  • Will this work on magento 2.2.2? – Wasiq Shahrukh Jan 2 '18 at 14:40
  • 1
    Have updated my answer with working example on Magento ver. 2.2.0. Hope it helps. – tesar.hk Jan 3 '18 at 17:09
1

I was running into the same issue on our client's Magento Open Source 2.2.1 (Community), and the problem appeared to be with the user_defined property. As soon as I flipped it to false in the install script, or eav_attribute.is_user_defined = 0 directly in the database, the attributes immediately started showing up on the admin customer edit form.

So in your case, Try setting $attributesInfo['nettermspo']['user_defined'] = false. Or rather:

...
$attributesInfo = [
    'nettermspo' => [
        'user_defined' => false,
        ...
    ]
];
...
| improve this answer | |
  • that's the wrong approach! Magento 2.2 introduced 'used_in_forms' for attributes. You should manipulate this. – Philipp Sander Jan 18 '18 at 14:21
  • 1
    In my case, this fix was actually used in conjunction with the used_in_forms approach. I even verified that the new attributes were added to the customer_form_attribute table after finding out that using the correct AttributeRepository approach didn't work correctly. I'm more than happy to collaborate to find the correct solution as that's what I'm always looking for. I would also suggest that if you are going to call me out on the wrong approach that you contribute an answer of your own. – floorz Jan 19 '18 at 15:39
  • Thanks @floorz, flipping the user_defined property worked. – Erfan Oct 19 '18 at 7:45
1

I can confirm that from Magento 2.2 onwards, user_defined has to be false in order for the used_in_forms approach to work.

The only other option I see, is to add the field to the UI component declaration by adding a file to your module view/base/ui_component/customer_form.xml with contents:

<?xml version="1.0" encoding="UTF-8"?>
<form xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Ui:etc/ui_configuration.xsd">
    <fieldset name="customer">
        <field name="my_custom_field" formElement="input">
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="source" xsi:type="string">customer</item>
                </item>
            </argument>
        </field>
    </fieldset>
</form>
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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