Limit the quantity of in-stock item on the cart page to stock level

Created on 23 May 2018, over 6 years ago
Updated 25 September 2023, about 1 year ago

Hello,
I added the follwing lines inside the function "viewsForm" inside the file "web/modules/contrib/commerce/modules/cart/src/Plugin/views/field/EditQuantity.php"viewsForm" of the module "Commerce Cart".
This allows me to maximize the number of items relative to the stock in the quantity fields of the form on the shopping cart page.

....
      $moduleHandler = \Drupal::service('module_handler');
      $stock = 0;
      if ($moduleHandler->moduleExists('commerce_stock')){
        $stockServiceManager = \Drupal::service('commerce_stock.service_manager');
        $purchasedEntity = $order_item->getPurchasedEntity();
        $stock = $stockServiceManager->getStockLevel($purchasedEntity);
      }
      $stock = ($stock != 0) ? $stock : 9999;

.....
        '#max' => $stock,
.....

It works pretty well, but is there a way to create a small module that will add this functionality without having to patch the contrib module?
or maybe it can be used for "commerce_stock" expect something better ...

Feature request
Status

Needs work

Version

1.0

Component

Code

Created by

🇫🇷France steveoriol Grenoble 🇫🇷

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

Production build 0.71.5 2024