Magento1 uses core session (core/session), whereas Magento 2 is no longer using core session like earlier.
Magento2 provides five types of sessions:
- Magento\Backend\Model\Session– This session is used for Magento backend.
- Magento\Catalog\Model\Session– Catalog session is used for the frontend for product filters.
- Magento\Checkout\Model\Session– Checkout session is used to store checkout related information.
- Magento\Customer\Model\Session– Customer session is used for customer, frontend login and all other activities.
- Magento\Newsletter\Model\Session– For newsletter data.
In such situations, custom session functionality comes handy. In a custom session, data will not be lost until developer forcefully reset it or session reaches timeout condition. So here are the following steps to create a custom session:
Directory Structure:
- app/code/MyModule/MySession/registration.php
- app/code/MyModule/MySession/etc/module.xml
- app/code/MyModule/MySession/etc/di.xml
- app/code/MyModule/MySession/Model/Session.php
- app/code/MyModule/MySession/Model/Session/Storage.php
To access your custom session in any block,model you can use it in following way:
To access custom session data in checkout related JS, you can mention it in CheckoutConfigProvider and use it in JS file.
You can use custom session all over website’s scope and maintain your data for further action.
Hope this will help you!