Through a setup script I created two new customer address fields for Magento 2. These field show up nicely in the backend and checkout. But, the values are not send from checkout to the api. Now the fields are not saved into the database.
Also, when editing the order address in the backend the values aren't stored. The values are only stored when editing the customer address through the customer itself.
What am I missing!? The Magento docs about adding a shipping field aren't helping me...
-
How did you add the fields?LM_Fielding– LM_Fielding2017年06月19日 16:47:32 +00:00Commented Jun 19, 2017 at 16:47
-
magento.stackexchange.com/questions/126036/…LM_Fielding– LM_Fielding2017年06月29日 13:08:04 +00:00Commented Jun 29, 2017 at 13:08
1 Answer 1
This usually happens when you specify "system" => "1" for the customer attributes. In order to make this working, you may create an upgrade script to set "system" => "0".
Here's an example:
app/code/YourVendor/YourModule/Setup/UpgradeData.php
use Magento\Framework\Setup\UpgradeDataInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Customer\Setup\CustomerSetupFactory;
use Magento\Customer\Setup\CustomerSetup;
use Magento\Customer\Model\Customer;
...
public function __construct(
CustomerSetupFactory $customerSetupFactory
) {
$this->customerSetupFactory = $customerSetupFactory;
}
...
public function upgrade(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
{
if (version_compare($context->getVersion(), '0.2.0') < 0) {
$customerSetup = $this->customerSetupFactory->create();
$customerSetup->getEavConfig()
->getAttribute(Customer::ENTITY, 'example_attribute_code')
->setData('is_user_defined', 1)
->setData('system', 0)
->save();
}
}
-
The example does the opposite to the suggestion?Tisch– Tisch2022年02月02日 13:08:27 +00:00Commented Feb 2, 2022 at 13:08
-
@Tisch In this example I'm just showing how to change the value of 'system'. You can either set it to 1 or to 0, whatever you need in your specific situation.Nikita Abrashnev– Nikita Abrashnev2022年02月22日 19:01:36 +00:00Commented Feb 22, 2022 at 19:01