I'm struggling to set the order date when creating a magento order programatically.

Here is what I'm trying to use:

$orderObj->setData('created_at', $date);

but it doesn't appear to work.

Thank you in advance!

  • What value did it stored in the db NULL? – Renon Stewart Oct 17 '14 at 13:16
  • Try setCreatedAt(Mage::getSingleton('core/date')->gmtDate()); – Renon Stewart Oct 17 '14 at 13:35

created_at and updated_at for orders is set in Mage_Sales_Model_Resource_Abstract::_prepareDataForSave():

 * Prepare data for save
 * @param Mage_Core_Model_Abstract $object
 * @return array
protected function _prepareDataForSave(Mage_Core_Model_Abstract $object)
    $currentTime = Varien_Date::now();
    if ((!$object->getId() || $object->isObjectNew()) && !$object->getCreatedAt()) {
    $data = parent::_prepareDataForSave($object);
    return $data;

You can see that updated_at is always set to the current time, whereas created_at is only set if the attribute is empty. The format that it should have comes from Varien_Date.

So you should be able to define your own creation timestamp:

$order->setCreatedAt(Varien_Date::formatDate($date, false));

where $date can be a Unix timestamp or an arbitrary date expression that can be parsed with PHPs strtotime

  • Does this not stop created_at being set when the object is not new also? – David Manners Oct 17 '14 at 14:01
  • You had me for a moment. No, it only stops it from being automatically set when the object is new. You still can set it manually. – Fabian Schmengler Oct 17 '14 at 14:15
  • 1
    that is what I meant sorry. The problem may be that it is a new object and they are trying to set a data other than current date. – David Manners Oct 17 '14 at 14:22
  • Ah, now I get it. Yeah, so one actually has to save the order first, then change created_at, then save again. – Fabian Schmengler Oct 17 '14 at 14:24
  • I stand corrected (again). It will work, look at the parantheses! – Fabian Schmengler Oct 17 '14 at 14:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.