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 ...

Comments

steveoriol created an issue. See original summary.

steveoriol’s picture

steveoriol’s picture

Title: how to turn a patch into a custom module » Maximize the quantity of in-stock item on the cart page
steveoriol’s picture

Issue summary: View changes
fool2’s picture

Assigned: Unassigned » fool2
Category: Support request » Feature request

This should be done as a form_alter from commerce_stock, not directly in commerce cart.

fool2’s picture

Title: Maximize the quantity of in-stock item on the cart page » Limit the quantity of in-stock item on the cart page to stock level
StatusFileSize
new1.1 KB

We need to test this patch. There is also an opportunity to remove the form altogether here, if it is an item that only has one in stock.

fool2’s picture

Status: Active » Needs review
StatusFileSize
new1.08 KB

Fixed formatting and a mistake

fool2’s picture

StatusFileSize
new1.09 KB

I'm a mess

luksak’s picture

StatusFileSize
new1.7 KB

I needed the default values to be adjusted directly. The attached patch does that. This functionality could be provided as a configurable option.

luksak’s picture

Status: Needs review » Needs work

This needs a re-roll at least.

joey91133’s picture

Status: Needs work » Needs review
StatusFileSize
new1.62 KB

re-roll at least