Order view: Sort by order number does not sort numerically

Created on 5 March 2020, over 5 years ago
Updated 1 September 2023, almost 2 years ago

When viewing the administrator order view
/admin/commerce/orders?&order=order_number&sort=asc

And you have at least 100 orders or more it does not sort numerically. Instead it sorts alphabetically

Eg:
1
10
11
....
100
110

Feature request
Status

Closed: works as designed

Version

2.0

Component

User experience

Created by

🇺🇸United States rhovland Oregon

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Merge Requests

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • 🇮🇳India arunkumark Coimbatore

    The Commerce Order Number field is a string field. So, it will work similarly to a string even though it was used as an integer number. So, it works as designed.

    Drupal\commerce_order\Entity\Order.php

    $fields = parent::baseFieldDefinitions($entity_type);
    
        $fields['order_number'] = BaseFieldDefinition::create('string')
          ->setLabel(t('Order number'))
          ->setDescription(t('The order number displayed to the customer.'))
          ->setRequired(TRUE)
          ->setDefaultValue('')
          ->setSetting('max_length', 255)
          ->setDisplayConfigurable('form', TRUE)
          ->setDisplayConfigurable('view', TRUE);
    
        $fields['version'] = BaseFieldDefinition::create('integer')

    To achieve the sorting as Integer we can able to use Query alter to type cast string as Integer. Below is an example.

    use Drupal\views\ViewExecutable;
    use Drupal\views\Plugin\views\query\QueryPluginBase;
    
    /**
     * Implements hook_views_query_alter().
     */
    function example_views_query_alter(ViewExecutable $view, QueryPluginBase $query) {
      if ($view->id() == 'VIEW_ID') {
        // Alter Commerce order to sort based on Order number.
        $formula_form = "CAST(order_number AS SIGNED)";
        $query->addField(NULL, $formula_form, 'order_number_field');
        $query->addOrderBy(NULL, NULL, 'DESC', 'order_number_field');
      }
    }
  • Status changed to Active almost 2 years ago
  • 🇺🇸United States rhovland Oregon

    This is a feature request not a bug report. I'm aware of why it doesn't sort correctly. I want to implement smart sorting as a feature at some future point. If this is not a feature the commerce maintainers want in commerce core then they can say so and close this as Won't Fix

  • Pipeline finished with Success
    about 19 hours ago
    Total: 591s
    #521444
Production build 0.71.5 2024