Sorting products based on a custom attribute Magento2

by Rick West   Last Updated October 09, 2019 18:09 PM

I have a plugin that adds some custom product sorting, based on a customer attribute, in a way similar to below.

# Vendor\ModuleName\Plugin\Product\ProductList\Toolbar;

case 'capacity_low_to_high':
    $collection->setOrder('capacity', 'ASC');
    break;

case 'name_high_to_low':
    $collection->setOrder('capacity', 'DESC');
break;

However, the sorting is not as expected. When sorting via the custom 'capacity' attribute, the products are being sorted alphanumerically, such as that the result is someting like:

1 11 19 2 3 4 5

Obviously this is not as desired, but I am unsure of the best way to sort the collection based on the numerical value of the attribute. Can anybody point me in the right direction? Ideally, I'm thinking that storing the capacity attribute as an integer would work best, but there doesn't seem to be an obvious way to implement that. Am I missing something? What are my options. Magento 2.3



Related Questions


Updated March 07, 2018 08:09 AM

Updated July 05, 2017 14:09 PM

Updated July 06, 2018 10:09 AM

Updated June 03, 2019 23:09 PM

Updated October 11, 2019 00:09 AM