今天,我们将讨论如何通过运行代码快速设置和取消session。随着Magento 2的定制化开发和定制,有时候获取Session信息是非常有必要的。对于 Magento 2 商店管理员来说,了解session并随时调整它们以在一定时间内获取重要信息是必要的。
Magento 2 session类型
那么 Magento 2 中的session是什么?
session是一种在 Magento 2 商店的每个页面上排序和存储变量的方法。它允许您的用户在您的商店中临时存储一些值,例如购物车中的商品、个人信息等。
当用户在您的商店中注册帐户时,session允许您存储他们的信息,例如名字、姓氏或 ID。然后,通过在 Magento 2 中设置自定义session变量,您可以存储更多附加信息。
现在,让我们看一下 Magento 2 中的session类型列表:
vendor/magento/module-catalog/Model/Session.php
 
vendor/magento/module-newsletter/Model/Session.php
 
vendor/magento/module-persistent/Model/Session.php
 
vendor/magento/framework/Message/Session.php
 
vendor/magento/module-customer/Model/Session.php
 
vendor/magento/module-backend/Model/Session.php
 
vendor/magento/module-checkout/Model/Session.php
查看以下方法,可以随时轻松快速地在 Magento 2 中设置或取消session。
在 Magento 2 中设置session
现在,您将开始通过以下代码调用 Catalog、Customer 和 Checkout session。
<?php
namespace Example\HelloWorld\Block;
class HelloWorld extends \Magento\Framework\View\Element\Template
{    
    protected $_catalogSession;
    protected $_customerSession;
    protected $_checkoutSession;
        
    public function __construct(
        \Magento\Backend\Block\Template\Context $context,        
        \Magento\Catalog\Model\Session $catalogSession,
        \Magento\Customer\Model\Session $customerSession,
        \Magento\Checkout\Model\Session $checkoutSession,
        array $data = []
    )
    {        
        $this->_catalogSession = $catalogSession;
        $this->_checkoutSession = $checkoutSession;
        $this->_customerSession = $customerSession;
        parent::__construct($context, $data);
    }
    
    public function _prepareLayout()
    {
        return parent::_prepareLayout();
    }
        
    public function getCatalogSession() 
    {
        return $this->_catalogSession;
    }
    
    public function getCustomerSession() 
    {
        return $this->_customerSession;
    }
    
    public function getCheckoutSession() 
    {
        return $this->_checkoutSession;
    }    
}
?>
然后,通过 Catalog、Customer 和 Checkout session,可以从.phtml文件中设置它们。
$block->getCatalogSession()->setMyName('Mageplaza');
echo $block->getCatalogSession()->getMyName() . '<br />'; // output: Mageplaza
 
$block->getCheckoutSession()->setTestData('Hello World');
echo $block->getCheckoutSession()->getTestData() . '<br />'; // output: Hello World
 
$block->getCheckoutSession()->setTestHello('Test Hello Value');
echo $block->getCheckoutSession()->getTestHello() . '<br />'; // output: Test Hello Value
在 Magento 2 中取消Session
如果您想取消设置这些session,请按以下步骤操作:
$block->getCatalogSession()->unsMyName();
$block->getCheckoutSession()->unsTestData();
$block->getCustomerSession()->unsTestHello();
具体来说,客户session允许收集客户信息,例如客户姓名和电子邮件。
// get customer data
if ($block->getCustomerSession()->isLoggedIn()) {
    $customerId = $block->getCustomerSession()->getCustomerId();
    $customerData = $block->getCustomerSession()->getCustomer();
    echo $customerId . '<br />';
    echo $customerData->getFirstname() . ' ' . $customerData->getLastname() . '<br />';
    echo $customerData->getEmail() . '<br />';
    print_r($block->getCustomerSession()->getCustomer()->getData());
}
结帐session将显示报价信息。
// get checkout session data
echo $block->getCheckoutSession()->getQuoteId();
print_r($block->getCheckoutSession()->getQuote()->getData());

