据我了解,当您/任何人打开您的网站以在某些地方使用它们(例如维护knock-out js 在 magento 中,例如在结帐页面、迷你购物车等中。
这里我创建了一个模块来实现这一点,通过这个模块,您可以管理应该将多少内容存储到浏览器本地存储中。意味着magento会将内容存储到浏览器的本地存储中,但是如果您希望在限制达到10-20时删除数据,无论您希望使用什么单位然后模块将删除内容从浏览器中,magento 将再次开始将数据存储到本地,从 1 个计数到您想要的限制。
在下面的位置创建一个模块
Magento_root/app/code/{VendorName}/{ModuleName}
在适当的给定位置创建以下文件。
- registration.php
- etc/module.xml
- 查看/前端/布局/catalog_product_view.xml
- view/frontend/templates/product/view/removelocal.phtml
- 查看/前端/web/js/removelocal.js
这里我不放registration.php & module.xml 的内容,假设你已经理解了。对于这个答案,我使用 VendorName => Vendorname 和 ModuelName => Removelocal。这是自定义模块的代码。
catalog_product_view.xml
<?xml version="1.0"?>
<page layout="1column" 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="vendorname.removelocal.content" template="Vendorname_Removelocal::product/view/removelocal.phtml" before="-" />
</referenceContainer>
</body>
</page>
removelocal.phtml
<div class="swatch-opt" data-role="remov-local-content"></div>
<script type="text/x-magento-init">
{
"[data-role=remov-local-content]": {
"Vendorname_Removelocal/js/removelocal": {
}
}
}
</script>
removelocal.js
define([
'jquery'
], function ($){
'use strict';
$.widget('mage.removelocal', {
_init: function () {
if(window.localStorage.product_data_storage)
{
var temp = window.localStorage.product_data_storage;
var myObject = JSON.parse(window.localStorage.product_data_storage);
var count = Object.keys(myObject).length;
if(count >= 10){
window.localStorage.removeItem('product_data_storage');
}
}
}
});
return $.mage.removelocal;
});
注意:完成后请运行以下命令。
php bin/magento module:enable Vendorname_Removelocal
php bin/magento setup:upgrade
php bin/magento setup:static-content:deploy -f
php bin/magento cache:flush