<?php
declare(strict_types=1);
namespace VendorModuleSetupPatchData;
use MagentoEavModelConfig;
use MagentoEavModelEntityAttributeSetFactory as AttributeSetFactory;
use MagentoEavModelEntityAttributeSourceBoolean;
use MagentoEavSetupEavSetupFactory;
use MagentoFrameworkExceptionLocalizedException;
use MagentoFrameworkSetupPatchDataPatchInterface;
use Zend_Validate_Exception;
/**
* @class CustomerSync
*/
class CustomerSync implements DataPatchInterface
{
/**
* @var Config
*/
private Config $eavConfig;
/**
* @var EavSetupFactory
*/
private EavSetupFactory $eavSetupFactory;
/**
* @var AttributeSetFactory
*/
private AttributeSetFactory $attributeSetFactory;
/**
* AddressAttribute constructor.`enter code here`
*
* @param Config $eavConfig
* @param EavSetupFactory $eavSetupFactory
* @param AttributeSetFactory $attributeSetFactory
*/
public function __construct(
Config $eavConfig,
EavSetupFactory $eavSetupFactory,
AttributeSetFactory $attributeSetFactory
) {
$this->eavConfig = $eavConfig;
$this->eavSetupFactory = $eavSetupFactory;
$this->attributeSetFactory = $attributeSetFactory;
}
/**
* {@inheritdoc}
*/
public static function getDependencies(): array
{
return [];
}
/**
* @return void
* @throws LocalizedException
* @throws Zend_Validate_Exception
*/
public function apply(): void
{
$eavSetup = $this->eavSetupFactory->create();
$customerEntity = $this->eavConfig->getEntityType('customer');
$attributeSetId = $customerEntity->getDefaultAttributeSetId();
$attributeSet = $this->attributeSetFactory->create();
$attributeGroupId = $attributeSet->getDefaultGroupId($attributeSetId);
$eavSetup->addAttribute('customer', 'eligible_for_sync', [
'type' => 'int',
'input' => 'boolean',
'label' => 'Eligible for Sync',
'visible' => false,
'source' => Boolean::class,
'required' => false,
'user_defined' => true,
'system' => false,
'global' => true,
'default' => 0,
'visible_on_front' => false,
'sort_order' => 50,
'position' => 50,
'is_visible_in_grid' => true
]);
$customAttribute = $this->eavConfig->getAttribute('customer', 'eligible_for_sync');
$customAttribute->addData([
'attribute_set_id' => $attributeSetId,
'attribute_group_id' => $attributeGroupId,
'used_in_forms' => ['adminhtml_customer', 'adminhtml_checkout', 'customer_account_create', 'customer_account_edit']
]);
$customAttribute->save();
}
/**
* {@inheritdoc}
*/
public function getAliases(): array
{
return [];
}
}
That is my controller to save the attribute value. In Xdebug set the value 1 but not update in the database customer_entity_int table
$customerCollection = $this->collectionFactory->create();
$customerCollection->addFieldToFilter('entity_id', [3,8]);
if ($customerCollection->getSize() > 0) {
foreach ($customerCollection as $customer) {
$customerId = $customer->getId();
$data = "1";
$customerData = $customer->getDataModel();
$customerData->setCustomAttribute('eligible_for_sync', $data);
$customer->updateData($customerData);
$customerResource = $this->customerFactory->create();
$customerResource->saveAttribute($customer, 'eligible_for_sync');
}
}