How to Get, Set and Unset Custom Session Variable in Magento 2
A session is a way of storing variables and making them available on multiple pages on a website. In other words, a session is a temporary object that is created on the server for each Magento 2 store users to store some values, i.e., items in a cart.
The session enables you to build more customized applications and increase the appeal of your website visually, and it is a way to preserve certain data across subsequent accesses.
Sometimes while Magento 2 module customization or module development of Magento 2, you may need to get set and unset custom session variable in Magento 2.
For instance, once the user registers in our store, it stores the customer’s first name, last name, id using session. Still, if you want to store additional information of the customer, i.e., age, then you have to use the method to get and set a custom session variable in Magento 2.
Suppose an owner wants a functionality where the popup should be displayed until the customer is logged in to the Magento 2 store. In that case, you have to unset the session variable when the customer logs out.
Check out the below method for the same:
Method to Get Set and Unset Custom Session Variable in Magento 2
- Use the below code in your block file.
12345678910111213141516171819202122232425262728293031<?phpnamespace Vendor\Module\Block\Session;use Magento\Backend\Block\Template\Context;use Magento\Checkout\Model\Session as checkoutSession;use Magento\Customer\Model\Session;protected $checkoutSession;protected $customerSession;public function __construct(Context $context,Session $customerSession,checkoutSession $checkoutSession,array $data = []){$this->checkoutSession = $checkoutSession;$this->customerSession = $customerSession;parent::__construct($context, $data);}public function getCheckoutSession(){return $this->checkoutSession;}public function getCustomerSession(){return $this->customerSession;} - Now, we set and get session from template file.
12345$block->getCheckoutSession()->setCustomPrice('24.40');echo $block->getCheckoutSession()->getCustomPrice(); // To get custom price value$block->getCustomerSession()->setCustomName('test');echo $block->getCustomerSession()->getCustomName(); // To get custom name value - To Unset session variable
12$block->getCheckoutSession()->unsCustomPrice();$block->getCustomerSession()->unsCustomName();
You can also find all details from customer session, checkout session, quote related details and sessions of catalog , backend, newsletter.
That’s it.
Any doubts? Do mention them in the Comments section below.
I would be glad to help you out.
Also, please share the solution with Magento Community via social media.
Thank you.
Sanjay Jethva
Sanjay is the co-founder and CTO of Meetanshi with hands-on expertise with Magento since 2011. He specializes in complex development, integrations, extensions, and customizations. Sanjay is one the top 50 contributor to the Magento community and is recognized by Adobe.
His passion for Magento 2 and Shopify solutions has made him a trusted source for businesses seeking to optimize their online stores. He loves sharing technical solutions related to Magento 2 & Shopify.
Prev
Solved: failed to open stream: No such file or directory in composer autoload_real.php in Magento 2
Fixed: Content Security Policy Warnings in Magento 2
Next