0

We have a custom boolean customer attribute created from installSchema script like below:

$eavSetup->addAttribute(
 \Magento\Customer\Model\Customer::ENTITY,
 'test_attribute',
 [
 'label' => 'Test Attribute',
 'input' => 'select', 
 'source' => 'Magento\Eav\Model\Entity\Attribute\Source\Boolean', 
 'default' => 0,
 'required' => false,
 'visible' => true,
 'system' => false,
 'filterable' => false,
 'is_used_in_grid' => false,
 'is_filterable_in_grid' => false,
 'is_searchable_in_grid' => false,
 ]
 );

I need to change the default value to "1" for this attribute through upgradeSchema. Tried below code for the same in upgrade schema:

if (version_compare($context->getVersion(), '1.1.0', '<')) {
 $this->eavSetupFactory->create()->updateAttribute(\Magento\Customer\Model\Customer::ENTITY, 'test_attribute', 'default', 1);
 }

But, it didn't worked. The default value is still No, also verified in eav_attribute table in database. I can also confirm that the upgrade schema is working and the if condition for version compare is also working. The version in setup_module table is also getting updated. Are there any changes needed to the updateAttribute method call? Please assist.

asked Jun 9, 2021 at 8:27

2 Answers 2

0

create etc/UpgradeData.php

namespace [Vendor]\[Module]\Setup;
 
use Magento\Eav\Setup\EavSetup;
use Magento\Eav\Setup\EavSetupFactory;
use Magento\Framework\Setup\UpgradeDataInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Customer\Setup\CustomerSetupFactory;
 
class UpgradeData implements UpgradeDataInterface
{
 private $eavSetupFactory;
 protected $customerSetupFactory;
 public function __construct(
 EavSetupFactory $eavSetupFactory,
 CustomerSetupFactory $customerSetupFactory
 )
 {
 $this->eavSetupFactory = $eavSetupFactory;
 $this->customerSetupFactory = $customerSetupFactory;
 }
 public function upgrade(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
 {
 $eavSetup = $this->eavSetupFactory->create(['setup' => $setup]);
 $customerSetup = $this->customerSetupFactory->create(['setup' => $setup]);
 //check version
 if (version_compare($context->getVersion(), '2.0.3') < 0) {
 //update customer attribute
 $customerSetup->updateAttribute(
 \Magento\Customer\Model\Customer::ENTITY,
 'attribute_code',
 'default',
 1
 );
 }
 }
}
answered Jun 9, 2021 at 11:10
0

It got fixed on using "default_value" instead of "default". So, the updated working code in upgradeSchema is:

if (version_compare($context->getVersion(), '1.1.0', '<')) {
 $this->eavSetupFactory->create()->updateAttribute(\Magento\Customer\Model\Customer::ENTITY, 'test_attribute', 'default_value', 1);
}
answered Jun 10, 2021 at 5:40

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.