[Commerce 5] Purchasing a variant that has 0 stock or setting unlimited stock on variant #3649
-
Hello all I have a requirement from a client that says if "some conditions are fulfilled users should be able to buy a purchasable even if it doesn't have stock". Alternative solution could be to set unlimitedStock on all variants (but I have no idea how to do this since the documentation doesn't cover it, just mentions it), naturally this would mean I have to throw out all these new mechanics around inventory management that had been introduced in the latest version. Client doesn't have multiple warehouses so it won't be an issue. Thanks a lot in advance. |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
Based on the help I got from @AugustMiller I think I was able to make some progress with this.
And I have added the updateStock method as a behavior on the variants based on what I have found in
I'm going to keep this discussion open, since I'm sure there are better ways to do this, or there will be eventually. |
Beta Was this translation helpful? Give feedback.
-
As of Commerce 5.3, We now have the following event:
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->outOfStockPurchasesAllowed = true;
}
}
}
); Hope that helps you project! |
Beta Was this translation helpful? Give feedback.
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: