Adding a date attribute to Magento is no problem – but what about date + time? In this article I’ll explain how to add a date/time product attribute to Magento admin programmatically with upgrade script. Basically to make it happen you need to configure the renderer for date field and source model to convert the date and save time as well as date.
So let’s start with renderer itself:
app/code/local/Youcompany/Yourmodule/Block/Adminhtml/Renderer/Datetime.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
/** * Datetime attribute field renderer */ class Youcompany_Yourmodule_Block_Adminhtml_Renderer_Datetime extends Varien_Data_Form_Element_Date { /** * Construct * * @param array $attributes */ public function __construct($attributes = array()) { parent::__construct($attributes); $this->setTime(true); } /** * Retrieve date format * * @return string */ public function getFormat() { return Mage::app()->getLocale()->getDateTimeFormat(Mage_Core_Model_Locale::FORMAT_TYPE_SHORT); } } |
Next – let’s create a source model that will handle this attribute on save:
app/code/local/Youcompany/Yourmodule/Model/Attribute/Datetime.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
/** * Datetime attribute source model */ class Youcompany_Yourmodule_Model_Attribute_Datetime extends Mage_Eav_Model_Entity_Attribute_Backend_Datetime { /** * Format date * * @param string|int $date * @return string */ public function formatDate($date) { if (empty($date)) { return null; } $date = Mage::app()->getLocale()->date($date, Mage::app()->getLocale()->getDateTimeFormat(Mage_Core_Model_Locale::FORMAT_TYPE_SHORT), null, false ); return $date->toString(Varien_Date::DATETIME_INTERNAL_FORMAT); } } |
And finally the upgrade script to create an attribute and connect it to model and renderer:
app/code/local/Youcompany/Yourmodule/sql/yourmodule_setup/mysql4-upgrade-0.0.1-0.0.2.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
/* @var $installer Mage_Eav_Model_Entity_Setup */ $installer = $this; $installer->startSetup(); /* @var $eavConfig Mage_Eav_Model_Config */ $eavConfig = Mage::getSingleton('eav/config'); $entityCode = Mage_Catalog_Model_Product::ENTITY; $installer->removeAttribute($entityCode, 'start_date'); // if attribute exist - delete it $installer->addAttribute($entityCode, 'start_date', array( 'label' => 'Start Date', 'group' => 'General', 'global' => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_GLOBAL, 'backend' => 'yourmodule/attribute_datetime', 'show_on_frontend' => false, 'unique' => false, 'is_configurable' => false, 'used_for_price_rules' => false, 'visible' => true, 'required' => false, 'user_defined' => true, 'is_user_defined' => false, 'searchable' => false, 'filterable' => false, 'visible_on_front' => true, 'default' => null, 'type' => 'datetime', 'input' => 'date', 'class' => 'validate-date', ) ); $attribute = $eavConfig->getAttribute($entityCode, 'start_date'); $attribute->setData('apply_to', array('simple', 'virtual')); // set product types to apply new attribute $attribute->setData('frontend_input_renderer', 'yourmodule/adminhtml_renderer_datetime'); $attribute->save(); $installer->endSetup(); |
You can use any setup options for you attribute, but the most important part here is:
1 |
'backend' => 'yourmodule/attribute_datetime', |
1 |
$attribute->setData('frontend_input_renderer', 'yourmodule/adminhtml_renderer_datetime'); |
I added the as described, but the datepicker still rest the same with only the date…
I use magento 1.8.0.0
Do you see your custom attribute in attribute set? Just to make it clear – this will not change every datepicker in admin to display time – this will work only for the brand new added attribute.
Hello,
I added the as described, but i’ve got an error :
Fatal error: Class ‘Mage_MyCompagny_MyModule_Checkout_Block_Adminhtml_Renderer_Datetime’ not found in …\lib\Varien\Data\Form\Abstract.php on line 144
An Idea ?
I use magento 1.11.1.0
Sorry I found my error, bad route to my module…
Thanks a lot
If you see Mage_ as a prefix to MyCompagny_MyModule – this is always a signal that you messed up with the route. Good to hear it helps though.
Hey Igor,
I’m looking to change the default special date from and to, to include the time option too. Any idea how to do that?
Cheers