Jak się zalogować Identyfikator klienta

Sep 04 2020

Próbuję uzyskać identyfikator zalogowanego klienta, ale wygląda na to, że nie działa

w moim bloku

<?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();
            }
        }

Teraz w pliku phtml

<?php
$customerId = $customer->getCustomerId();
?>

wszelkie sugestie lub problemy w kodzie dzięki

Zaktualizuj kontroler

<?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(); 
    }


  
} 
 

Odpowiedzi

3 AmitBera Sep 08 2020 at 17:42

Jeśli nie możesz podać identyfikatora klienta w klasie blokowej, gdy strona osiągnęła pełną pamięć podręczną strony.

Musisz przekazać identyfikator klienta jako zmienną kontekstową HTTP .

Widzieć https://devdocs.magento.com/guides/v2.3/extension-dev-guide/cache/page-caching/public-content.html#configure-page-variations

Utwórz klasę wtyczki na 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>

We wtyczce podaj bieżący identyfikator klienta jako zmienną kontekstową 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);
    }
} 

Następnie Inject, \Magento\Framework\App\Http\Contextw swojej klasie bloku i pobierz wartość identyfikatora klienta przez$this->httpContext->getValue('current_cust_id')

1 DhirenVasoya Sep 07 2020 at 13:23

Twój kod blokowy musi polubić ten:

public function getCustomerId()
{
     if($this->_customerSession->isLoggedIn()): return $this->_customerSession->getCustomer()->getId();                        
     endif;
}

A w phtml musisz zadzwonić w ten sposób:

<?php $customerId = $block->getCustomerId(); ?>
MohitPatel Sep 05 2020 at 19:01

Twój kod jest doskonały, zakładam, że wywołanie pliku blokowego w .xmlpliku i pliku wywołania pliku xml .phtml, a poniżej uruchamiasz wszystkie polecenia, takie jak: -

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

Po sprawdzeniu wyników i ponownie nie pokazuj swoich wyników, sprawdź swój dziennik błędu udostępnij mi.

THANKS.

RiteshSantra Sep 05 2020 at 21:46

W pliku Block musisz zmienić kod jak poniżej

Od:

<?php
$customerId = $customer->getCustomerId();
?>

Do:

<?php
$customerId = $block->getCustomerId();
?>
WaqarAli Sep 07 2020 at 17:19

Jeśli klient jest zalogowany, nie możesz uzyskać z sesji. Spróbuj w ten sposób

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;
    }
}
Msquare Sep 07 2020 at 16:45

Użyj tego w swoim pliku układu, w którym dodajesz swój blok.

cacheable = „false”

        <block class="VendoreName\ModuleName\Block\BlockName"
               name="block_index_edit"
               template="VendoreName_ModuleName::edit.phtml"
               cacheable="false" />

Aktualizacja

Twój plik kontrolera

<?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(); 
    }
} 

Twój plik blokowy

<?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();
    }

}

Twój plik phtml

<?php
$customerId = $block->getCustomerId();
echo "Customer ID is ".$customerId;
?>

Mam nadzieję, że to Ci pomoże.