【问题标题】:Need to display user's choices realtime in magento需要在magento中实时显示用户的选择
【发布时间】:2011-12-05 19:57:15
【问题描述】:

我正在开发一个 magento 网上商店,其中有一个页面,客户可以在其中选择他/她想要取货的路线。选择是双重的,目前有两组不同的按钮,因此没有复选框或单选按钮。首先,用户选择日期,然后选择地点。

现在我正在调用一个 javascript 函数,该函数在按下按钮时加载一个 cookie,然后重新加载页面并将用户返回到一个锚链接,这样用户在进行第一次选择时就不会感到困惑。当页面重新加载时,侧边栏包含读取 cookie 内容的 php。这感觉不是很直观,我相信有更好的方法来做到这一点。用户所做的选择应显示在侧边栏购物车上,这是一个 .phtml 文件。只是一个简单的“你选择了路线 x”就足够了,现在我有一个回声,但页面必须先重新加载。

简而言之,当用户做出选择时,侧边栏应更新有关选择的信息,而无需页面重新加载本身并在锚链接的帮助下返回位置。通知最好位于左侧边栏中。我不认为我想使用弹出窗口或临时通知,但它们可能是一项附加功能。

我很确定这是一个非常简单的问题,但由于某种原因,我似乎找不到正确的关键字,然后是 magento 本身。

【问题讨论】:

  • 我已将“实时”更改为“ajax”,因为我怀疑这就是您所暗示的。另外,你的意思是使用“jquery”标签吗? Magento 通常附带 Prototype,尽管主题通常也有 jQuery。
  • 好的,我已经部分解决了这个问题,我将在一周内更新更多信息。我确实得到了实时更新的数据,但是 magento 的结构让它有点困难,或者我想得太难了。

标签: php javascript ajax magento


【解决方案1】:

好的,这是迄今为止我提出的最好的一个。我仍然认为它只是部分解决了,我认为我可以从 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

还有很多其他类似的例子,但上面的例子似乎涵盖了大部分基础。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-07-01
    • 1970-01-01
    • 2012-11-13
    • 2011-06-07
    • 2011-11-30
    • 1970-01-01
    • 1970-01-01
    • 2012-04-25
    相关资源
    最近更新 更多