radical hacks Fourth Item Free Database hack sent in the following hack. here is what he had to say about the code....

This is a hack for the Webstore for ordering with a discount of buy 3 get the 4th item (least value of the 4) free. It will display the Number of free items they will receive, and will put the discount into the calculations starting at the order form.

As I am constrained for time and not a Perl programmer I utilized 2 globals to accomplish this. $store_discount is the actual monetary variable that holds the total discount amount, and $temp_discount, which holds the number of free items.

The way the discount is calculated is by calling up sort.pl which will open the cart, add a field and then place a copy of the quantity in that field. This is used to decrement with so as not to disturb the actual quantity field.

Then it sorts the cart from lowest price to highest price, as the discount will start with the lowest price items.

From there it decrements the total_discount counter (which is the original number of free items), adds the lowest price item to the $store_discount variable, and decrements the field that has the copied quantity.

When that field reaches zero, but with free items ($total_quantity) still existing, it moves to the next item in the cart (the second lowest priced item).

Then, after a little magic, it writes the info back to the cart.