Sorting Numbers in Product Name

  • Posts: 10
  • Thank you received: 2
2 years 1 day ago #341087

-- HikaShop version -- : 4.5.1
-- Joomla version -- : 4.1.2
-- PHP version -- : 8.1.0
-- Browser(s) name and version -- : Any
-- Error-message(debug-mod must be tuned on) -- : N/A

Hello,

Apologies if this has been asked before - I’m sure it’s a common concern, but I couldn’t find a thread via Google or the forum’s search using relevant keywords.

Basically, my products have models and numbers in their name. Unfortunately, the way it’s currently ordering things seems to be by digit. I.E. 1, 11, 13, 2, 21, 3, 34, etc.

I would like this to be numerical, and also account for letters and words after or before the number, alphabetically. For example:

#1 Clip
#1 Pin
#2 Clip
#2 Pin
#3 Clip


I’ve tried getting this to work with custom fields, but all to no avail. I’ve adjusted both the menu and the module, trying every default option. I realize I could manually order things, but I have tens of thousands of items and we’re constantly adding more, so I feel it’s impractical.

Thanks in advance for any advice.

Please Log in or Create an account to join the conversation.

  • Posts: 81539
  • Thank you received: 13069
  • MODERATOR
2 years 17 hours ago #341091

Hi,

You won't be able to do that with the options available.
In the options available, you can select a column on which the sorting will be done and whether the sorting can be ascending or descending.
And then, MySQL will sort the elements based on the type of the column in the database. So if you use the product_name column, its type is varchar, and thus the sorting will be alphabetical. If the type of the column would be "integer" for example, then the sorting would be numerical.
But what you want is a hybrid numerical and then alphabetical sorting on one column.

The only solution, supposing you don't have a # at the beginning of the product_name but directly the number, is to use this trick:
stackoverflow.com/questions/34054293/ord...habetically-in-mysql
That way, you have the number at the beginning of the text handled as a number for the sorting, and then the text behind handled as text.
However, there is no system in HikaShop to set "order by b.product_name + 0, b.product_name" in the options. That can be done by developing a small plugin implementing the onBeforeProductListingLoad ( www.hikashop.com/support/documentation/6...reProductListingLoad ) event and overriding the $order parameter. For a PHP developer, it should be easy to do.

The following user(s) said Thank You: jbrull

Please Log in or Create an account to join the conversation.

  • Posts: 10
  • Thank you received: 2
1 year 11 months ago #341115

Makes sense. Thank you for the provided documentation and explanation, I’ve got it working now.

The following user(s) said Thank You: nicolas

Please Log in or Create an account to join the conversation.

Time to create page: 0.049 seconds
Powered by Kunena Forum