skip to Main Content

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


  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. ).

    Login or Signup to reply.
  2. 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

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search