-1

Slightly unusual case. I've got a Drupal Commerce store making hand-made goods, where the client wants sold stock to be visible in product list views marked as sold out, always at the end of every list, so that people who've browsed all the available stock without finding what they want can get ideas based on products that have been made before and could put in a request for something like a previously sold product to be made.

So, I want a sort that takes the commerce stock field, which is a fairly ordinary numeric field, and sorts on it, but treating all numbers greater than or equal to 1 as 1.

The conversion is easy, the sorting algorithm would be easy to code, and I'm sure this could be done with a very small custom module hook somewhere in the Views sort process - I'm just not sure which of the many Views hooks would be the appropriate position in the views sorting process to modify the field value like this. Also, there might be a better way. I'd prefer not to create a duplicate boolean field then sort on that, as it might end up out of sync.

How can I sort on a numeric field like it was a boolean field like this?

1

2 Answers 2

1

You would have to probably sort against the stock level field and then also sort against the boolean. That is native in Views.

So, Add the sort to sort by the Boolean field and sort with DESC so that the out-of-stock items fall to the bottom.

And then under it add your default sort (stock level) as normal.

Edit: Reading into this again and rereading your question... I will expand the above. there are two ways to do this.

  1. is above - with the addition of a boolean field which would have to get flipped if your stock levels drop to zero. you could use rules to make that happen and populate the field based on the condition as part of your checkout ruleset.

  2. You might also be able to accomplish this with views aggregation to group and modify that sort.

4
  • The problem is that commerce stock does not provide such a boolean ;) Commented Dec 7, 2016 at 19:11
  • 1
    Sorry, updated my answer.
    – Remog
    Commented Dec 7, 2016 at 19:13
  • That's what I was planning to do after solving this question and figuring out how to sort by 0 or >0. Views Aggregation (and Views Calc) are interesting ideas, will see if they have any such feature Commented Dec 7, 2016 at 19:15
  • Views aggregation is built into views. if you click on Advanced there is an aggregate option there. There are a few guides out there on how to implement it.
    – Remog
    Commented Dec 7, 2016 at 19:19
1
+50

Not the most elegant solution, but it still works...

Have you view in two displays: main (anything) and zeros (attachment)

  • In the main, set a filter with your field and a condition "greater than" 0. (Not greater than or equal to zero")

  • In the zeros display, set your field and a condition "equal to" 0.

If you want the rows with the field = zero first, add the attachment before; if last, add the attachment after.

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.