{ Hello Magento 2 }

解决 Magento 2 应用问题,更注重深度挖掘。(ง •̀_•́)ง

0%

private content

Private content section is used to deal with customer private content for cacheable pages.

Experiments

etc/frontend/di.xml

1
2
3
4
5
6
7
8
9
10
11
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<preference for="VendorName\TestModule\Api\Data\AttributeInterface" type="VendorName\TestModule\Model\Attribute"/>
<type name="Magento\Customer\CustomerData\SectionPoolInterface">
<arguments>
<argument name="sectionSourceMap" xsi:type="array">
<item name="customsection" xsi:type="string">VendorName\TestModule\CustomerData\CustomSection</item>
</argument>
</arguments>
</type>
</config>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
<?php
namespace VendorName\TestModule\CustomerData;
use Magento\Customer\CustomerData\SectionSourceInterface;

class CustomSection implements SectionSourceInterface
{
/**
* {@inheritdoc}
*/
public function getSectionData()
{
$randString = $this->getNonceStr();
return [
'msg' => $randString,
];
}

protected function getNonceStr($length = 32)
{
$chars = 'abcdefghijklmnopqrstuvwxyz0123456789';
$str = '';
for ($i = 0; $i < $length; ++$i) {
$str .= substr($chars, mt_rand(0, strlen($chars) - 1), 1);
}
return $str;
}
}

view/frontend/layout/catalog_product_view.xml

1
2
3
4
5
6
7
8
9
<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<body>
<referenceContainer name="content">
<block class="Magento\Framework\View\Element\Template" name="custom_section" template="VendorName_TestModule::customsection.phtml">
</block>
</referenceContainer>
</body>
</page>

view/frontend/templates/customsection.phtml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
test section
<div class="customsection" data-bind="scope: 'section'">
<p data-bind="text: customsection().msg"></p>
</div>
<script type="text/x-magento-init">
{
"*": {
"Magento_Ui/js/core/app": {
"components": {
"section": {
"component": "VendorName_TestModule/js/section"
}
}
}
}
}
</script>

view/frontend/web/js/section.js

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
define([
'uiComponent',
'Magento_Customer/js/customer-data',
'Magento_Customer/js/section-config'
], function (Component, customerData, config) {
'use strict';

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

customerData.reload('customsection');
this.customsection = customerData.get('customsection');
}
});
});

Ok, every time we refresh catalog detail page, we will see randonm string. Now, I want the random string to be refreshed when customer add a product to cart. etc/frontend/sections.xml

1
2
3
4
5
6
7
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Customer:etc/sections.xsd">
<action name="checkout/cart/add">
<section name="customsection"/>
</action>
</config>

References

DevDocs - Private content Sections in Magento 2 Magento 2: how do customer sections / sections.xml work? During cacheable page rendering Magento is cleaning all data that can be specific to a particular user (unsetting private data). That’s why session is empty during rendering. The responsibility of cleaning private data lies on several depersonalization plugins. Customer session for example is cleaned by \Magento\Customer\Model\Layout\DepersonalizePlugin. Session depersonalization in Magento 2, why and how? customer session does not work except customer page - Magento 2