Views filter don't work with computed fields out of the box.
We will likely need to create a custom filter plugin for this.
First we should investigate, if there are some implemantations in contrib.
| Comment | File | Size | Author |
|---|---|---|---|
| commerce_stock-views_filter_2018.10.02-12-56-45.png | 115.01 KB | mrpauldriver | |
| commerce_stock_views_filters-2018.10.02-12-58-35.png | 65.58 KB | mrpauldriver | |
| commerce_stock_views_filters-2018.10.02-13-03-12.png | 143.63 KB | mrpauldriver |
Comments
Comment #2
mrpauldriver commentedComment #3
guy_schneerson commentedThanks for reporting Paul.
This is because the stock level is a calculated field and does not hold a value that views can interrogate. We will probably need to create a Views Filter Plugin to handle this.
Comment #4
olafkarsten commentedComment #5
langelhc commentedSubscribing...
Comment #6
olafkarsten commented@langelhc - no need to comment for subscribing to an issue. Have a look at the right sidepane (or footer on mobile), there is a "Follow" link with a star icon somewhere
Comment #7
alyaj2a commentedI have a question, Are you planning to implement the field to be stored in the DB?
Comment #8
olafkarsten commented@alyaj2a
No, the field will not be stored in DB. Commerce stock is transaction based. That means we have a journal with every stock transaction that took place. We query that table just in time we need the level. That is why we will not save the absolute level to db.
Comment #9
alyaj2a commented@olafkarsten , I'm understand, But Is there some way create filter criteria in views with the level stock field? Or use the stock level field in sort criteria?
Comment #10
olafkarsten commentedfeel free to reopen
Comment #11
czigor commentedReopening this as it's still impossible to add a stock filter to a variation view. E.g. "show variations that have stock larger than 100".
Comment #12
ahkhoon commentedI am having the same issue, I am promoting some products at front page with View and I wouldn't want to show products which are currently out of stock. There is no way to filter by stock level.
I know programatically one can get the stock level by:
$stockServiceManager->getStockLevel($variation)
Comment #13
remaye commentedHi, I'm interested too in having access to stock level in a view (filtering, sorting).
Does anybody have a clue on how to use $stockServiceManager->getStockLevel($variation) for that ?
Thanks.
Comment #14
ericchew commentedI have a similar situation where I want to create a view for showing variations that are "Low on Stock". We have a variation field called "low_quantity" that i'd like to compare to the current stock level. If the current stock level is less than "low_quantity", then the variation would show in the View table. A few details about my setup...we only have 1 stock location and only hold stock levels on product variations.
I was able to get this to work, but there are a few hoops to jump through...and it is not something that would work for everyone. Here are the details of what I've learned about how this module works and the quirks of using views with it.
With all that being said, my solution was:
LocalStockTransactionEvents::LOCAL_STOCK_TRANSACTION_INSERTso that on EVERY transaction, the stock location level is updated.the localStockService is injected into the event subscriber, this is the service
commerce_stock.local_stock_serviceI'm not sure if it is possible to create a views filter for something that is not stored in the database, so this was my solution. Hopefully it helps.
*EDIT*
I'd also like to add a few points from my slack conversation with @olafkarsten and @Guy Schneerson.
This was my observation as to how the real-time stock level is calculated / displayed:
I asked why the stock_location_level is not updated on every transaction, this was the response from @olafkarsten:
Comment #15
josh.stewart commented@ericchew would you be willing to share your code from your solution above? I've got a similar problem that I need to figure out and would love a good boost. Commerce is very new to me and the more code I can look at the better I think. I'm in Drupal slack at jmstewart if that is easy enough. Would appreciate all the help I can get here.
Comment #16
dubs commentedIn my option, stock filtering is ESSENTIAL for views. There are SO many use cases for requiring stock level information in views.
That being said, here's an alternative solution that works for me. For the reasons mentioned already in related tickets, this may not be the best idea for high traffic sites....
Create a new field on the product variation, and then create an event subscriber for LocalStockTransactionEvents::LOCAL_STOCK_TRANSACTION_INSERT
This simply grabs the current stock level like this: -
Then you can use the field in Views for filtering, displaying, etc. One advantage for displaying is no overhead for stock transactions which the default stock field will use.
Bearing in mind the above disclaimed, use at your own risk - it works for me :-)
Comment #18
guy_schneerson commentedI have updated the views integration so an implicit relationship is added from location level to product variation. This will allow adding a "Quantity" under the "Local Stock Location Level" category to both the sort and filter.
In order for the stock location to be up to date, you will need to set "Stock aggregation mode" to "Real-time" in a new config screen under admin/commerce/config/stock/local_stock_config
The only remaining issue will be that products that have not had any transaction (stock activity) will not show. This has a core issue that once fixed will resolve this Base field filter operators do not include "Is empty (NULL)" and "Is not empty (NOT NULL)"
Please test and let me know if this fixes the issue?
Comment #19
netzkombuese commentedHi.
As this is working now for us and we are able to sort and filter on Quantity we just now get this when running Cron:
Error: Call to a member function id() on null in _commerce_stock_local_update_stock_level_queue() (Zeile 92 in /var/www/vhosts/netzkombuese-entwicklung.de/hexdev/modules/contrib/commerce_stock/modules/local_storage/commerce_stock_local.module)We just added updates only patch from 22 (https://www.drupal.org/project/commerce_stock/issues/3211778) to the latest dev to be able to run update.php which was not able before the patch.
Any ideas what went wrong here?
Thanks
Comment #20
guy_schneerson commentedI am glad this is working for you so marking it as fixed. but if anyone finds an issue they can reopen.
The 3211778 had a small issue and a patch was provided by @mitrpaka and I just committed that change. A new dev version will automatically be created by drupal.org in the next day and the update hook will start working corectly.
Comment #21
netzkombuese commentedHi and sorry we have to reopen this. We have now installed the last dev version. Unfortunately we still get the same error message when we start the cron.
Error: Call to a member function id() on null in _commerce_stock_local_update_stock_level_queue() (Zeile 92 in /var/www/vhosts/netzkombuese-entwicklung.de/hexdev/modules/contrib/commerce_stock/modules/local_storage/commerce_stock_local.module)Any ideas whats wrong here?
EDIT:
Just thought we got it by going back to alpha 6 and reinstalling dev but the error reoccured after a few minutes.
So even with the actual dev version we get the error listed above. This does not appear with alpha6 which does not have the needed functionality. What are we doing wrong?
Thanks
Comment #22
netzkombuese commentedComment #23
guy_schneerson commentedHi @netzkombuese
The error you are getting is not related to this issue. However, it is a new one and I think I figured it out in https://www.drupal.org/project/commerce_stock/issues/3248964 and will be easy to fix.