I'm using magento v2.5 and i tried to create customer programmatically from frontend and backend, i share the same code between frontend and backend, after customer being save then i try to save the customer custom attribute which i made, the problem is when i try to do it from frontend side it works fine, but when i try to create the customer programmatically from backend (adminhtml) the customer was saved, but the customer custom attribute not being save:
// \Magento\Customer\Model\CustomerFactory $_customerFactory,
// \Magento\Customer\Api\CustomerRepositoryInterface $_customerRepoInterface,
$customer = $this->_customerFactory->create();
$customer->setWebsiteId(1);
$customer->setFirstname($data['firstname']);
$customer->setLastname($data['lastname']);
$customer->setPassword($data['password']);
$customer->setGender($data['gender']);
$customer->setDob($data['dob']);
$customer->setEmail($data['email']);
$customer->save();
$customer = $this->_customerRepoInterface->getById($customer->getId());
$customer->setCustomAttribute('dice_sum', '23');
$customer->setCustomAttribute('place_of_birth','joan');
$this->_customerRepoInterface->save($customer);
but if i try to load the old customer from backend try to save the customer custom attribute, it's working fine:
$customer = $this->_customerRepoInterface->getById($customer->getId());
$customer->setCustomAttribute('dice_sum', '9999');
$customer->setCustomAttribute('place_of_birth','dome');
$this->_customerRepoInterface->save($customer);
edit:
i've tried to add the customer attribute from raw query but it still not working either:
$objectManager = \Magento\Framework\App\ObjectManager::getInstance(); // Instance of object manager
$resource = $objectManager->get('Magento\Framework\App\ResourceConnection');
$connection = $resource->getConnection();
$tableName = $resource->getTableName('customer_entity_varchar');
$sql = "SELECT * FROM " . $tableName . " WHERE attribute_id = '160' and entity_id = '".$data['customer_id']."'";
$result = $connection->fetchAll($sql);
if (empty($result)) {
$sql = "INSERT INTO " . $tableName . " (attribute_id, entity_id, value) Values ('160','".$data['customer_id']."','".$data['place_of_birth']."')";
$connection->query($sql);
}
$sql = "SELECT * FROM " . $tableName . " WHERE attribute_id = '135' and entity_id = '".$data['customer_id']."'";
$result = $connection->fetchAll($sql);
if (empty($result)) {
$sql = "INSERT INTO " . $tableName . " (attribute_id, entity_id, value) Values ('135','".$data['customer_id']."','".$data['dice_sum']."')";
$connection->query($sql);
}
setCustomAttribute
,Just like