1

Using Magento 2.3.0 Whenever trying to save customer I get errors that newly created attributes are required, even when I set their values.

etc/extend_attributes.xml

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Api/etc/extension_attributes.xsd">
    <extension_attributes for="Magento\Customer\Api\Data\CustomerInterface">
        <attribute code="customershipping_enabled" type="string" />
        <attribute code="customershipping_price" type="string" />
    </extension_attributes>
</config>

Setup/InstallData.php

<?php
namespace <vendor>\<module_name>\Setup;

use Magento\Eav\Model\Entity\Attribute\Source\Boolean;

use Magento\Framework\Setup\InstallDataInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;

class InstallData implements InstallDataInterface {
    private $customerSetupFactory;

    public function __construct(
        \Magento\Customer\Setup\CustomerSetupFactory $customerSetupFactory
    ) {
        $this->customerSetupFactory = $customerSetupFactory;
    }

    public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context) {
        $customerSetup =$this->customerSetupFactory->create(['setup'=>$setup]);

        $setup->startSetup();

        $customerSetup->addAttribute('customer', 'customershipping_enabled', [
            'label'=>'Customer Shipping Enabled',
            'type' => 'int',
            'input' => 'select',
            'source' => Boolean::class,
            'required'=>true,
            'visible'=>true,
            'default' => 0,
            'position' => 198,
        ]);

        $customerSetup->addAttribute('customer', 'customershipping_price', [
            'label'=>'Customer Shipping Price',
            'type'=>'decimal',
            'input' => 'text',
            'required'=>true,
            'visible'=>true,
            'default' => 0,
            'position' => 199,
        ]);

        $enabledAttribute = $customerSetup->getEavConfig()->getAttribute('customer', 'customershipping_enabled');
        $enabledAttribute->setData('used_in_forms', ['adminhtml_customer']);
        $enabledAttribute->save();

        $priceAttribute = $customerSetup->getEavConfig()->getAttribute('customer', 'customershipping_price');
        $priceAttribute->setData('used_in_forms', ['adminhtml_customer']);
        $priceAttribute->save();

        $setup->endSetup();
    }
}

I have read many tutorials and documentation on this, and I believe this should be working correctly, am I missing something? Whenever I try to add new customer or update existing customer, it says that these 2 attributes are required values, save fails.

Also looks identical to this post: mage2gen.com/snippets/customerattribute

Cmaclean066
  • 65
  • 2
  • 8

2 Answers2

1

I had similar issue recently, try to add this in 'used_in_forms'.

You might have to remove the attribute and reinstall it:

'used_in_forms' => ['adminhtml_customer', 'customer_account_edit', 'customer_account_create']

edit

Oh I think this should solve the issue, just checked my installData and upgradeData scripts and they all have system => 0. Just add it in.

    $customerSetup->addAttribute('customer', 'customershipping_enabled', [
        'label'=>'Customer Shipping Enabled',
        'type' => 'int',
        'input' => 'select',
        'source' => Boolean::class,
        'required'=>true,
        'visible'=>true,
        'default' => 0,
        'position' => 198,
        'system' => 0
    ]);

It'll be related to this issue:

https://apiworks.net/magento2/magento-2-is-not-saving-the-customer-attribute/

The function getCustomAttributesMetadata is looping through all EAV attributes and checking if the attribute is marked as “is_system” inside the “customer_eav_attribute” table, which was the case with my custom attribute.

Solution:

By default, Magento flagged my custom attribute as is_system = 1, so I just needed to add ‘system’ => false in my upgrade script and execute it again (after I removed the original attribute directly from the database. ).

giolliano sulit
  • 996
  • 1
  • 6
  • 11
  • I dont want the attributes to be displayed on frontend forms, for admin only. Weird thing is when i add attributes only without adding form adminhtml_customer, they are still displayed in admin customer edit. – Cmaclean066 Mar 05 '19 at 05:50
  • Yes you guys are right, adding 'system' => false to attribute creation solved the problem. – Cmaclean066 Mar 05 '19 at 08:32
0

Root cause of this issue is design behavior of magento 2.

If custom attribute is set as a required one than it must be configured to be shown on storefront and to be shown in all the forms.

If you wants a custom attribute to be required only on some certain forms, then an extension attribute should be used instead with 'required'=>false.

Extension attributes are used to extend functionality of custom attributes.

You just need to replace

'required'=>true,

with

'required'=>false,

For more details please refer the link: Click here

  • Please note the issue is not that its required. It's that he's filling it in and it's still not saving.. – giolliano sulit Mar 05 '19 at 06:14
  • The customershipping price has now saved correctly, however the attribute customershipping_enabled is not saving the value "yes". Is that because of – Cmaclean066 Mar 05 '19 at 08:34