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">
This tip is helpful to stop the client from caching content.
Directions:
Open your local XML file.
This can be found in the [Magento Root]/app/etc/local.xml
Locate the follow area under the <global> node, and insert the code from line 26:
<config> <global> <install> <date>< ![CDATA[Mon, 27 Sep 2010 12:23:53 +0000]]></date> </install> <crypt> <key>< ![CDATA[demo]]></key> </crypt> <disable_local_modules>false</disable_local_modules> <resources> <db> <table_prefix>< ![CDATA[]]></table_prefix> </db> <default_setup> <connection> <host>< ![CDATA[localhost]]></host> <username>< ![CDATA[magento_user]]></username> <password>< ![CDATA[]]></password> <dbname>< ![CDATA[magento]]></dbname> <active>1</active> </connection> </default_setup> </resources> <session_save>< ![CDATA[files]]></session_save> <!-- Add the code below this line--> <session_cache_limiter>< ![CDATA[nocache]]></session_cache_limiter><!-- see http://php.net/manual/en/function.session-cache-limiter.php#82174 for possible values --> <!-- Add the code above this line--> </global>
After you save and commit your changes, clear your Magento cache for the site.
Well, we all know Magento is the best e-commerce platform around. And Authorize.net has to be the most popular payment gateway around. So it came as a real surprise that there’s no support in Magento for the (not so new) service from Authorize.net called CIM (Customer Information Manager) that lets you save customer credit cards at Authorize.net safely and in a PCI compliant way. Not only is there no support in Magento for CIM, but the available extensions are very limited in functionality.
So when a customer asked for saved credit cards where they could add/edit and delete saved cards as well as save multiple cards, it became clear that the only thing to do was to build an extension ourselves. The result? Pure fun. I know – geeky as all you know what. But using the Authorize.net API and building out the Magento functionality was a blast. All functionality was created without saving any credit card numbers or CVC codes to the local Magento database. Here are some highlights of what we’ve come up with:
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());
It was a sad morning here at iFuel Interactive. We got into the office today, and noticed that out beloved water cooler was making an odd noise. Rick went to go get some water for the coffee pot, and noticed that the entire machine was very hot, and water that was being dispensed out was boiling hot (even from the cold dispenser). He quickly unplugged the water cooler, and took out the 5 gallon water jug. Something must have happened to the heating coils, as it even started to melt the mouth of the jug. Thank goodness this didn’t happen over the weekend, or who knows what could have happened.