本文介绍如何在Magento 2中创建和销毁不同类型的会话(结帐,客户,目录)。
如下所示,Magento 2中存在不同类型的会话类。
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
在下面的示例代码中,我将处理Catalog,Customer和Checkout会话。
依赖注入和对象管理器的编码方式如下所示。
使用依赖注入(DI)
<?php
namespace Chapagain\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;
}
}
?>
现在,我们设置并从模板(.phtml)文件获取会话。
$block->getCatalogSession()->setMyName('Mukesh Chapagain');
echo $block->getCatalogSession()->getMyName() . '<br />'; // output: Mukesh Chapagain
$block->getCheckoutSession()->setTestData('123');
echo $block->getCheckoutSession()->getTestData() . '<br />'; // output: 123
$block->getCustomerSession()->setTestHello('456');
echo $block->getCustomerSession()->getTestHello() . '<br />'; // output: 456
取消会话
$block->getCatalogSession()->unsMyName();
$block->getCheckoutSession()->unsTestData();
$block->getCustomerSession()->unsTestHello();
从客户会话中,我们还可以获取客户信息,例如客户名称和电子邮件。
// 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());
}
从结帐会话中,我们可以获取报价信息。
// get checkout session data
echo $block->getCheckoutSession()->getQuoteId();
print_r($block->getCheckoutSession()->getQuote()->getData());
使用对象管理器
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$catalogSession = $objectManager->get('\Magento\Catalog\Model\Session');
$customerSession = $objectManager->get('\Magento\Customer\Model\Session');
$checkoutSession = $objectManager->get('\Magento\Checkout\Model\Session');
// set session variables and their value
$catalogSession->setMyName('Mukesh Chapagain');
$checkoutSession->setTestData('123');
$customerSession->setTestHello('456');
// print session variables value
echo $catalogSession->getMyName() . '<br />'; // output: Mukesh Chapagain
echo $checkoutSession->getTestData() . '<br />'; // output: 123
echo $customerSession->getTestHello() . '<br />'; // output: 456
// Unset session
$catalogSession->unsMyName();
$checkoutSession->unsTestData();
$customerSession->unsTestHello();
希望这可以帮助。谢谢