大米CMS官网论坛,大米站长联盟,大米站长之家,大米开发者社区

 找回密码
 注册大米会员

QQ登录

只需一步,快速开始

查看: 5327|回复: 0

magento 2.x how to show private content?

[复制链接]

496

主题

773

帖子

7631

积分

超级版主

Rank: 8Rank: 8

积分
7631

授权用户

发表于 2020-4-7 14:56:56 | 显示全部楼层 |阅读模式
本帖最后由 追影 于 2020-4-7 15:04 编辑

Private content section is used to deal with customer private content for cacheable pages.
Experiments
etc/frontend/di.xml
  1. <?xml version="1.0"?>
  2. <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
  3.     <preference for="VendorName\TestModule\Api\Data\AttributeInterface" type="VendorName\TestModule\Model\Attribute"/>
  4.     <type name="Magento\Customer\CustomerData\SectionPoolInterface">
  5.         <arguments>
  6.             <argument name="sectionSourceMap" xsi:type="array">
  7.                 <item name="customsection" xsi:type="string">VendorName\TestModule\CustomerData\CustomSection</item>
  8.             </argument>
  9.         </arguments>
  10.     </type>
  11. </config>
复制代码
  1. <?php
  2. namespace VendorName\TestModule\CustomerData;
  3. use Magento\Customer\CustomerData\SectionSourceInterface;

  4. class CustomSection implements SectionSourceInterface
  5. {
  6.     /**
  7.      * {@inheritdoc}
  8.      */
  9.     public function getSectionData()
  10.     {
  11.         $randString = $this->getNonceStr();
  12.         return [
  13.             'msg' => $randString,
  14.         ];
  15.     }

  16.     protected function getNonceStr($length = 32)
  17.     {
  18.         $chars = 'abcdefghijklmnopqrstuvwxyz0123456789';
  19.         $str   = '';
  20.         for ($i = 0; $i < $length; ++$i) {
  21.             $str .= substr($chars, mt_rand(0, strlen($chars) - 1), 1);
  22.         }
  23.         return $str;
  24.     }
  25. }
复制代码

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

view/frontend/templates/customsection.phtml
  1. <div class="customsection" data-bind="scope: 'section'">
  2.     <p data-bind="text: customsection().msg"></p>
  3. </div>
  4. <script type="text/x-magento-init">
  5.     {
  6.         "*": {
  7.             "Magento_Ui/js/core/app": {
  8.                 "components": {
  9.                     "section": {
  10.                         "component": "VendorName_TestModule/js/section"
  11.                     }
  12.                 }
  13.             }
  14.         }
  15.     }
  16. </script>
复制代码
view/frontend/web/js/section.js
  1. define([
  2.     'uiComponent',
  3.     'Magento_Customer/js/customer-data',
  4.     'Magento_Customer/js/section-config'
  5. ], function (Component, customerData, config) {
  6.     'use strict';

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

  12.             customerData.reload('customsection');
  13.             this.customsection = customerData.get('customsection');
  14.         }
  15.     });
  16. });
复制代码




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



回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册大米会员

本版积分规则

QQ|小黑屋|大米CMS社区 ( 蜀ICP备11002200号-2广告联系:广告联系 

Powered by 大米CMS

© 2010-2020 大米CMS Inc.

快速回复 返回顶部 返回列表