Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added get variant stock event #1516

Closed
wants to merge 3 commits into from
Closed

Added get variant stock event #1516

wants to merge 3 commits into from

Conversation

samuelbirch
Copy link
Contributor

This will allow the ability for plugins to say how much stock is available for a variant. We can achieve negative stock/back ordering/etc using this.

I've added the EVENT_AFTER_REMOVE_LINE_ITEM to the_saveLineItems function in the Order element.

@lukeholder
Copy link
Member

Thanks Samuel, please give me some time to look through all this.

@samuelbirch
Copy link
Contributor Author

Hey @lukeholder, any thoughts on the get variant stock event?

@lukeholder
Copy link
Member

Sorry I am away from keyboard at the moment and was actually going to write a reply about the event when back. Give me a couple hours. From mobile.

@lukeholder
Copy link
Member

lukeholder commented Jul 20, 2020

Didn't realise my commit closed this pull request

@samuelbirch
Copy link
Contributor Author

@lukeholder no problem :)

@lukeholder
Copy link
Member

I have added 2 events to help with knowing when a line item was added and removed from the order in a clearer way with commit 7baba4a

We don't want getStock() to be a getter with an event at the moment. When we add an inventory management system in the future (#44), we will give plugins the opportunity to extend the system then.

Putting an event in the getter doesn't really make sense in the case of our basic stock counter being deducted when items are purchased. If you are hitting an external API to get the stock, probably best to set the stock to unlimited and add a custom field that manages your own stock (You can even add your own validator to the line item to check that stock).

If your plugin/module wants to add stock to a product, best to do something like this:

// Add 9999 stock to a variant.
Craft::$app->getDb()->createCommand()->update(
Table::VARIANTS,
['stock' => new Expression('stock + :qty', [':qty' => 9999])],
['id' => $variant->id]
)->execute();

If you want to explain your use-case more, happy to talk more about it here.

Thanks.

@samuelbirch
Copy link
Contributor Author

Thanks @lukeholder. I'm writing a inventory management plugin at the moment which does manage the stock separately. I'm needing a way to check that stock when for a line item is added/edited but also for admin orders in the variant element picker.

if i set everything to unlimited then in admin orders it would all be available but i still need to be able to say which is and which isn't available. Is there a solution for this?

@Anubarak
Copy link

Thanks for this pull request, I made a custom stock system as well and it's per "store" (think about it like in a restaurant)
And this logic helped me to get rid of the Events on record basis where I had to store the lineItem and the correct order during other events.

One Question: Any plans to trigger those functions, when an Order is deleted?
Currently I cannot increase my stock again unless I use the afterDelete Event of the order but it would be more convenient if deleting an order would trigger the event as well in it's custom "beforeDelete" function

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants