Hi folks,

One of my friend is having a problem, he is not in the mailing list so I am
posting this problem on his behalf. Please check how can this problem be
solved and let me know, I'll let him know the answer, thanks :)

*Problem:*
Where i work we use a perl based shopping cart (Cartit). It has a quantity
discount feature however it is limited to the discount being applied to one
item at a time (for example buy 25 or more of druk beads 10mm and get
10%off. Currently the shopping cart only applies the quantity discount one
item at a time.) we would like it to be have a "mix and match discount" -
using the previous example we like offer a customer the ability to get a
discount based on the total quantity ordered (based upon the description
being the same but the options ($description_ext being different)(for
example 10 red druk beads,10 blue druk beads, 10 white druk beads = 30
beads so they should get a 10 percent discount as long as the total order
amounts meets the level for the quantity discount.
the program already takes the "itemline" = FCART which is in an array and
splits into a string $FCART from which ,using the SPLIT function various
variables are extracted ($quantity,$sku,$description,etc..) and used for
calculations or other functions. However using a FOR loop this splitting isdone
one line at a time. Before the line is available for printing or other
things it is sent to a subroutine where the quantity discount is decided on
a line by line basis. We were wondering if would be possible to write a
subroutine that by using hashes and/ or arrays it would do the quantity for
discount on an accumulative basis( foe example - for all items having the
same sku or description (regardless on any options chosen)- it would add up
all the quantities and then we could use that "total quantity" for the
qualification for the quantity discount and pass it onto the "sub
get_discount_amountofcode" routine.
 Included is the following perl code discussed above ;

 for($i=0;$i<@FCART;$i++){
($quantity,$sku,$description,$description_ext,$price,$weight,$id,$taxable,$smarttaxname,$smarttaxperc,$smartistaxable,$smartcondition,$mref,$giftname,$giftfee,$discountcode)
= split(/\|/,$FCART[$i]);

($isqdisced,$price) =
&get_discount_amountofcode($quantity,$price,$discountcode);
$amount = sprintf("%.2f",$quantity*$price);
$trueamount = $amount;
 $totalgiftfee += ($giftfee *$quantity);
if($discount >0){
$trueamount = sprintf("%.2f",($trueamount - ($trueamount *$discount)));
$total_discount += sprintf("%.2f",($amount *$discount));
}
 if(($smarttaxname ne "")&&(($smartcondition eq "" ||
$TCONDITIONS{$smartcondition} ==1))){
if(!$SPECIALTAXES{$smarttaxname}){
$asc .= '*';
$SPECIALTAXES{"\_$smarttaxname"} = $asc;
}
 $SPECIALTAXES{$smarttaxname} += sprintf("%.2f",($trueamount *
$smarttaxperc));
 }
 $total += sprintf("%.2f",$amount);
$truetotal += sprintf("%.2f",$trueamount);
if($taxable){
if($smartistaxable){
 $taxabletotal += sprintf("%.2f",($trueamount + ($trueamount *
$smarttaxperc)));
}else{
$taxabletotal += sprintf("%.2f",$trueamount)
 }
}
 $numprodincart+=$quantity;
   $return_data .= "<TR>\n";
 $return_data .= "<TD ALIGN=\"CENTER\" class=cartbody$tablecolor>";
 if($editable){
$return_data .= "<INPUT TYPE=\"TEXT\" NAME=\"cartitq_$id\"
VALUE=\"$quantity\" SIZE=\"3\" MAXLENGTH=\"4\" class=quantityinput>";
 }else{
$return_data .= "$quantity";
}
 $return_data .= '</TD>';
 $return_data .= "<TD align=\"center\" class=cartbody$tablecolor><a
href=\"$USERCONFIGS{'cgi_bin'}/cartit.cgi?lhm=$id&cid=$cart_ID\">$USERCUSTOM{'TRASH_CAN'}</a></TD>"
if($editable);
  if($USERCONFIGS{'go_gift'}){
$return_data .= "<TD align=\"center\" class=cartbody$tablecolor>";
if($editable){
$return_data .= sprintf("<a
href=\"$USERCONFIGS{'cgi_bin'}/cartitgift.cgi?cartitgift=2&id=$id&cid=$cart_ID&g=%s\">",urlencode($giftname));
$return_data .=  $giftname ne '' ? "$USERCUSTOM{'GIFT_CHECK_BOX_ON'}" :
"$USERCUSTOM{'GIFT_CHECK_BOX_OFF'}";
}else{
$return_data .=  $giftname ne '' ? "Yes" : "No";
 }
$return_data .= "</a>" if($editable);
$return_data .= "</TD>";
}
 $return_data .= "<TD align=\"center\" valign=\"middle\"
class=cartbody$tablecolor><a
href=\"$USERCONFIGS{'cgi_bin'}/cartit.cgi?sfl=$id&cid=$cart_ID&sq=$quantity\">$USERCUSTOM{'SAVE_FOR_LATER'}</a></TD>"
if($editable);
  $return_data .= "<TD class=cartbody$tablecolor>";
  if($sku ne ""){
$return_data .= '(' . $USERCUSTOM{'SKU_DISPLAY_TEXT'} . $sku . ') ';
}
 if($mref ne "" && $editable){
$return_data .= " <A HREF=\"$mref\">$description</A>";
}else{
$return_data .= " $description";
}
 if(($smarttaxname ne "")&&(($smartcondition eq "" ||
$TCONDITIONS{$smartcondition} == 1))){
$return_data .=  $SPECIALTAXES{"\_$smarttaxname"};
}
 $return_data .=  " $description_ext ";
 if($isqdisced){
$return_data .=  sprintf("<I>Your quantity discount for this item is %2d%.
This discount is reflected in the unit price of this item.</i>",$isqdisced
* 100);
}
 if($USERCONFIGS{'go_gift'}){
if($giftname ne ""){
if($editable){
$return_data .= sprintf("<BR>[This item is a gift for &quot;<A
HREF=\"$USERCONFIGS{'cgi_bin'}/cartitgift.cgi?cartitgift=2&id=$id&cid=$cart_ID&g=%s\">$giftname</A>&quot;]",urlencode($giftname));
}else{
$return_data .= "<BR>[This item is a gift for &quot;$giftname&quot;]";
}
}
 }
 $return_data .= "</TD>\n";
     $return_data .= "<TD ALIGN=\"RIGHT\" VALIGN=\"TOP\"
class=cartbody$tablecolor>$price</TD>\n";
 $return_data .= "<TD ALIGN=\"RIGHT\" VALIGN=\"TOP\"
class=cartbody$tablecolor>$amount</TD>\n";
   $return_data .='</TR>';
 $tablecolor++;
$tablecolor=0 if($tablecolor==2);
}
 This is the subroutine where the quantity discount is figured out:
##############################################################
sub get_discount_amountofcode{
my($myquantity,$myamount,$codestuff) = @_;
return(0,$myamount) if($codestuff eq "");
# return($myamount ) if($godiscount eq "");
my($godiscount) = 0;
my($part);
@PARTS = split(/#/,$codestuff);
foreach $part (@PARTS){
my($dfrom, $dto, $damount) = split(/:/,$part);
if(($myquantity >= $dfrom)&&($myquantity <= $dto || $dto eq "+")){
$godiscount=($damount / 100);
last;
}
}
  return("",$myamount ) if($godiscount ==0);
return($godiscount,sprintf("%.2f",$myamount * (1-$godiscount)));
}
###############################################
#$godiscount is the percentage amount to be applied for the quantity
discount

Reply via email to