简体   繁体   English

如何根据时间和星期几在magento静态块中显示动态文本?

[英]How to display dynamic text in magento static block depending on time and day of week?

In a static block in magento I want to display a dynamic message depending on the day of the week and the time. 我想在magento的静态块中显示动态消息,具体取决于星期几和时间。

It's for customers, so they know when customer support is open and when it is closed. 它是针对客户的,因此他们知道客户支持的开放时间和关闭时间。

For example: I want to display: "Now Open" on monday - fridays from 09:00 till 17:00. 例如:我想在星期一显示:“现在开放”-星期五从09:00到17:00。 On saturday, sunday and on monday - friday from 17:01 untill 08:59 I want to display the text: "Now closed". 在星期六,星期日和星期一-星期五从17:01到08:59,我要显示文本:“ Now close”。

Is there a way to do this with javascript or any other way? 有没有办法用javascript或其他方式做到这一点?

Please keep in mind that I'm quite a noob on this matter :). 请记住,在这个问题上我是个菜鸟。

All help is very much appreciated! 非常感谢所有帮助!

I haven't provided the complete solution here because it will take certain time and effort but i could help you with some idea, so that you can achieve your requirement. 我在这里没有提供完整的解决方案,因为这需要花费一些时间和精力,但是我可以为您提供一些帮助,以便您可以满足您的要求。

The logic is : 逻辑是:

  1. You can create two static block with the image or info for open and close .You can easily do it from admin panel. 您可以使用图像或信息创建两个静态块以进行打开关闭 。您可以在管理面板中轻松完成此操作。
  2. Now the main portion.ie you need to manipulate time and days.If you want it fully dynamic and if the admin could choose the days and time than you need to create a custom module. 现在是主要部分,即您需要操纵时间和日期。如果您希望它完全动态,并且管理员可以选择日期和时间,那么您需要创建一个自定义模块。 In that module you need to provide a place to choose the days and time(You have to use your logic here,How it will be easier for you to manipulate the data you get from here. You can use multi-select for days and so on). 在该模块中,您需要提供一个选择日期和时间的位置(您必须在这里使用逻辑,如何更轻松地操作从此处获得的数据。您可以在几天内使用多选功能,这样上)。
  3. Next part is,after after your module is ready you have to get that data inserted from the backend to the template file where you want to display the static blocks.There you have to use some conditions to show either the open or close block. 下一部分是,在模块准备好之后,您必须将数据从后端插入到要显示静态块的模板文件中。在那里,您必须使用一些条件来显示打开关闭块。

You may need to do little research on how to manipulate time in your case. 您可能需要对如何处理案件的时间进行很少的研究。 That's all. 就这样。

Try this, if you have any further problems than feel free to ask. 如果您有其他任何问题,可随时尝试。

Hope this will help. 希望这会有所帮助。

I managed to came up with another solution. 我设法提出了另一种解决方案。

Place this in HTML Head: 将其放在HTML Header中:

<style type="text/css">
#openSign.OPEN {
color: green;
background-color: yellow;
font-size: x-large;
}
#openSign.CLOSED {
color : red;
background-color: pink;
font-size: large;
}
</style>

<script type="text/javascript">

var OPENAT = 7.5; // 7:30 AM ... change as appropriate - can be fractions of an hour e.g. 7.5 = 7:30am
var CLOSEAT = 21; // 9:00 PM ... change as appropriate

function areWeOpen( ) {
var sign = document.getElementById("openSign");
var day = new Date().getDay();
var hour = new Date().getHours();
var mins = new Date().getMinutes();
hour = hour + mins/60;
if ( day >=1 && day <=5 && hour >= OPENAT && hour < CLOSEAT )  {
sign.innerHTML = "We are now OPEN";
sign.className = "OPEN";
} 
else {
sign.innerHTML = "Sorry, we are now CLOSED";
sign.className = "CLOSED";
}
}
</script>

And this in the static block: 而这在静态块中:

<body onload = "areWeOpen()">
<span id="openSign"></span>

I don't know if this is a neat solution, but it works for me. 我不知道这是否是一个很好的解决方案,但是它对我有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM