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;
}
}
$mobileNumber = 0;
and$customerId = 0;
\Magento\Customer\Model\SessionFactory
instead of\Magento\Customer\Model\Session
I have Done same thing Yesterday