How to create custom commerce order state programmatically Drupal 7

  • Posted on: 27 April 2017
  • By: jmu

Right now, I am working on the e-commerce website, and trying to add some "ORDER STATE" that drupal doesn't provide by default. In this tutorial, I will provide the code and some samples. It is very simple to create custom states. Let do it!

If you have never created a custom module, please briefly read the official tutorial first. You can find it here.

1. Create a .info file 

; $Id: $
name = custom order state module
description = add new order state for displaying
core = 7.x
package = Custom

dependencies[] = commerce_order

I used "custom order state" as my module name & "commerce_order_state.info" as my file name. You can use whatever you want.

2. Create a .module file

<?php
/**
 * Implement hook_commerce_order_state_info function
 */
function custom_order_state_commerce_order_state_info() {
    $order_states = array();

    $order_states ['processing'] = array(
        'name' => 'processing',
        'title' => t('Processing'),
        'description' => t('Orders in this state is still processing.'),
        'weight' => 1,
    );

    $order_states ['shipped'] = array(
        'name' => 'shipped',
        'title' => t('Shipped'),
        'description' => t('Orders in this state is shipped.'),
        'weight' => 2,
    );

    return $order_states;
}

You can find more detail description in this website: hook_commerce_state_info. And that's it!

Right now, you can go to your website, install your new module and then jump to "order setting" page. When you add or edit your "Order statuses", you will see your new order state in the drop-down list.

custom order state

Add new comment

CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.
Image CAPTCHA
Enter the characters shown in the image.