如果达到时间x,我需要能够隐藏Magento结账时的下订单按钮

时间:2011-12-28 09:27:21

标签: javascript magento checkout

如果在结账阶段达到截止日期,我需要能够隐藏“下订单”按钮。在这家商店,顾客正在为他们的订单挑选路线,然后他们从路线中获取订单。

当客户选择路线时,设置了截止时间的cookie,我需要在结账过程中实时比较此cookie与当前时间,以确保客户无法将订单下达到已关闭的路线。 / p>

我有时间比较在PHP工作,但它只获得页面加载的当前时间,所以如果有人花了太多时间在结帐,这可能是一个问题。我已经尝试了ajax脚本并将它们放在button.phtml内但由于某种原因它们似乎不起作用。我确信这与magento的主题/文件结构有关,我没有得到。

1 个答案:

答案 0 :(得分:0)

到目前为止,这是我的解决方案。它非常简单,但我现在的问题是,我无法弄清楚如何从服务器获取当前时间,因为javascript是客户端,它使用用户的时钟设置。令人惊讶地难以做到这一件简单的事情。我想我很快就可以搞清楚。

此代码位于button.phtml

<script>
  document.getElementById('deadline_reached').style.visibility = 'hidden';  

  function validateDeadline() {

   <?php 

    date_default_timezone_set('Europe/Helsinki');
    $deadlineTimeHuman=$_COOKIE["deadlineDate"];
    $deadlineTimeUnixTimestamp = strtotime($deadlineTimeHuman);
     ?>

  // Current date 
  var timeNowCompare = Math.round(new Date().getTime() / 1000) //Current date is changed to unix timestamp without milliseconds os it matches with php's timestamp format.

  //Cookie's deadline time which has been changed to timestamp is loaded into variable.
  var deadlineDate = "<?php echo $deadlineTimeUnixTimestamp ?>";

  if (timeNowCompare < deadlineDate) // What happens when we are in schedule.
  { 
  alert("Now < Deadline Order can be placed \n"+"n:"+ timeNowCompare +"\nd:"+deadlineDate); // This is for debugging
  }
  else if (timeNowCompare > deadlineDate) // What happens when current time is higher than deadline.
  { 
  alert("Now > Deadline Order cant be placed \n"+"n:"+ timeNowCompare +"\nd:"+deadlineDate); // This is for debugging
  document.getElementById('place_order_btn').style.visibility = 'hidden';  
  document.getElementById('deadline_reached').style.visibility = 'visible'; 

  }     
}  

window.setInterval(function(){
  validateDeadline();
 }, 10000);
</script>