|
本帖最后由 追影 于 2020-4-7 15:04 编辑
Private content section is used to deal with customer private content for cacheable pages. Experimentsetc/frontend/di.xml - <?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>
复制代码- <?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- <?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- <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- 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 - <?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>
复制代码
|
|