Skip to main content

How to use Private Content or Sections in Magento 2?

We will show here how we can display a private content in a full cached page, it is very useful if you want to display information per customer 
Magento loads sections by AJAX request to /customer/section/load/ and caches loaded data in the browser local storage under the key mage-cache-storage. Magento tracks when some section is changed and load updated section automatically.

We will use this Vendor/Module as names

Step 1: Define a custom section
We will define a custom section in the di.xml file by adding a new section into sections pool.

Create di.xml file in app/code/Vendor/Module/etc/frontend directory.

<?xml version="1.0"?>
<config xmlns:xsi=""
    <type name="Magento\Customer\CustomerData\SectionPoolInterface">
            <argument name="sectionSourceMap" xsi:type="array">
                <item name="custom_section" xsi:type="string">Vendor\Module\CustomerData\CustomSection</item>

Next, create CustomSection.php file in app/code/Vendor/Module/CustomerData directory.

namespace Vendor\Module\CustomerData;
use Magento\Customer\CustomerData\SectionSourceInterface;

class CustomSection implements SectionSourceInterface
    public function getSectionData()
        return [
            'customdata' => "put here your dynamic data",

In getSectionData() method we will define our data that we want to store in section.

Step 2: Display custom section data in frontend
We will display our custom section data in the product view page. You can get section data in any Magento pages.

Next, we will create catalog_product_view.xml in app/code/Vendor/Module/view/frontend/layout directory.


<?xml version="1.0"?>
<page xmlns:xsi=""
        <referenceContainer name="">
            <block class="Magento\Catalog\Block\Product\View" name="aureatelabs_custom_section" after="-"


Next, we will create a custom_section.phtml in app/code/Vendor/Module/view/frontend/templates directory.

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

Next, We will create section.js in app/code/Vendor/Module/view/frontend/web/js directory.

], function (Component, customerData) {
    'use strict';

    return Component.extend({
        initialize: function () {
            this.customsection = customerData.get('custom_section');

Now run the below command and check on any product view page.

php bin/magento setup:upgrade
php bin/magento setup:di:compile
php bin/magento cache:flush