I’m trying to find a solution to use a double inventory where a different stock is being used based based on the user role. I know there are many easy solutions to limit the amount of products they can order at once, but this doesn’t prevent them from placing another order. Ideally I would like to have a second inventory for each product which is used for a specific user role.
I’ve already been experimenting with a workaround where I add each product twice with a different inventory and show/hide the product based on user role. But this requires a lot of changes throughout the entire website to show/hide products from every page and element, not to mention all the links to product pages.
2
Answers
Thanks for getting me started, I already figured it would be a complex task :) With some minor adjustments in your code I got it working for simple products (just haven't tested the last step after purchase yet).
Being motivated by your approach, I pushed forward and created a solution for variable products by adding a custom field for each variation:
And saving the data from these custom fields again:
Everything above works fine :) but now I have been struggling for a few hours to get it working on the product page. I understand we need jQuery to get the variation id (unless I'm wrong?). Showing the variation id in HTML is easy, but how do we get the custom role stock value in php? Is it only possible with AJAX? Based on one of your examples I created this:
In another attempt I managed to get ALL role based stock quantities for every variation of the product without jQuery, but then I don't know how to determine which one is currently selected:
This is a very complex task. First, you need to display an additional input field in the Edit Product page:
Then, you need to save it:
Then, you need to filter stock quantity and status if the customer is logged in:
Finally, you need to reduce the correct stock on payment complete:
Totally untested and works with simple products only.