[ic] Prevent from ordering more than is in stock

Jeff Dafoe interchange-users@icdevgroup.org
Mon Oct 14 15:24:01 2002


> I have prevented out of stock orders (foundation demo) by conditionally
> displaying the quantity box / having an 'Add to cart' button clickable in
the
> various pages (results.html, flypage.html, and the components such as
random,
> etc.), by use of a [if-item-data inventory quantity > 0] tag. I also got
rid
> of the 'Out Of Stock' and 'N' hyperlinks in flypage.html and results.html
> respectively.

    I have this in my templates/components/cart.  Display q_message
somewhere in your "error message" area of the cart and it will let users
know if their order has been modified.  This code came out of this llist and
seems to work well.

    It should probably go in the ord/checkout file also, although I don't
have it there because I don't have that much concurrency.  For sites with
really high item concurrency at checkout there should probably be something
in the etc/profiles.order also.  For sites with mega ultra crazy order
volume and small stock quantities it might even be necessary to go beyond
that, one might have to start doing some sort of encapsulation using a
database transaction or a nasty semaphone to check the stock level and
decrement it or kick the order back all in one autonomous, serialized
operation.  But, man, if I ever get to that transaction volume I'll be able
to pay someone else to do that while I sit on the beach and drink
margaritas.  Until that point I will be using this:

[comment]dont let users add more to cart than we have in stock[/comment]
[perl tables=inventory]
  my $item;
  foreach $item (@{$Carts->{main}}) {
    my $on_hand = tag_data('inventory', 'quantity', $item->{code});
    next if $on_hand >= $item->{quantity};
    if ($on_hand<=0) {
      $item->{quantity} = 0;
      $item->{q_message} = "Item is currently out of stock.";
    } else {
      $item->{quantity} = $on_hand;
      $item->{q_message} = "Limited item stock, order quantity adjusted.";
    }
  }
[/perl]



Jeff