Hi there,
I manage an online shopping cart and the quantity discount (either % or $ amt per unit) portion is not working. We have coupon codes, which are working, but I wanted to see if anyone could see any trouble with the following code that deals with our discounts which are based on quantity of item ordered:
Currently, no matter what quantity you enter in the shopping cart, the full price per unit appears and the quantity discounts are not applied.
Any feedback would be greatly appreciated.
/* SAVE DISCOUNT FUNCTIONS */
function saveDiscounts($info, $pID){
for($x=0; $x<$info[control2]; $x++){
$data[pID] = $pID;
$data[dsID] = $info[dsID][$x];
$data[dsMin] = $info[dsMin][$x];
$data[dsMax] = $info[dsMax][$x];
$data[dsDiscount] = str_replace("\$", "", $info[dsDiscount][$x]);
if($data[dsMin] && !$data[dsID]){
$this->insertDiscount($data);
} elseif($data[dsMin] && $data[dsID]) {
$this->updateDiscount($data);
} elseif(!$data[dsMin] && $data[dsID]) {
$this->deleteDiscount($data[dsID]);
} else {
// do nothing
}
unset($data);
}
}
function insertDiscount($info){
$Q = "insert into ds_discounts "
. " (pID, "
. " dsMin, "
. " dsMax, "
. " dsDiscount) "
. "values "
. " (\"$info[pID]\", "
. " \"$info[dsMin]\", "
. " \"$info[dsMax]\", "
. " \"$info[dsDiscount]\") "
. "";
$this->mySQL_Query($Q);
}
function updateDiscount($info){
$Q = "update ds_discounts set "
. " dsMin = \"$info[dsMin]\", "
. " dsMax = \"$info[dsMax]\", "
. " dsDiscount = \"$info[dsDiscount]\" "
. "where "
. " dsID = '$info[dsID]' "
. "";
$this->mySQL_Query($Q);
}
function deleteDiscount($dsID){
$Q = "delete from "
. " ds_discounts "
. "where "
. " dsID = '$dsID' "
. "";
$this->mySQL_Query($Q);
}
function getDiscountArray($pID){
$Q = "select "
. " dsID, "
. " dsMin, "
. " dsMax, "
. " dsDiscount "
. "from "
. " ds_discounts "
. "where "
. " pID = '$pID' "
. "";
$result = $this->mySQL_Query($Q);
$x=0;
while($list = mysql_fetch_array($result)){
$array[dsMin][$x] = $list[dsMin];
$array[dsMax][$x] = $list[dsMax];
$array[dsDiscount][$x] = $list[dsDiscount];
$array[dsID][$x] = $list[dsID];
$x++;
}
return $array;
}
function getProductDiscount($pID, $pQty){
$dsMin = ($pQty-1);
$dsMax = ($pQty+1);
$Q = "select "
. " dsDiscount "
. "from "
. " ds_discounts "
. "where "
. " pID = '$pID' and "
. " '$pQty' >= dsMin and "
. " '$pQty' <= dsMax "
. "";
$result = $this->mySQL_Array($Q);
return $result[0];
}
function getOriginalPrice($pID, $oID=''){
if($oID > 0){
$Q = "select "
. " oPrice "
. "from "
. " ds_options "
. "where "
. " oID = '$oID' "
. "";
$result = $this->mySQL_Array($Q);
if($result[0] > '0.00'){
return $result[0];
}
}
$Q = "select "
. " pPrice, "
. " pSalesPrice, "
. " pSale "
. "from "
. " ds_products "
. "where "
. " pID = '$pID' "
. "";
$result = $this->mySQL_Array($Q);
$pPrice = ($result[pSale]=="Y" && $result[pSalesPrice]>0) ? $result[pSalesPrice] : $result[pPrice];
return $pPrice;
}
function applyDiscounts($cSession){
$result = $this->getOrder($cSession , "N");
while($list = mysql_fetch_array($result['result'])){
$cpDiscount = $this->getProductDiscount($list['pID'], $list['pQty']);
// figure discount if there is one
if($cpDiscount){
if(strstr($cpDiscount, "%")){
$cpDiscount = str_replace("%", "", $cpDiscount);
$cpDiscount = trim($cpDiscount);
$oPrice = $this->getOriginalPrice($list['pID'], $list['oID']);
$discount = round($oPrice * ($cpDiscount/100), 2);
$nPrice = ($oPrice-$discount);
$this->updateNewCartPrice($list['cID'], $nPrice);
} else {
$oPrice = $this->getOriginalPrice($list['pID'], $list['oID']);
$nPrice = ($oPrice-$cpDiscount);
$this->updateNewCartPrice($list['cID'], $nPrice);
}
// if no discount applies put
// the price back to the original price
} else {
$nPrice = $this->getOriginalPrice($list['pID'], $list['oID']);
$this->updateNewCartPrice($list['cID'], $nPrice);
}
}
}
function updateNewCartPrice($cID, $pPrice){
$Q = "update ds_carts set pPrice='$pPrice' where cID='$cID'";
$this->mySQL_Query($Q);
}