问题
I have a little problem. I only want to display the date without time in my order mails.
For that I have to use {{var order.getCreatedAtFormated(''short'')}}
afaik. But it is still showing the time. Of course I searched the Magento source and I am wondering why it's not working.
When I look into app/code/core/Mage/Sales/Model/Order.php
I can find that:
/**
* Get formated order created date in store timezone
*
* @param string $format date format type (short|medium|long|full)
* @return string
*/
public function getCreatedAtFormated($format)
{
return Mage::helper('core')->formatDate($this->getCreatedAtStoreDate(), $format, true);
}
Looking further into /app/code/core/Mage/Core/Helper/Data.php
shows this:
public function formatDate($date = null, $format = Mage_Core_Model_Locale::FORMAT_TYPE_SHORT, $showTime = false)
{
if (!in_array($format, $this->_allowedFormats, true)) {
return $date;
}
if (!($date instanceof Zend_Date) && $date && !strtotime($date)) {
return '';
}
if (is_null($date)) {
$date = Mage::app()->getLocale()->date(Mage::getSingleton('core/date')->gmtTimestamp(), null, null);
} else if (!$date instanceof Zend_Date) {
$date = Mage::app()->getLocale()->date(strtotime($date), null, null);
}
if ($showTime) {
$format = Mage::app()->getLocale()->getDateTimeFormat($format);
} else {
$format = Mage::app()->getLocale()->getDateFormat($format);
}
return $date->toString($format);
}
So shouldn't it already hide the time when I pass the short argument to that function?
回答1:
Mage::helper('core')->formatDate(null, Mage_Core_Model_Locale::FORMAT_TYPE_SHORT, false);
The last parameter allows for time to be hidden:
public function formatDate($date = null, $format = Mage_Core_Model_Locale::FORMAT_TYPE_SHORT, $showTime = false)
That is very annoying of Magento really, to provide a wrapper to a method but not provide full access. Why they haven't written the method like the following I don't know...
public function getCreatedAtFormated($format, $showTime = true)
{
return Mage::helper('core')->formatDate($this->getCreatedAtStoreDate(), $format, $showTime);
}
回答2:
You have to override /app/code/local/Mage/Sales/Model/Order.php, create a new function
public function getCreatedAtFormatedHideTime($format)
{
return Mage::helper('core')->formatDate($this->getCreatedAtStoreDate(), $format, false);
}
So you can call it from mail template:
{{var order.getCreatedAtFormatedHideTime('short')}}
Output:
01/01/2000
来源:https://stackoverflow.com/questions/19221799/hide-time-in-transaction-mails-with-var-order-getcreatedatformatedshort