1

I have created a module, which creates one custom customer address attribute. In my attribute I have visible true, Now I need to visible false. I need to update that attribute. My code is as below:

public function upgrade(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
    {
        $setup->startSetup();
        $customerSetup = $this->customerSetupFactory->create(['setup' => $setup]);
        $customerSetup->updateAttribute('customer_address', 'custom_id', [
           'is_visible'  =>  false,
       ]);

        $attribute = $customerSetup->getEavConfig()->getAttribute('customer_address', 'custom_id')
        ->setData([
            'used_in_forms' => [],
        ]);

       $attribute->save();
       $setup->endSetup();
}

it's giving error "Unique constraint violation". How could I update this attribute

Thanks.

2 Answers 2

2

As you can see the method updateAttribute requires the field and the value, which should be string-type:

/**
 * Update Attribute data and Attribute additional data
 *
 * @param int|string $entityTypeId
 * @param int|string $id
 * @param string|array $field
 * @param mixed $value
 * @param int $sortOrder
 * @return $this
 */
public function updateAttribute($entityTypeId, $id, $field, $value = null, $sortOrder = null)
{
    $this->_updateAttribute($entityTypeId, $id, $field, $value, $sortOrder);
    $this->_updateAttributeAdditionalData($entityTypeId, $id, $field, $value);
    return $this;
}

So you should write a code like this:

$customerSetup->updateAttribute('customer_address', 'custom_id', 'is_visible', false);
1
  • Thanks, @Siarhey your answer is appreciated. Commented Jan 2, 2019 at 9:35
0

I did one mistake, removed from code and its working. Answer as below:

public function upgrade(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
    {
        $setup->startSetup();
        $customerSetup = $this->customerSetupFactory->create(['setup' => $setup]);
        $customerSetup->updateAttribute('customer_address', 'custom_id', [
           'is_visible'  =>  false,
       ]);

        $attribute = $customerSetup->getEavConfig()->getAttribute('customer_address', 'custom_id');

       $attribute->save();
       $setup->endSetup();
}

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.