1

I want to sort an output of a view so that the elements with a filed value of "0" be at the end of the list, but all the others are to be treated equally. In fact, it's an Drupal Commerce site, and I want all products with stock >0 to appear first in the lists, regardless of the stock quantity, so that the unavailable stuff get at the end, but still be in the view (after that, I sort alphabeitcally). Any ideas how to do that? That means making the output of a boolean forumla stock=0 the sorting criterion

3 Answers 3

1

You could do this if you use something like the Views php module, because that will allow you to use php code to sort by. I know that module is currently looking for additional maintainers, I just can't think of an alternative.

Once you've enabled it, and added a php field to sort by, I'd have it return something like:

min([commerce_stock], 1);  // replace [commerce_stock] with whatever your available stock variable is.

What that code will do is return 1 (the minimum) if you have any stock more than 1, but it will return 0 if the product is all out.

So it will only return 1 (there are items) or 0 (there are none), which is exactly what you want. Next, you can add a sort by name or whatever you want to sort these two groups further.

Let us know if it works. I was having a bit of problems getting this rolling, since my stock variable is just returning my product ID, and I'm not sure if I'm just running slightly out-of-date modules or what. So let us know if a similar approach works when you try it!!

3
  • that sounds promising. I have some trouble identitying the the stock variable used by Drupal Commerce / DC stock. I had some big problems in the meantime, regarding the availability of a view where nodes (commerce product displays) are sorted by a filed form an entity tied by a relationship (DC product). If the user category does not have the permission to view the products, it would not see the nodes.
    – Dalmaz
    Commented Apr 17, 2013 at 17:47
  • Searching for the variable, i just a discussion of my the problem I mentioned above: drupal.org/node/1276450
    – Dalmaz
    Commented Apr 17, 2013 at 18:00
  • Using just the code @Boriana mentioned did not bring the intended results :-(
    – Dalmaz
    Commented Apr 17, 2013 at 18:58
1

May not be the right answer but it works. On main view (filter stock>0), add a view block, add a filter by stock=0, on main view, insert the new block view as a footer, you can inherit url data on settings. Make CSS as needed.

0

I have not implemented this idea but this might help you.Add a hidden field to your product and give it a text value like A for those on stock and value B for those not in stock. Now u can sort by this field and i guess you might get what you wanted.I guess this might work.

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.