Comments

googletorp’s picture

I don't believe you can setup Commerce from within the UI on how to handle order numbers, but you could create any custom logic and use the hooks for when an order is completed, created etc, to make it use any order_number system you please (in a custom module).

rszrama’s picture

Status: Active » Fixed

Yep, that's right. One way to do it would be to use a variable_get() / variable_set() to track the current order number and use the checkout completion hook that googletorp mentioned to set the order number to your desired value. The hook is hook_commerce_checkout_copmlete($order).

franfran’s picture

Thanks for the advice! So I should have my own module and database table to map the Commerce order number and my own formatted Receipe number?
And for any display like order list in the admin page, order complete email, and order history in member's page, I queue and display the Receipe number instead of the Commerce order number.

rszrama’s picture

You don't need to do this, because the Order Number is a column of the commerce_order table that you can actually use to store these numbers. The only thing you need to do is make sure you have a way to keep track of what number to use next. Then on hook_commerce_order_update() or hook_commerce_checkout_complete(), you should change the order's order_number property to the required value. Doing this on checkout completion makes sense to me.

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.

adrianmak’s picture

the Order Number in table is a numeric data type, how could I use a string pattern of order number ?

rszrama’s picture

The order_id is numeric, but order_number is actually a varchar (text or numbers). You just have to implement some method of setting the order number to what you want on order creation or some other event (such as checkout completion).

guguss’s picture

Hi,

I had to solve that issue for a personal project. I'm now trying to provide a more generic solution to handle that.

It's still on my sandbox for now : http://drupal.org/sandbox/GuGuss/1765778

aidanlis’s picture

Often an admin, not a user, creates the order so we needed to use presave:

/**
 * Implements hook_commerce_order_presave().
 */
function yourmodule_tweaks_commerce_order_presave($order) {
  if (isset($order->order_id)) {
    $order->order_number = sprintf('KW%05d', $order->order_id);
  }
}
dianikol’s picture

What about using a rule to do that? Just a quick plan

1) Event: After updating an existing commerce order
2) Conditions: Data comparison: if the unchanged order status is not pending or edit and the updated status is pending
3) Action: Use PHP to get the latest completed order to see what the next order number should be and do the update to the order number according to your plans

Maybe you can set a variable with variable_set a starting point value and then using this variable to the rule action from above to set the order number.

Something like that. I hope it's clear :)

GuGuss sandbox module works great for orders made by customers but not when an admin make the order

Tell be what do you think for the rule solution

Thanks

bpirkle’s picture

The rules solution worked for me. The rule entry in my features module looks like this:

  $items['rules_assign_custom_order_number'] = entity_import('rules_config', '{ "rules_assign_custom_order_number" : {
      "LABEL" : "Assign custom order number",
      "PLUGIN" : "reaction rule",
      "REQUIRES" : [ "mymodule", "entity" ],
      "ON" : [ "commerce_order_presave" ],
      "DO" : [ { "mymodule_assign_custom_order_number" : [] } ]
    }
  }');

And the code in my custom module looks like this:


/**
* Implements hook_rules_action_info().
*/
function mymodule_rules_action_info() {
  return array(
    'mymodule_assign_custom_order_number' => array(
      'label' => t('MyModule Assign Custom Order Number'),
      'group' => t('MyCompany'),
      'parameter' => array(
        'commerce_order' => array(
          'type' => 'commerce_order',
          'label' => t('Order to modify'),
        ),
      ),
    ),
  );
}


/**
 * Custom rule action.  Modify as needed for your particular situation.
 */
function mymodule_assign_custom_order_number($commerce_order) {
  // This rule is fired multiple times during an order's lifetime.
  // Make sure we only assign our order number once.
  // This logic depends on our custom order numbers being prefixed
  // with a string (not numeric) value.
  if (is_numeric($commerce_order->order_number)) {
    $order_num = mymodule_generate_order_num($commerce_order->order_id);
    $commerce_order->order_number = $order_num;
  }
}

anybody’s picture

Issue summary: View changes

#11 is great! What do you think about creating a module for that or including this as (possibly disabled) rule in DC? (favourite)

lmeurs’s picture

Thanks for #11. I did not use the rule entry for a Features module, but created a rule through the Rules UI for the After saving a new commerce order event. The order did not save automatically, so I had to add a save option to the commerce_order parameter:

'commerce_order' => array(
  'type' => 'commerce_order',
  'label' => t('Order to modify'),
  'save' => TRUE,
),

Worked out fine, thanks again!

hosais’s picture

Issue summary: View changes

Hi Imeurs,

May I ask "sage option to the commerce_order" means? how can we change order number by some settings?

Thanks.

hosais

lmeurs’s picture

@hosais: It has been a few months, so please bare with me. :-)

I do not remember why the addition of the save => TRUE option was necessary, but the following worked for me, hopefully it will for you too.

Based on the solution from #11 I created file sitecommerce.rules.inc for one of my site's custom modules named sitecommerce with the following contents (replace sitecommerce for the name of your own module):

/**
 * @link http://nodeone.se/en/learn-the-rules-framework
 * @link http://dgo.to/rules_example
 * @link http://internetdevels.com/blog/creating-custom-events-and-actions-using-rules-api
 * @link http://jamestombs.co.uk/2012-09-19/drupal-7-defining-rules-within-module-using-php
 */

/**
 * Implements hook_rules_action_info().
 */
function sitecommerce_rules_action_info() {
  $actions = array(
    'sitecommerce_rule_action_custom_order_number' => array(
      'label' => t('Set custom order number for Commerce orders (wd)'),
      'group' => t('Commerce Order'),
      'parameter' => array(
        // Commerce order.
        'commerce_order' => array(
          'type' => 'commerce_order',
          'label' => t('Order to modify'),
          'save' => TRUE,
        ),
        // Prefix, just for fun.
        'prefix' => array(
          'type' => 'text',
          'label' => t('Order number prefix'),
          'default value' => 'AB',
        ),
      ),
    ),
  );
  return $actions;
}

/**
 * Rule action callback: set custom order number.
 *
 * @todo Use variable_get() to create an order id independent counter?
 */
function sitecommerce_rule_action_custom_order_number($commerce_order, $prefix) {
  // Create order number based on prefix (set through Rules UI), the last two
  // digits of the current year and the order ID starting at 2222 and padded to
  // 6 digits.
  $commerce_order->order_number = trim($prefix) . date('y') . '-' . str_pad(2222 + $commerce_order->order_id, 6, '0', STR_PAD_LEFT);
}

In this file we defined a Rules action with callback function, after this I created a rule through the Rules UI for the After saving a new commerce order event and selected our new action.

Callback function sitecommerce_rule_action_custom_order_number() generates a new order number based on prefix (set through Rules UI), the last two digits of the current year and the order ID starting at 2222 and padded to 6 digits. This function can off course be altered to your likings.

hosais’s picture

@lmeurs

I really appreciate your quick and detail response. I am sincerely sorry that I responded your post so late.

I will get back to this issue soon.

Thank you again.

hosais

mandus.cz’s picture