1

I need to add prefix in order number in magento. Like If Order is Cash On Delivery Based then order no should to be like... cod12332434343.... if order is prepaid..then it should be
pre12323233223 .... like this

if order is check/moneyorder then it should be.... cmo12121212121..... For this i want to know that....how to add prefix in magento order number....before order create ....and saved in database.....

2 Answers 2

6

The order increment ID is generate by the class Mage_Eav_Model_Entity_Increment_Numeric (see table eav_entity_type column increment_model) which is invoked on order beforeSave via Mage_Eav_Model_Entity_Type::fetchNewIncrementId.

Now the later has no knowledge of what it is actually rendering the increment ID for. Where I normally would suggest using another increment_model (as described by Atwix) this won't work.

The best solution in this case would probably be to use an observer. In this case you want to do that before the object is committed to database so we can use the $this->_eventPrefix.'_save_before' invoked by Mage_Core_Model_Abstract::_beforeSave.

The order models eventPrefix is sales_order so that would make the event you want to observer sales_order_save_before.

Tying that all together we would get something like this in your own custom module

config.xml

[...]
<global>
   <events>
        <sales_order_save_before>
            <observers>
                <[namespace]_[module]_sales_order_save_before>
                    <type>singleton</type>
                    <class>[Namespace]_[Module]_Model_Observer</class>
                    <method>salesOrderSaveBefore</method>
                </[namespace]_[module]_sales_order_save_before>
            </observers>
        </sales_order_save_before>
        [...]
    </events>
    [...]
</global>

[Namespace]/[Module]/Model/Observer.php

class [Namespace]_[Module]_Model_Observer 
{
    public function salesOrderSaveBefore($o)
    {
        $this->_generateOrderId($o);
        return $this;
    }

    protected function _generateOrderId($o)
    {
        $_object = $o->getDataObject();

        if ($_object->isObjectNew !== true) // only set the increment of a new order
             return;

        $incrementId = $_object->getIncrementId(); // get original increment ID
        $payment = $order->getPayment();

        /**
         * Do some magic with payment methods 
         * to format the $incrementID
         */

        $_object->setIncrementId($incrementId); // set the new increment ID to be saved
    }
}

Disclaimer: this is untested code and might need some tender love and tweaking before you deploy it on a live environment.

3
  • what is the use of this...$_object->isObjectNew ....not working with this condition...
    – Deepak Rai
    Commented Nov 5, 2015 at 13:53
  • Ah in that case just check if $_object->getId() is set Commented Nov 5, 2015 at 13:57
  • please review the answer...and remove Disclaimer
    – Deepak Rai
    Commented Nov 6, 2015 at 5:36
0

This will not work, if you use some payment service provider like paypal etc. because you are getting directed to the service provider and then back to the shop. in this process you have the reserved order id, last incremend id etc. which have diverged.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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