In web_store.cgi: Replace: &require_supporting_libraries (__FILE__, __LINE__, "$sc_cgi_lib_path", "$sc_html_setup_file_path", "$sc_mail_lib_path"); With: &require_supporting_libraries (__FILE__, __LINE__, "$sc_cgi_lib_path", "$sc_html_setup_file_path", "$sc_mail_lib_path", "$sc_sort_path"); In web_store.setup. Add: $sc_sort_path = "./Library/sort.pl"; In web_store_html_lib.pl : Replace: elsif ($reason_to_display_cart =~ /delete/i) { &cart_table_header("Delete Item"); } else { &cart_table_header(""); } # Next, the client's cart is read line by line (file open # errors handled by file_open_error as usual). open (CART, "$sc_cart_path") || &file_open_error("$sc_cart_path", "display_cart_contents", __FILE__, __LINE__); With: elsif ($reason_to_display_cart =~ /delete/i) { &cart_table_header("Delete Item"); } else { &cart_table_header(""); } sort_fields(); #This is the change. It calls the sort.pl file # Next, the client's cart is read line by line (file open # errors handled by file_open_error as usual). open (CART, "$sc_cart_path") || &file_open_error("$sc_cart_path", "display_cart_contents", __FILE__, __LINE__); ******************************************************** In web_store_order_lib.pl: replace: if ($final_discount > 0) { $final_discount = &display_price($final_discount); print "Discount: $final_discount

"; $text_of_cart .= &format_text_field("Discount:") . "= $final_discount\n\n"; } with: if ($store_discount > 0) { $final_discount = &display_price($final_discount); print "Discount: $store_discount

"; $text_of_cart .= &format_text_field("Discount:") . "= $store_discount\n\n"; } ******************************************* replace: $final_discount = $discount if ($discount > 0); $final_shipping = $shipping if ($shipping > 0); $final_sales_tax = $sales_tax if ($sales_tax > 0); $temp_total = $temp_total - $discount + $shipping + $sales_tax; } # End of $calc_loop # # The grand total becomes the final temp # total after the routine has been processed $grand_total = $temp_total; with: $final_discount = $discount if ($discount > 0); $final_shipping = $shipping if ($shipping > 0); $final_sales_tax = $sales_tax if ($sales_tax > 0); $temp_total = $temp_total - $discount + $shipping + $sales_tax; } # End of $calc_loop # # The grand total becomes the final temp # total after the routine has been processed $grand_total = $temp_total - $store_discount; #this is the revised line Replace: if ($store_discount > 0) { $final_discount = &display_price($final_discount); print "Discount: $store_discount

"; $text_of_cart .= &format_text_field("Discount:") . "= $store_discount\n\n"; } With: if ($store_discount > 0) { $final_discount = &display_price($final_discount); print "Discount for $temp_discount free item(s): $store_discount

"; ####This is the revised line###### $text_of_cart .= &format_text_field("Discount:") . "= $store_discount\n\n"; } ********************************************************************* Save this section as sort.pl and place in /Library: ############################################################## # Sort The Fields ############################################################## sub sort_fields (){ local($count) = 0; local($row); local(@row1); local($sortable_field); local($new_row); local(@new_rows); local(@sorted_rows); local($sorted_field); local($old_but_sorted_row); local(@database_rows); local($total_count) = 0; local($temp_quantity) = 0; ################################################################ # # # First we open the CART. In our cart we have a field that # # is the 4th field after the quantity field is added (So the # # third field in your database). We make this field equal the # # quantity field. This is so we have a counter we can decrement# # as we are decrementing the $total_discount (Number of free # # items) and increasing the $discount (Monetary amount of the # # discount). Then we sort on the price (field 3 in the CART) # # The reason we do this is we will bring the CART back up from# # the first line and start the discount process. # ################################################################ open (CART, "$sc_cart_path") || &file_open_error("$sc_cart_path", "display_cart_contents", __FILE__, __LINE__); @row1 =; close(CART); while (@row1[$count] ne "") { $row = @row1[$count]; @row = split (/\|/, $row); $sortable_field = $row[3]; $quantity = $row[0]; $total_count += $quantity; $row[4] = $row[0]; $total_discount = int($total_count/4); unshift (@row, $sortable_field); $new_row = join ("\|", @row); push (@new_rows, $new_row); $count++ } @sorted_rows = sort (@new_rows); print "Total Free Items: "; print ($total_discount); print "
"; ##Place holder for orderform free items ################# $temp_discount = $total_discount; ##Now put the field back where it belongs################ @database_rows = (); foreach $sorted_row (@sorted_rows) { @row = split (/\|/, $sorted_row); $sorted_field = shift (@row); $old_but_sorted_row = join ("\|", @row); push (@database_rows, $old_but_sorted_row); } $store_discount = 0; $count=0; @row1 = ; close(CART); while (@database_rows[$count] ne "") { $row = @database_rows[$count]; @row = split (/\|/, $row); while (($row[4] >0) && ($total_discount > 0)) { $store_discount = $store_discount + $row[3]; $row[4] = $row[4] - 1; $total_discount = $total_discount - 1; } $count++; } @sorted_rows = sort (@new_rows); print "Total Discount: "; print ($store_discount); print "
"; @database_rows = (); foreach $sorted_row (@sorted_rows) { @row = split (/\|/, $sorted_row); $sorted_field = shift (@row); $old_but_sorted_row = join ("\|", @row); push (@database_rows, $old_but_sorted_row); } ################################################## # print the results into the CART # ################################################## open (CART, ">$sc_cart_path"); print CART (@database_rows); close(CART); } 1; # Libraries always end this way so that they return true to requir