2

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);
      }
5
  • For backend system, you donot need to set use setCustomAttribute,Just like
    – Amit Bera
    Commented Sep 17, 2018 at 6:48
  • Did you checked my answer? Commented Sep 17, 2018 at 8:31
  • Can you add $customer->save() just after $customer->setCustomAttribute('place_of_birth','joan'); Commented Sep 17, 2018 at 8:55
  • Try this magento.stackexchange.com/a/200365/48571 Commented Sep 17, 2018 at 8:59
  • not working either Commented Sep 17, 2018 at 9:48

2 Answers 2

0

I am using the Data Model to save my custom customer attribute value code is given below.

 $customer = $this->_customerRepoInterface->getById($customer->getId())**-getDataModel();**
 $customer->setCustomAttribute('dice_sum', '9999');
 $customer->setCustomAttribute('place_of_birth','dome');
 $this->_customerRepoInterface->save($customer);
-1

BEST SOLUTION:

If you have used service provider class means RepositoryInterface, Then I suggest to you use service provider for create the new customer.

<?php

namespace Exchanges\Test\Test;

/**
 * Description of customerCustomAttribute
 *
 */
class customerCustomAttribute {

    /**
     * @var \Magento\Customer\Api\CustomerRepositoryInterface
     */
    protected $customer;

    /**
     * @var \Magento\Customer\Api\Data\CustomerInterface
     */
    protected $customerData;

    public function __construct(
    \Magento\Customer\Api\CustomerRepositoryInterface $customer,
    \Magento\Customer\Api\Data\CustomerInterface $customerData      
    ) {

        $this->customerData = $customerData;
        $this->customer = $customer;
    }
    public function saveCustomer()
    {
        $customer =  $this->customerData;
        $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->setCustomAttribute('dice_sum', '23');
        $customer->setCustomAttribute('place_of_birth','joan');
        $this->customer->save($customer);

    }        
}
3
  • still not working Commented Sep 17, 2018 at 7:22
  • have you use service provide RepositoryInterface?.
    – Amit Bera
    Commented Sep 17, 2018 at 8:22
  • yes i've used it Commented Sep 17, 2018 at 8:25

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.