I am currently working on a project where I have to sort products based on their times being sold. Unfortunately I could not find anything related to this functionality.

Where do I start to implement this feature/functionality?

1 Answer 1


Here is a simple start. It builds a query, which goes into the commerce lineitems table, groups the rows by purchasableId and gives the top 20 counts. Be aware, that the result consists of Variants-IDs.

        $result = (new Query())->select([
            'COUNT(purchasableId) as times_bought',
            ->orderBy(['times_bought' => SORT_DESC])

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.