If ProductA has 5 in stock, and I add 5 or less to the Cart, I can continue to add 5 or less to the cart regardless of the cart's existing quantity.

Obviously moving to Checkout or updating the cart from the cart page reveals the stock validation error, however this still means the user has more items in their cart than they can purchase, and the stock levels are not automatically reduced, meaning that the user needs to manually lower the stock levels to get around the error.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

guy_schneerson’s picture

Priority: Major » Normal

Hi @carn1x,
Yes i am aware of this issue. The initial goal was to make sure users cant order out of stock items and make sure the module operates securely and with no errors, i was planning of adding this functionality as part of version 2 but may try and do it before as i agree it is not the best user experience.
i am downgrading priority to Normal, as most of the issues raised so far actually stooped the module from working as described above.

carn1x’s picture

Thanks a lot :)

carn1x’s picture

Ok in the interests of time, I hacked the commerce_stock.module and changed the following function:

function commerce_stock_add_to_cart_validate($form, &$form_state) {
  $qty = $form_state['input']['quantity'];
  $product_id = $form_state['input']['product_id'];                                                                                            
  $product = commerce_product_load($product_id);
  if (!(isset($product->commerce_stock_override['und']) && $product->commerce_stock_override['und'][0]['value'] == 1)) {
    if (commerce_stock_product_check_out_of_stock($product_id, $qty, $remaining_stock)) {
        form_set_error("stock_error", t('Maximum quantity of %title that can be purchased is %max', array('%title' => $product->title, '%max' => $remaining_stock)));
    }else{
      global $user;
      //load the current cart
      if ($order = commerce_cart_order_load($user->uid)) {
        $cart_qty = 0;
        //cycle throw each line item ID
        foreach($order->commerce_line_items['und'] as $line_item_id){
          //load each line item in the cart
          $line_item = commerce_line_item_load($line_item_id['line_item_id']);
          if($line_item->commerce_product['und'][0]['product_id'] == $product_id){
            //add the line item quantity to the quantity check
            $cart_qty += $line_item->quantity;
          }
        }
        $qty += $cart_qty;
        if (commerce_stock_product_check_out_of_stock($product_id, $qty, $remaining_stock)) {
          form_set_error("stock_error", t('Maximum quantity of %title that can be purchased is %max, and you already have %cart_qty in your shopping cart.', array('%title' => $product->title, '%max' => $remaining_stock, '%cart_qty' => $cart_qty)));
        }
      }
    }
  }
}

Since you're aware of this issue and working on V2, I won't bother rolling a patch, please anybody let me know if I can improve the code at all. Still very noobish regarding what functions commerce and entity API provides :)

guy_schneerson’s picture

Hi @carn1x,
looks good if you got time to add a patch i will include it in version 1, let me know if you don't and ill make the change when i get a chance.

joachim’s picture

Several points:

- this fix doesn't cover the case where one product node shows multiple products -- the commerce_stock_add_to_cart_validate validator is not added to the form in this case. we probably need a new, separate validator to handler already-in-cart stock checks
- secondly, I'm not sure this is the best way to deal with the use case. What if there are two widgets, and Alice adds one to her cart, then Bob adds one to his, and Alice adds one more to her cart? Who wins? Is it whoever reaches checkout first? Admittedly, this makes the whole stock control rather more complex, because it becomes a resource allocation system, and you have to build a system for releasing resources after a certain time. Basically, adding an item to your cart would mean you decrease stock count and have it for a certain time. Also, not all sites would necessarily want this model. One for another issue, I think.
- code style in the patch: comments should be full sentences; variables shouldn't use abbreviations; run the code through Coder module for other tweaks :)

joachim’s picture

guy_schneerson’s picture

Thanks @joachim this patch is unlikely to go into the V1 as I have now started work on version two and will review this functionality for all cases.

guy_schneerson’s picture

Status: Active » Needs review
FileSize
2.57 KB

patch based on the stock V2 function for taking the current Cart quantity into account for the add to cart functions.

guy_schneerson’s picture

Status: Needs review » Fixed

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.

hibersh’s picture

Status: Closed (fixed) » Needs review
FileSize
823 bytes

patch adds line item type check, avoid exception for entitymetadata wrapper

guy_schneerson’s picture

thanks @hibresh i think you solved an issue that was responsible for http://drupal.org/node/1411032#comment-5575072, I am going to look into this and a few other issues this weekend.

I think the correct way to handle this will be by making sure $line_item->type is in commerce_product_line_item_types().
missed that out :(

guy_schneerson’s picture

patch fixes the issue adds a check that the line item is a product, i am committing this for both version 1 and 2.

guy_schneerson’s picture

guy_schneerson’s picture

Status: Needs review » Fixed
carn1x’s picture

Awesome thanks :D

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.