Come impostare e ottenere i dati della sessione del cliente in magento 2


12

Sto lottando con la sessione di Magento 2. Ho creato sotto il file del controller come codice di esempio.

<?php
namespace vendor_name\module_name\Controller\SetGetSession;

use Magento\Framework\App\Action\Action;

class SetGetSession extends Action
{
    protected $customerSession;

    public function _construct(
        \Magento\Customer\Model\Session $customerSession
    ) {
        $this->customerSession = $customerSession;
    }   

    public function execute()
    {

    }
}

Qualcuno può aiutarmi per favore con come assegnare i dati e recuperarli dalla variabile di sessione?

Grazie.

Risposte:


19

È possibile impostare e ottenere la sessione del cliente utilizzando Magento\Customer\Model\Session

protected $customerSession;

public function __construct(   
    \Magento\Customer\Model\Session $customerSession
){
    $this->customerSession = $customerSession;
}

$this->customerSession->setMyValue('test');
$this->customerSession->getMyValue();

O dal gestore degli oggetti.

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$customerSession = $objectManager->create('Magento\Customer\Model\Session');
$customerSession->setMyValue('test');
$customerSession->getMyValue();
  1. Impostazione di informazioni per la sessione del cliente:
$om = \Magento\Framework\App\ObjectManager::getInstance(); $session =
$om->get('Magento\Customer\Model\Session');  
$session->setTestKey('test value');
  1. Ottenere informazioni dalla sessione del cliente:
$om = \Magento\Framework\App\ObjectManager::getInstance();  $session =
$om->get('Magento\Customer\Model\Session');
echo $session->getTestKey();

La sessione estenderà la classe principale Magento\Framework\Session\SessionManagerper gestire la sessione.

Spero che questa risposta ti possa aiutare.


Ricevo un errore come "Chiama a una funzione membro setMyValue () su null" con set fornito e ottieni codice sessione.
— Aniket Shinde,

Controlla la risposta modificata aggiunta dal gestore oggetti.
— Krishna ijjada,

Grazie per l'aiuto. Funziona con il gestore degli oggetti, ma sembra che stia aumentando il tempo di caricamento della pagina. L'ho provato prima di pubblicare la domanda.
— Aniket Shinde,

3

È necessario iniettare \Magento\Customer\Model\Sessionclasse per set e ottenere dati nella sessione del cliente

Utilizzo dell'iniezione delle dipendenze

protected $customerSession;

public function _construct(
    ...
    \Magento\Customer\Model\Session $customerSession
    ...
) {
    ...
    $this->customerSession = $customerSession;
    ...
}   

public function setValue()
{
    return $this->customerSession->setMyValue('YourValue'); //set value in customer session
}

public function getValue()
{
    return $this->customerSession->getMyValue(); //Get value from customer session
}

Utilizzando Object Manager

$objectManager = \Magento\Framework\App\ObjectManager::getInstance(); 
$customerSession = $objectManager->get('Magento\Customer\Model\Session');

$customerSession->setMyValue('YourValue'); //set value in customer session
echo $customerSession->getMyValue(); //Get value from customer session
Utilizzando il nostro sito, riconosci di aver letto e compreso le nostre Informativa sui cookie e Informativa sulla privacy.
Licensed under cc by-sa 3.0 with attribution required.