The code below illustrates how to display or use a block of code AFTER a certain time. This code uses Magento’s core date functionality which uses your store’s configured timezone. So if your store is configured to use Eastern Standard Time, this code will compare dates and times using Eastern Standard Time. I’ve found this code to be most useful if you need to display a promotional banner, callout or message to your customer on, after or between a certain date and time. Say for instance you want your promo banner to display after 2:00am and you don’t wish to consume 10 cups of coffee to stay up all night to deploy your banner, this code will help you by automating that deployment process. Please drop us a comment if you find this code useful or if you experience any issues, we’re happy to help! ENJOY!
Display or use a block of code AFTER a certain time:
</div>
<div id="_mcePaste"><?php</div>
<div id="_mcePaste">$current_date = (int)date("Ymd", Mage::getModel('core/date')->timestamp(time())); // YYYYMMDD</div>
<div id="_mcePaste">$current_hour = (int)date("Gi", Mage::getModel('core/date')->timestamp(time())); // HMM or HHMM (24 hour)</div>
<div id="_mcePaste">$display_date_start = (int)20130308; // March 08th 2013</div>
<div id="_mcePaste">$display_hour_start = (int)830; // 8:30am</div>
<div></div>
<div id="_mcePaste">if($current_date >= $display_date_start && ($current_hour >= $display_hour_start || $current_date > $display_date_start)) {</div>
<div id="_mcePaste">// code to display or use goes here</div>
<div id="_mcePaste">}</div>
<div id="_mcePaste">?></div>
<div id="_mcePaste">
</div>
<div id="_mcePaste"><?php</div>
<div id="_mcePaste">$current_date = (int)date("Ymd", Mage::getModel('core/date')->timestamp(time())); // YYYYMMDD</div>
<div id="_mcePaste">$current_hour = (int)date("Gi", Mage::getModel('core/date')->timestamp(time())); // HMM or HHMM (24 hour)</div>
<div id="_mcePaste">$display_date_start = (int)20130308; // March 08th 2013</div>
<div id="_mcePaste">$display_hour_start = (int)830; // 8:30am</div>
<div id="_mcePaste">$display_date_end = (int)20130408; // April 08th 2013</div>
<div id="_mcePaste">$display_hour_end = (int)1830; // 6:30pm</div>
<div></div>
<div id="_mcePaste">if($current_date >= $display_date_start && $current_date <= $display_date_end && ($current_hour >= $display_hour_start || $current_date > $display_date_start) && ($current_hour <= $display_hour_end || $current_date < $display_date_end)) {</div>
<div id="_mcePaste">// code to display or use goes here</div>
<div id="_mcePaste">}</div>
<div id="_mcePaste">?></div>
<div id="_mcePaste">
Later versions of Magento have added javascript code to /js/varien/js.js to “stub out” the console if it’s not available. This is no doubt to prevent a javascript crash if errant console.log statements get left in throughout the project. The problem is, it prevents console.log from working in Chrome – which I increasingly use just because it’s so much faster.
But there’s a fix (thanks to AlexB in this post http://stackoverflow.com/questions/8080610/javascript-console-log-in-magento for this fix):
Find this block of code in /js/varien/js.js:
if (!("console" in window) || !("firebug" in console))
{
var names = ["log", "debug", "info", "warn", "error", "assert", "dir", "dirxml",
"group", "groupEnd", "time", "timeEnd", "count", "trace", "profile", "profileEnd"];
window.console = {};
for (var i = 0; i < names.length; ++i)
window.console[names[i]] = function() {}
}
And change it to:
var is_chrome = navigator.userAgent.toLowerCase().indexOf('chrome') > -1;</HL>
if (!("console" in window) || !("firebug" in console) && !is_chrome)
{
var names = ["log", "debug", "info", "warn", "error", "assert", "dir", "dirxml",
"group", "groupEnd", "time", "timeEnd", "count", "trace", "profile", "profileEnd"];
window.console = {};
for (var i = 0; i < names.length; ++i)
window.console[names[i]] = function() {}
}
Magento uses caching extensively. If you also want to be able to save something to cache rather than reading from the database or some more expense resource like that, it’s simple:
$value = Mage::app()->loadCache(‘my_cache_key’);
If the key isn’t found, $value will be null.
To store a value in the cache is almost as easy:
Mage::app()->saveCache($data_to_store, ‘my_cache_key’, array(), $duration_in_seconds);
(The empty array in the call above is standing in for a parameter called “tags” which, to my knowledge, is not used. Someone please correct me on that if I’ve got it wrong.)
Sometimes I’m banging my head against the wall trying to figure out why my block isn’t loading right or some other layout element isn’t rendering. And I’ve always wished I could just see the layout as Magento is seeing it. So I finally dug in to see and it’s actually incredibly simple to output the layout for a given page in xml format.
echo Mage::app()->getLayout()->getNode()->asNiceXml('', 0);
The layout is just a Varien_Simplexml_Config. getNode buys you access to the private $_xml variable that holds the root node of the xml document. Then asNiceXml formats it nicely for the screen. If you’d rather output to a file (they can be large chunks of xml) supply a file name for the first parameter of asNiceXml and the xml will be written there, too.
Now back to why my block isn’t loading…
Sometimes Magento does something and it’s just not obvious why. We’ve all heard “why aren’t my products showing up?” more times than we can count. Magento’s object model for building queries is great, but sometimes you just need to see the sql that’s being run on the database in order to know what’s really going on.
In the past I’ve used the general log in mysql. That can be a great source of information, but, depending on the version of mysql, it can be a pain to set up. Other times I’ve inserted logging code into various db classes in order to print out queries that are being run, but I always have to find the right place again. But today I stumbled on this blog entry - http://blog.nexcess.net/2011/04/15/logging-database-queries-in-magento/ that spells out exactly how you can turn on some (as far as I know) undocumented logging functionality that is built right in to the Magento data access layer. Kudos to the author and thanks for sharing!
As shown elsewhere, you can add an attribute to the category entity in Magento. If the attribute is of type select, getting the text value of an option is a bit of a pain:
$category = Mage::getModel(‘catalog/category’)->load(10);
$attribute = Mage::getSingleton(‘eav/config’)->getAttribute(‘catalog_category’, ‘myattribute_code’);
$text = $attribute->getSource()->getOptionText($category->getMyattributeCode());