Skip to content

[Commerce 5] Purchasing a variant that has 0 stock or setting unlimited stock on variant #3649

Answered by lukeholder
hatyi asked this question in Q&A
Discussion options

You must be logged in to vote

As of Commerce 5.3, We now have the following event:

\craft\commerce\services\Purchasables::EVENT_PURCHASABLE_OUT_OF_STOCK_PURCHASES_ALLOWED

It works together with the new "Allow out of stock purchases" lightswitch on variants to allow your to override the value in the event based on the context.

Example:

use \craft\commerce\services\Purchasables;
use \craft\commerce\events\PurchasableOutOfStockPurchasesAllowedEvent;

Event::on(
  Purchasables::class,
  Purchasables::EVENT_PURCHASABLE_ALLOW_OUT_OF_STOCK_PURCHASES,
  function(PurchasableOutOfStockPurchasesAllowedEvent $event) {
      if($order && $user = $order->getUser()){
         if($user->isInGroup(1)){
             $event->outOfStockP…

Replies: 2 comments 1 reply

Comment options

You must be logged in to vote
1 reply
@hatyi
Comment options

Comment options

You must be logged in to vote
0 replies
Answer selected by lukeholder
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants