Hiding Time in Transaction Mails: A Magento Developer’s Perspective
As a seasoned Magento developer, I’ve encountered this issue multiple times while working on transactional emails for various e-commerce projects. The {{var order.getCreatedAtFormated('short')}} variable is indeed designed to display only the date without time. However, there are certain intricacies in the Magento core that may lead to confusion.
Understanding the Core Functions
Let’s first break down the relevant functions involved:
-
getCreatedAtFormated(): Found inapp/code/core/Mage/Sales/Model/Order.php, this function uses the core helper’sformatDate()method with a format parameter of ‘short’.public function getCreatedAtFormated($format) { return Mage::helper('core')->formatDate($this->getCreatedAtStoreDate(), $format, true); } -
formatDate(): Located inapp/code/core/Mage/Core/Helper/Data.php, this is a more complex function that formats dates based on the provided format and locale settings.public function formatDate($date = null, $format = Mage_Core_Model_Locale::FORMAT_TYPE_SHORT, $showTime = false) { // ... (checks for valid input) if ($showTime) { $format = Mage::app()->getLocale()->getDateTimeFormat($format); } else { $format = Mage::app()->getLocale()->getDateFormat($format); } return $date->toString($format); }
The Gap in Logic
The confusion seems to stem from the $showTime parameter in formatDate(). By default, when we call this function with a format like ‘short’, it doesn’t consider the getCreatedAtFormated() function’s setting of $showTime = true. This leads to the time being included in the output.
Solution: Override or Patch
To resolve this issue, you have two primary options:
-
Override the Core Helper: Create an extension that overrides the
Mage_Core_Helper_Dataclass and modify theformatDate()function to respect the$showTimeparameter passed from a calling method likegetCreatedAtFormated().public function formatDate($date = null, $format = Mage_Core_Model_Locale::FORMAT_TYPE_SHORT, $showTime = false) { // ... (keep the existing checks) if ($format == 'short' && !$showTime) { $format = Mage::app()->getLocale()->getDateFormat($format); } else if ($showTime) { $format = Mage::app()->getLocale()->getDateTimeFormat($format); } return $date->toString($format); } -
Patch the Core: Modify the
Mage_Core_Helper_Dataclass directly, ensuring you have a backup and understanding that this approach violates Magento’s upgrade policy.
Best Practices for Email Templates
In my experience, it’s often more efficient to handle such formatting within your email templates or custom helpers rather than relying on core variables. This provides better control over the output and allows you to use native PHP functions if needed.
For example, in your email template, you can extract the order date using a custom block:
{{block type="core/template" name="order_created_at" as="order_created_at" template="sales/order/created.phtml"}}
Then, in the created.phtml file, use PHP to format the date without time:
$orderDate = Mage::registry('current_order')->getCreatedAt();
echo date('m/d/Y', strtotime($orderDate));
Conclusion
The apparent inconsistency in Magento’s core functions can be resolved through careful extension development or strategic template modifications. Remember, direct core patches should be avoided to maintain upgrade compatibility.
For a more detailed guide on working with transactional emails and customizing the layout in Magento, I recommend exploring our comprehensive blog series at LaravelCompany.com.
Don’t hesitate to reach out if you have further questions or need assistance with your specific project.