[ic] Prevent from ordering more than is in stock

Interchange user interchange-users@icdevgroup.org
Wed Oct 16 18:56:01 2002


On Tuesday 15 October 2002 04:48 pm, you wrote:
> Quoting Interchange user (interchange.mail@virgin.net):
> > >     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
> >
> > Thanks Jeff, that's just the type of thing I was looking for. I've put
> > that at the top of ord/checkout.html, as I figure it should be parsed
> > before anything else, but it doesn't seem to be having any effect on the
> > quantities (I've restarted Interchange). Any ideas?
>
> As part of my continuing series plugging IC 4.9, there is now a feature
> that does this:
>
>     MaxQuantityField  inventory:quantity
>
> You can set a field which will intrinsically limit the order quantity to
> the amount in inventory. If the user orders 9 while 9 are in stock, then
> there is an intervening order for 5, the number on order will automatically
> be adjusted to 4 on the next cart refresh.
>
> (The observant will notice that there are of some possible problems with
> contention there, as it would be possible to have an order adjusted
> between time of loading the checkout page and time of actual order
> placement. There is some exception handling possible with this, but it
> isn't documented yet. This type of thing is a very advanced function
> though, and probably anyone using it should be capable of perusing the
> source.)
>
> There is also
>
>     MinQuantityField  inventory:min_order
>
> which enforces a minimum order quantity automatically. Both of these set
> warnings via $Tag->warning() when an adjustment takes place.



Mike,

Until I upgrade, I want to get this code working.

Although I can see exactly what the code is supposed to be doing, I'm not 
100% sure that the syntax is correct. Having said that, it's working fine for 
Jeff (I also tried it in the cart component, to no avail), and I notice that 
you suggested something very similar (see below). Can you offer me any 
pointers as to where I'm going wrong?



<<
> First, a big thanks to the developers for a nice piece of software!
> 
> But it's a complex one, so one question:
> 
> How do I modify [item-quantity] in an [item-list]

You cannot, directly.

> 
> 
> Since I have limited stock, I need to stop users from ordering more
> items than I have.

There are a number of ways to do this. Here is one, placed at the top
of the basket and checkout page:

[perl tables=inventory]

        my $cart = $Carts->{main};
        my $item;
        foreach $item (@cart) {
                my $on_hand = tag_data('inventory', 'quantity', $item->{code});
                next if $on_hand >= $item->{quantity};  <
                $item->{quantity} = $on_hand;
                $item->{q_message} = "Order quantity adjusted to fit stock.";
        }

[/perl]

Then you can place [item-modifier q_message] in the cart somewhere to
tell them when this happened.
>>