Magento en PHP:每天显示一个不同的小部件并在最后循环

Magento en PHP: Show a different widget each day and loop at the end

在我的 Magento 网上商店中,我想每天展示不同的小部件。

我在 10 个不同的日子里有 10 个不同的小部件。 10 天后,应该会再次显示第一个小部件。

此代码将包含在来自的 phtml 中。

我想要的:

第 1 天显示:

<?php
$filter = new Mage_Widget_Model_Template_Filter();
$_widget = $filter->filter('{{widget type="myextension/widget_block" block_id="1"}}');
echo $_widget;
?>

第 2 天显示:

<?php
$filter = new Mage_Widget_Model_Template_Filter();
$_widget = $filter->filter('{{widget type="myextension/widget_block" block_id="2"}}');
echo $_widget;
?>

第 3 天显示:

<?php
$filter = new Mage_Widget_Model_Template_Filter();
$_widget = $filter->filter('{{widget type="myextension/widget_block" block_id="3"}}');
echo $_widget;
?>

.....

第 10 天显示:

<?php
$filter = new Mage_Widget_Model_Template_Filter();
$_widget = $filter->filter('{{widget type="myextension/widget_block" block_id="10"}}');
echo $_widget;
?>

第 10 天后从第 1 天重新开始。 这没有结束日期 ...

我该怎么做?

2015 年 4 月 17 日更新: 在我的示例中,block_id 是按顺序排列的。但可能不会跟进 block_id 的 ... 所以第 1 天可能 block_id="12",第 2 天:block_id="4",第 3 天:block_id="21" 等等

使用日期和模数获取 1-10 的值。

<?php
$filter = new Mage_Widget_Model_Template_Filter();
$_widget = $filter->filter('{{widget type="myextension/widget_block" block_id="'.((date('z')%10)+1).'"}}');
echo $_widget;
?>

解释部分...

date('z'); //the day of the year will be an integer 1-365
%10  //modulus is the remainder after division, so as the number climbs it will go 0,1,2,3,4,5,6,7,8,9,0,1,2,3....
+1 //turns your 0-9 values into 1-10 to match block ids.

更新后的问题现在问...如果我的块 ID 不是连续的怎么办?我认为在那种情况下,您可以定义一个数组,其中包含键 1-10 和实际块 ID 的值。

$idMapping = array(
    1    => 3,
    2  => 5,
    3  => 9,
    4 => 13,
    5 => 17,
    6 => 23,
    7 => 29,
    8 => 31,
    9 => 37,
    10 => 41
);
$filter = new Mage_Widget_Model_Template_Filter();
$_widget = $filter->filter('{{widget type="myextension/widget_block" block_id="'.$idMapping[(date('z')%10)+1].'"}}');
echo $_widget;

关于我的解决方案的另一件事。一年中的天数不能被 10 整除,因此在年底从 365 回到 1 时,您将无法通过所有 10 笔交易进行完整轮换。因此,解决方法是通过将其与固定日期进行比较来生成我们的每日增量值,因此当您远离该日期时,它总是会攀升。

$date = "2015-04-17";
$diff = abs(strtotime($date) - time());
$days = floor($diff/3600/24); //converts seconds to hours, then to days
//so now you can replace date('z') with $days and you'll loop continuously without any weird gap at the end of the year.