3

In my mobile number update module, logged user userId and mobilenumber set to .phtml hidden fields, but some times its not works properly ! please check my code

app/code/{vendor}/{module}/Block/Account/Dashboard/Updatemobile.php

<?php
namespace {vendor}\{module}\Block\Account\Dashboard;

use Magento\Framework\View\Element\Template\Context;

class Updatemobile extends \Magento\Framework\View\Element\Template
{
    protected $_customersession;

    public function __construct(Context $context, \Magento\Customer\Model\Session $customerSession)
    {
        $this->_customersession = $customerSession;
        parent::__construct($context);
    }

    public function getCustomerid()
    {
        $customerId = 0;
        if ($this->_customersession->isLoggedIn()) {
            $customerId = $this->_customersession->getCustomer()->getId();
        }
        return $customerId;
    }

    public function getMobilenumber()
    {
        $mobileNumber = 0;
        if ($this->_customersession->isLoggedIn()) {
            $mobileNumber = $this->_customersession->getCustomer()->getMobilenumber();
        }
        return $mobileNumber;
    }
}

app/code/{vendor}/{module}/view/frontend/templates/updateMobile.phtml

<div class="update_mob">
    <div class="update_mob_text">
        <input type="text" value="<?php echo $block->getMobilenumber(); ?>" class="updatemobnumber"/>
        <input type="hidden" value="<?php echo $this->getUrl("mobilelogin/index/updatemobilenumber"); ?>" class="url" />
        <input type="hidden" value="<?php echo $block->getCustomerid();?>" class="userId" />
        <span><?php echo __("You will get all the sms on this number"); ?></span>
    </div>
    <div class="update_mob_btn">
        <input type="button" value="Update Mobile Number" class="updatemobbtn action primary"/>
         <img class="sms-request-load" src="<?php echo $block->getViewFileUrl('images/loader-1.gif'); ?>" alt="Please Wait..." id="updatemobile_img"/>
    </div>
</div>

app/code/{vendor}/{module}/Controller/Index/Updatemobilenumber.php

<?php
namespace {Vendor}\{module}\Controller\Index;

use Magento\Framework\App\Action\Context;
use Magento\Framework\Controller\ResultFactory;
use Magento\Customer\Model\CustomerFactory;
use Magento\Customer\Model\Customer;
use Magento\Customer\Model\Data\Customer as CustomerData;
use Magento\Customer\Model\ResourceModel\Customer as CustomerResource;
use Magento\Customer\Model\ResourceModel\CustomerFactory as CustomerResourceFactory;

class Updatemobilenumber extends \Magento\Framework\App\Action\Action
{
    protected $_resultPageFactory;
    protected $customerFactory;
    protected $customerData;
    protected $customer;
    protected $customerResourceFactory;
    protected $customerResource;
    public function __construct(
        Context $context,
        CustomerFactory $customerFactory,
        Customer $customer,
        CustomerData $customerData,
        CustomerResource $customerResource,
        CustomerResourceFactory $customerResourceFactory,
         $data = array()
        )
    {
        parent::__construct($context);

        $this->customerFactory  = $customerFactory;
        $this->customer = $customer;
        $this->customerData = $customerData;
        $this->customerResourceFactory = $customerResourceFactory;
        $this->customerResource = $customerResource;

    }
    public function execute()
    {
        $mobile = (string)$this->getRequest()->get('mobile');
        $customerId = (string)$this->getRequest()->get('userId');
        $this->customerData = $this->customer->getDataModel();
        $this->customerData->setId($customerId);
        $this->customerData->setCustomAttribute('mobilenumber', $mobile);
        $this->customer->updateData($this->customerData);
        $this->customerResource = $this->customerResourceFactory->create();
        if ($mobile != "") {
            $this->customerResource->saveAttribute($this->customer, 'mobilenumber');
        }
        $this->messageManager->addSuccess("Mobile Number Update successfully");

        $data = 1;     
        $resultJson = $this->resultFactory->create(ResultFactory::TYPE_JSON);
        $resultJson->setData($data);
        return $resultJson;

    }
}
  • try removing $mobileNumber = 0; and $customerId = 0; – Mohit Rane Jul 19 at 6:57
  • Can you please upload code of your updatemobilenumber controller? – Rohan Hapani Jul 19 at 6:58
  • try \Magento\Customer\Model\SessionFactory instead of \Magento\Customer\Model\Session I have Done same thing Yesterday – Waqar Ali Jul 19 at 7:00
  • @ Rohan Hapani please check updated Question – Shafeel Sha Jul 19 at 7:02
  • It seems like your first hidden field value always display 1. – Rohan Hapani Jul 19 at 7:03
2

You're returning the value of $customerId and $mobileNumber outside of

if ($this->_customersession->isLoggedIn()) {}

that's why it's returning value 0 since you're initializing with 0 . Update this code in

app/code/{vendor}/{module}/Block/Account/Dashboard/Updatemobile.php

    public function getCustomerid()
    {
        if ($this->_customersession->isLoggedIn()) {
             return $this->_customersession->getCustomer()->getId();
        }
        return false;
    }

    public function getMobilenumber()
    {
        if ($this->_customersession->isLoggedIn()) {
             return $this->_customersession->getCustomer()->getMobilenumber();
        }
        return false;
    }

UPDATE

The issue is in $_customersession property

Underscore isn't needed in your $_customersession property you can also try to define property without underscore, it's not needed.

Change it to

$customerSession

from

$_customersession

If you are facing the cache issue, you can use in your layout file

cacheable="false";

Hope it Helps.

  • hi @Mohit Rane , thank you for your response , but its did not works for me i think my logged session values not correctly fetch – Shafeel Sha Jul 19 at 8:36
  • can you print this $this->getRequest()->getParams(); Or $this->getRequest()->getPostValue(); see what it prints – Mohit Rane Jul 19 at 8:40
  • where ? i keep this code – Shafeel Sha Jul 19 at 8:48
  • in your controller Updatemobilenumber.php file – Mohit Rane Jul 19 at 8:49
  • /index/updatemobilenumber/?mobile=123456798&userId=0&_=1563526335045 500 (Internal Server Error) it shows when i click update mobile number – Shafeel Sha Jul 19 at 8:53

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.