0

I have a native Magento 2.2. I added a static attribute for customer named ficalcode.

On the backoffice, no problem, I can update the attribute. From the frontend it's more complex.

I'm logged with customer #2, on the execute of my controller these line do not modify the fiscalcode :

 67     public function execute()
 68     {
 69         try {
 70             //$customer = $this->customerFactory->create()->load($this->customerSession->getCustomerId());
 71             $customer = $this->customerFactory->create()->load(2);
 72             $customer
 73                 ->setFiscalcode($this->getRequest()->getPost('fiscalcode'))
 74                 ->setCustomAttribute('fiscalcode', $this->getRequest()->getPost('fiscalcode'))
 75                 ->save();
 76 

But if I change the id of the customer for 1 and if I'm still logged with customer #2, all is working :

 67     public function execute()
 68     {
 69         try {
 70             //$customer = $this->customerFactory->create()->load($this->customerSession->getCustomerId());
 71             $customer = $this->customerFactory->create()->load(1);
 72             $customer
 73                 ->setFiscalcode($this->getRequest()->getPost('fiscalcode'))
 74                 ->setCustomAttribute('fiscalcode', $this->getRequest()->getPost('fiscalcode'))
 75                 ->save();
 76 

Does anyone know the reason?

0

I found the reason.

After there line I do something like :

 84             $document = $this->documentFactory->create()
 85                 ->setCustomerId($customer->getId())
 86                 ->setExpireDate($this->getRequest()->getPost('expire_date'))
 87                 ->setExtensions(implode(',', $extensions))
 88                 ->save();

documentFactory is a model with an after save. On the afterSave it load the customer and save it. The solution is to asociate the customer and not the customerId and make a getCustomer function.

The code become :

 84             $document = $this->documentFactory->create()
 85                 ->setCustomer($customer)
 86                 ->setExpireDate($this->getRequest()->getPost('expire_date'))
 87                 ->setExtensions(implode(',', $extensions))
 88                 ->save();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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