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="MagentoCustomerApiDataCustomerInterface">
<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 MagentoEavModelEntityAttributeSourceBoolean;
use MagentoFrameworkSetupInstallDataInterface;
use MagentoFrameworkSetupModuleContextInterface;
use MagentoFrameworkSetupModuleDataSetupInterface;
class InstallData implements InstallDataInterface {
private $customerSetupFactory;
public function __construct(
MagentoCustomerSetupCustomerSetupFactory $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
2
Answers
I had similar issue recently, try to add this in ‘used_in_forms’.
You might have to remove the attribute and reinstall it:
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.
It’ll be related to this issue:
https://apiworks.net/magento2/magento-2-is-not-saving-the-customer-attribute/
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