Sections in Magento 2

In Magento 2, a lot work has been done in improving the response time and Sections are a part of it. Magento sets customer related data in local storage of browser in key value pair. Each key is known as section. In this post we will see How we can create custom section in our module and use the data in the section in our template file.

For this tutorial I am using Webkul_Section module. For creating our custom section we need to give entry in etc/frontend/di.xml file.

<config xmlns:xsi="" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <type name="Magento\Customer\CustomerData\SectionPoolInterface">
            <argument name="sectionSourceMap" xsi:type="array">
                <item name="customsection" xsi:type="string">Webkul\Section\CustomerData\CustomSection</item>

You can see we have given item name=”customsection” and it define its data source as Webkul\Section\CustomerData\CustomSection.

Now, we will create getSectionData() in Webkul\Section\CustomerData\CustomSection.php file. This function is responsible for the setting data in different sections.

namespace Webkul\Section\CustomerData;
use Magento\Customer\CustomerData\SectionSourceInterface;

class CustomSection implements SectionSourceInterface
     * {@inheritdoc}
    public function getSectionData()
        return [
            'msg' =>'Data from section',

Now, to check everything is working fine till here, you can check local storage of your browser. If you are using chrome then you can see it under Application tab as in screenshot below.

Now, we will fetch the message that we set in our custom section. For this we call our template file customsection.phtml on product view page.

<?xml version="1.0"?>

<page xmlns:xsi="" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
        <referenceContainer name="content">
            <block class="Magento\Framework\View\Element\Template"  name="custom_section" template="Webkul_Section::customsection.phtml">

Template file: customsection.phtml

<div class="wk-customsection"data-bind="scope: 'section'">
    <p data-bind="text: customsection().msg"></p>
<script type="text/x-magento-init">
        "*": {
            "Magento_Ui/js/core/app": {
                "components": {
                    "section": {
                        "component": "Webkul_Section/js/section"

In template file, you can see we have bind the div.wk-customsection with the component section. Now, we will create the Webkul/Section/frontend/web/js/section.js file.

], function (Component, customerData) {
    'use strict';
    return Component.extend({
        /** @inheritdoc */
        initialize: function () {
            this.customsection = customerData.get('customsection'); //pass your custom section name

Now, refresh your cache and when you will visit any product page you can see the message set in section.

In section.js file we have used Magento_Customer/js/customer-data as this js file is responsible for setting and getting section data. Section data gets updated when ajax call with post, put, delete requests are made.

Advantages of using section

When caching is enabled the whole page content is cached so this helps us to dynamically manage the components of page. For example, the complete page is cached, Now when the customer logs in only the components get updated with the value from the section as you can see customer name, cart, wish list are all managed by section.

All section data is fetched in 1 single ajax call, hence the number of ajax requests are reduced to a huge number.

You can also manage when to update the section data by using etc/frontend/sections.xml

That’s all for this post. Happy Caching. 🙂

. . .


Add Your Comment

Be the first to comment.