好的,这是迄今为止我提出的最好的一个。我仍然认为它只是部分解决了,我认为我可以从 magento 社区获得更多帮助,但现在它运行良好。
将此脚本包含到 custom.phtml 标头中:
<script type="text/javascript">
function AJAX(){
try{
xmlHttp=new XMLHttpRequest(); // Firefox, Opera 8.0+, Safari
return xmlHttp;
}
catch (e){
try{
xmlHttp=new ActiveXObject("Msxml2.XMLHTTP"); // Internet Explorer
return xmlHttp;
}
catch (e){
try{
xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
return xmlHttp;
}
catch (e){
alert("Your browser does not support AJAX.");
return false;
}
}
}
}
// Timestamp for preventing IE caching the GET request (common function)
function fetch_unix_timestamp()
{
return parseInt(new Date().getTime().toString().substring(0, 10))
}
// Reload div with php
function refreshdiv_timediv(){
var seconds = '3';
var divid = "ajax";
var url = "routes/customer_needs_to_see_this.php";
var xmlHttp_one = AJAX();
// No cache
var timestamp = fetch_unix_timestamp();
var nocacheurl = url+"?t="+timestamp;
// The code...
xmlHttp_one.onreadystatechange=function(){
if(xmlHttp_one.readyState==4){
document.getElementById(divid).innerHTML=xmlHttp_one.responseText;
setTimeout('refreshdiv_timediv()',seconds*1000);
}
}
xmlHttp_one.open("GET",nocacheurl,true);
xmlHttp_one.send(null);
}
// Start the refreshing process
window.onload = function startrefresh(){
setTimeout('refreshdiv_timediv()',seconds*1000);
}
</script>
然后我将 customer_needs_to_see_this.php 放到 www 根目录中的文件夹路由中。上面的脚本会自动刷新 div “ajax”,在 ajax-div 里面有一个 php include routes/user_needs_to_see_this.php。带include的div位于cart_sidebar.phtml
我不得不将文件放在 magento 的模板文件之外,因为我不能像 ../template/checkout/customer_needs_to_see_this.php 这样包含。当它们与视图(phtml 文件)文件位于同一文件夹中时包括工作,否则它似乎不起作用。我也无法让脚本从 magento 的模板文件中获取 user_needs_to_see_this.php。
现在,当客户在 custom.phtml 页面上时,它每 3 秒更新一次。我认为没关系,因为它只是阅读和制作饼干,所以它并不重。以后可能会改成onclick。
我得到了代码here。
还有很多其他类似的例子,但上面的例子似乎涵盖了大部分基础。