Saturday, May 7, 2016

Set Custom Order Increment id in Magento

Magento is most uses PHP based platform for e-commerce website. In Magento website adds products from admin panel and in front any user buy that product and after complete payment method or check out it's generated new order in the Magento database with unique order id.In default Magento order id(as increment id) starts from '100000001' and then after increment one for the next order.
Magento all orders saves in sales_flat_order table with unique entity_id as primary key. so you can get order information by order entity id or increment value. In order table entity_id is the primary key so you can't modify it. but you can change the order increment id that displays also in fronted.
When you want to develop a website and want to change the increment id that from a database you can change the order increment id prefix only. for that use below SQL query in your MySql command.


UPDATE `eav_entity_store` SET `increment_prefix` = 'NEWS-' WHERE `entity_store_id` = '1' AND `entity_type_id` = '5'

Here you can add own prefix in `increment_prefix` column.`entity_store_id` is your store id and `entity_type_id` is order type(different for invoice,shipment).
Sometimes you want to change the order increment id not only prefix or want to add a different prefix using some condition then you must change in core Magento files with creating a simple module as below.
First create new Newsinfo_Order.xml file in app/etc/modules/ as below.

<config>
  <modules>
    <newsinfo_order>
      <active>true</active>
      <codepool>local</codepool>
    </newsinfo_order>
  </modules>
</config>
 
Than create new config.xml file in app/code/local/Newsinfo/Order/etc/ as below.

<?xml version="1.0"?>
<config>
  <modules>
    <Newsinfo_Order>
      <version>1.0.0</version>
    </Newsinfo_Order>
  </modules>
  <global>
    <models>
      <sales>
        <rewrite>
          <order>Newsinfo_Order_Model_Order</order>
        </rewrite>
      </sales>
    </models>
  </global>
</config>
 
Here in below file just rewrite magento 'Mage_Sales_Model_Order' model with custom extension or you can directly changes in core file from app/code/core/Mage/Sales/Model/Order.php
Than after create new model file in app\code\local\Newsinfo\Order\Model named Order.php with below code. 

class Newsinfo_Order_Model_Order extends Mage_Sales_Model_Order 
{
 protected function _beforeSave()
 {
  parent::_beforeSave();
  $this->_checkState();
  $this->setIncrementId('NEWSINFO-'.$this->getIncrementId());
  return $this;
 }
}
Here below function you can change your order prefix and also add some condition for different prefix or dynamic prefix or order increment id.order increment id is unique value so here must use unique value otherwise order not created.

1 comment :

  1. I suggest using an extension, which will tackle this issue, you can check it out at: https://goo.gl/62WC03 for more features.

    ReplyDelete