0

I have a Magento 2.4.1 site where I've added a custom attribute to store a discount percentage. I want to sort a category by this value.

However, in the grid list view (based off the Luma theme), it treats the percentage saving as a string. For example, it orders them as follows:

  • 2
  • 23
  • 5
  • 57
  • 6
  • 60

...instead of:

  • 2
  • 5
  • 6
  • 23
  • 57
  • 60

The attribute is configured as:

  • Catalog Input Type for Store Owner: Text field
  • Input Validation for Store Owner: Integer number

Is there a way to force it to be treated as an integer so it orders properly?

4
  • You can use php sort function sort($numbers). Can you explain in details so I can help you more? – Vikram Kumar Feb 11 at 19:18
  • When you are viewing a category in Magento, there is the "Sort by" drop down box. Choosing my custom attribute from the list, it's clear Magento isn't treating the values as integers, but sorting as though they are strings. – fistameeny Feb 11 at 19:37
  • you could try a 'hacky' idea - add the attribute with Catalog Input Type for Store Owner = 'price' - the value will be stored as decimal number and treated as such. but i am not sure if you have some business constraints, if you need to show this somewhere on frontend or something like that, in which case that might not be the best idea. Please let me know if this works for you. – Diana Botean Feb 12 at 13:49
  • Thanks for the suggestion. I'll use that as my fallback. For now, I've tricked the sorting by left padding single digits with a zero, e.g. 08 instead of 8. That seems to have done the job – fistameeny Feb 16 at 20:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.