Cách đăng nhập ID khách hàng
tôi đang cố gắng lấy id khách hàng của một khách hàng đã đăng nhập nhưng nó dường như không hoạt động
trong khối của tôi
<?php
namespace Vendor\Module\Block;
use Magento\Framework\View\Element\Template;
use Magento\Backend\Block\Template\Context;
class Rfq extends \Magento\Framework\View\Element\Template
{
protected $_customerSession; public function __construct(Context $context,
\Magento\Customer\Model\Session $customerSession, array $data = [])
{
parent::__construct($context, $data);
$this->_customerSession = $customerSession;
}
public function getCustomerId()
{
if($this->_customerSession->isLoggedIn()): return $this->_customerSession->getCustomer()->getId();
endif;
}
public function execute()
{
$this->_view->loadLayout(); $this->_view->getLayout()->initMessages();
$this->_view->renderLayout();
}
}
Bây giờ trong tệp phtml
<?php
$customerId = $customer->getCustomerId();
?>
bất cứ đề nghị hoặc vấn đề trong mã thankss
Cập nhật bộ điều khiển
<?php
namespace Vendor\Module\Controller\Index;
use Magento\Framework\App\Action\Action;
use Magento\Framework\App\Action\Context;
use Magento\Framework\App\RequestInterface;
use Magento\Framework\View\Result\PageFactory;
class Customer extends \Magento\Framework\App\Action\Action
{
protected $resultPageFactory;
public function __construct(
Context $context, PageFactory $resultPageFactory
) {
$this->resultPageFactory = $resultPageFactory;
parent::__construct($context); } public function execute() { return $this->resultPageFactory->create();
$this->_view->loadLayout(); $this->_view->renderLayout();
}
}
Trả lời
Nếu bạn không thể id khách hàng ở lớp khối của mình khi trang của bạn truy cập ở bộ nhớ cache toàn trang.
Bạn phải chuyển id khách hàng làm biến ngữ cảnh HTTP .
Xem https://devdocs.magento.com/guides/v2.3/extension-dev-guide/cache/page-caching/public-content.html#configure-page-variations
Tạo một lớp plugin trên Magento\Framework\App\Action\AbstractAction.
<?xml version="1.0" ?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<type name="Magento\Framework\App\Action\AbstractAction">
<plugin name="add_customr_id_full_page" disabled="false" sortOrder="20"
type="Devamitbera\Module\Plugin\Framework\App\Action"/>
</type>
</type>
</config>
Trên plugin, hãy chuyển id khách hàng hiện tại làm biến ngữ cảnh http:
<?php
declare(strict_types=1);
namespace Devamitbera\Module\Plugin\Framework\App\Action;
use Magento\Customer\Model\Context as CustomerContext;
use Magento\Framework\Exception\NoSuchEntityException;
class AbstractAction
{
/**
* @var \Magento\Customer\Api\CustomerRepositoryInterface
*/
private $customerRepository; /** * @var \Magento\Customer\Model\Session */ private $customerSession;
/**
* @var \Magento\Framework\App\Http\Context
*/
private $httpContext; public function __construct( \Magento\Customer\Model\Session $customerSession,
\Magento\Framework\App\Http\Context $httpContext, \Magento\Customer\Api\CustomerRepositoryInterface $customerRepository
) {
$this->customerSession = $customerSession;
$this->httpContext = $httpContext;
$this->customerRepository = $customerRepository;
}
public function aroundDispatch(
\Magento\Framework\App\ActionInterface $subject, callable $proceed,
\Magento\Framework\App\RequestInterface $request ) { $customerId = $this->customerSession->getCustomerId(); $this->httpContext->setValue(
'current_cust_id',
$customerId, 0 ); return $proceed($request);
}
}
Sau khi Inject đó, \Magento\Framework\App\Http\Contexttrên lớp khối của bạn và nhận giá trị id khách hàng này bằng cách$this->httpContext->getValue('current_cust_id')
Mã khối của bạn cần phải thích mã này:
public function getCustomerId()
{
if($this->_customerSession->isLoggedIn()): return $this->_customerSession->getCustomer()->getId();
endif;
}
Và trong phtml, bạn cần gọi như thế này:
<?php $customerId = $block->getCustomerId(); ?>
Code của bạn là hoàn hảo. Tôi giả sử lệnh gọi tệp khối của bạn trong .xmltệp của bạn và trong tệp gọi tệp xml. .phtmlVà bạn chạy bên dưới tất cả các lệnh như: -
remove your var/cache
remove your var/view_preprocessed
php bin/magento setup:di:compile
php bin/magento s:up
php bin/magento s:s:d -f
php bin/magento c:c
php bin/magento c:f
Sau khi bạn kiểm tra đầu ra của mình Và một lần nữa không hiển thị đầu ra của bạn, hãy kiểm tra lỗi nhật ký của bạn chia sẻ với tôi.
THANKS.
Trong tập tin Block bạn cần thay đổi mã như bên dưới
Từ:
<?php
$customerId = $customer->getCustomerId();
?>
Đến:
<?php
$customerId = $block->getCustomerId();
?>
Nếu khách hàng đã đăng nhập thì bạn không thể nhận Từ phiên Hãy thử cách này
public function __construct(
\Magento\Framework\App\Http\Context $customerContext, \Magento\Customer\Model\SessionFactory $customer
{
$this->customer = $customer;
$this->customerContext = $customerContext;
}
public function getCustomerID()
{
$customerLoggedIn= $this->customerContext->getValue(\Magento\Customer\Model\Context::CONTEXT_AUTH);
if($customerLoggedIn){ $customerId = $this->customer->create()->getCustomer()->getId(); return $customerId;
}
}
Sử dụng điều này vào tệp bố cục của bạn nơi bạn thêm khối của mình.
cacheable = "false"
<block class="VendoreName\ModuleName\Block\BlockName"
name="block_index_edit"
template="VendoreName_ModuleName::edit.phtml"
cacheable="false" />
Cập nhật
Tệp điều khiển của bạn
<?php
namespace Vendor\Module\Controller\Index;
use Magento\Framework\App\Action\Action;
use Magento\Framework\App\Action\Context;
use Magento\Framework\App\RequestInterface;
use Magento\Framework\View\Result\PageFactory;
class Customer extends \Magento\Framework\App\Action\Action
{
protected $resultPageFactory; public function __construct( Context $context,
PageFactory $resultPageFactory ) { $this->resultPageFactory = $resultPageFactory; parent::__construct($context);
}
public function execute()
{
return $this->resultPageFactory->create(); $this->_view->loadLayout();
$this->_view->renderLayout();
}
}
Tệp khối của bạn
<?php
namespace VendoreName\ModuleName\Block;
use Magento\Framework\View\Element\Template;
class Rfq extends Template
{
protected $request;
protected $sessionFactory; public function __construct( Template\Context $context,
\Magento\Framework\App\Request\Http $request, \Magento\Customer\Model\SessionFactory $sessionFactory,
array $data = [] ) { parent::__construct($context, $data); $this->sessionFactory = $sessionFactory; $this->request = $request; } public function getCustomerId() { // get customer id here $customerSession = $this->sessionFactory->create(); return $customerSession->getCustomer()->getId();
}
}
Tệp phtml của bạn
<?php
$customerId = $block->getCustomerId();
echo "Customer ID is ".$customerId;
?>
Tôi hy vọng cái này sẽ giúp bạn.