Page 1 of 3

discount voucher how to set up using php

Posted: Mon Mar 25, 2013 9:28 am
by jonnyfortis
Hi want to add a field to my shopping cart page that can have a discount code (created in the backend of the system and stored in a PHP MySQL database) inserted then this gives a discount to the grandTotal of the shopping cart and shown in the checkout page.

I have a db built with three columns

Code: Select all

disID (int) 11 = 1
disCode (varchar)200 = discount0001
disValue (varchar)200 = .10 (as %10)
can anyone help

thanks

Re: discount voucher how to set up using php

Posted: Mon Mar 25, 2013 12:39 pm
by requinix
Help with... what? You seem to be on the right track.

Re: discount voucher how to set up using php

Posted: Tue Mar 26, 2013 6:29 am
by jonnyfortis
i need to know how to implement it in the front end

on the shopping cart i will have a input field that inserts the value of disCode column, this then compares the value and shows and error if the value doesnt match but if it does match then when the user is sent to the checkout the disValue is carried across and deducted of the $XC_GranTotal

but i dont know how to do this

Re: discount voucher how to set up using php

Posted: Tue Mar 26, 2013 1:30 pm
by requinix
You've described it pretty well: add a textbox for the code, check it when the form is submitted, error if it's bad, proceed to checkout with the discount amount if it's good.

Seems like you need help more with the coding part so... what's your code?

Re: discount voucher how to set up using php

Posted: Wed Mar 27, 2013 5:47 am
by jonnyfortis
thats the thing i am unclear with the code. i have a text box but dont know how to implement any of it. What code shall i post?

Re: discount voucher how to set up using php

Posted: Wed Mar 27, 2013 12:17 pm
by requinix
Whatever you have now, even if it doesn't quite work yet.

Re: discount voucher how to set up using php

Posted: Wed Mar 27, 2013 1:26 pm
by jonnyfortis
this is what i have done but it is completly wrong as i have made the statement an update
this is on the cart page

Code: Select all

if ((isset($_POST["MM_update"])) && ($_POST["MM_update"] == "form2")) {
  $updateSQL = sprintf("UPDATE LOTTIE_voucher SET vouchCode=%s WHERE vouchID=%s",
                       GetSQLValueString($_POST['vouchCode'], "text"),
                       GetSQLValueString($_POST['vouchID'], "int"));

Code: Select all

<table align="left">
              <tr>
                <td>Voucher Code:</td>
                <td><input type="text" name="vouchCode" value="<?php echo htmlentities($row_rsVoucher['vouchCode'], ENT_COMPAT, 'utf-8'); ?>" size="32" /></td>
                <td><input type="submit" value="update" /></td>
              </tr>
            </table>
however i havent carried anything over to the checkout but this is what makes up the grand total so far

// *** Compute Grand Total

Code: Select all

$XC_GranTotal = $XCart_sumTotal+$XC_SalesTax+$XC_Shipping-$XC_Discount;
then this is echoed out

Code: Select all

<?php echo DoFormatCurrency($XC_GranTotal, 2, ',', '.', '£ ', ''); ?>

Re: discount voucher how to set up using php

Posted: Wed Mar 27, 2013 2:37 pm
by requinix
The cart information is being passed along somehow, right? All the stuff being ordered and maybe totals and tax and whatever? Add into that the discount - either the code or the actual discount amount. Then every page along the line that gets cart information also gets the discount (and uses it however the page wants). Eventually the billing happens and whatever it does to figure the total also subtracts out the discount.

So what's the code for those pieces? For where the cart information is being saved, and the pages that show what's in the cart and the totals, and for what does billing?

Re: discount voucher how to set up using php

Posted: Thu Mar 28, 2013 6:08 am
by jonnyfortis
The cart information is being passed along somehow, right?
yes

this is on the product detail , shopping cart and checkoput pages

Code: Select all

// *** X Shopping Cart ***
$useSessions = true;
$XCName = "LottieCart";
$XCTimeout = 30;
$XC_ColNames=array("ProductID","Quantity","Name","Price","Stock","Total");
$XC_ComputedCols=array("","","","","","Price");
require_once('XCInc/XCart.inc');
So what's the code for those pieces? For where the cart information is being saved, and the pages that show what's in the cart and the totals, and for what does billing?

Code: Select all

// *** Save XCart contents to table ***
require_once('XCInc/XCsaveAction.inc');
if (isset($_GET['XC_SaveCartToTable']) && ($_GET['XC_SaveCartToTable'] == "1")) {
  $XC_destColName = array("ProductID","Quantity","","","","Total");
  $XC_destColType = array("str","str","str","num","num","num");
  $XC_orderId = $_SESSION['OrderID'];
  $XC_tableName = "LOTTIE_orderdetails";
  $XC_OrderIDCol = "OrderID";
  $XC_OrderIDType = "num";
  $XC_AddToTableRedirect = "../HostedSample/Process.php?$x_reqstr";
  $XC_conName = "lotties";
  require_once('XCInc/SaveXCartToTable.inc');
}

function DoFormatCurrency($num,$dec,$sdec,$sgrp,$sym,$cnt) {
  setlocale(LC_MONETARY, $cnt);
  if ($sdec == "C") {
    $locale_info = localeconv();
    $sdec = $locale_info["mon_decimal_point"];
    $sgrp = $sgrp!="" ? $locale_info["mon_thousands_sep"] : "";
    $sym = $cnt!="" ? $locale_info["currency_symbol"] : $sym;
  }
  $thenum = $sym.number_format($num,$dec,$sdec,$sgrp);
  return $thenum;
}

Re: discount voucher how to set up using php

Posted: Tue Apr 02, 2013 1:27 pm
by jonnyfortis
can anybody help me with the information i have shown?

thanks

Re: discount voucher how to set up using php

Posted: Wed May 08, 2013 6:21 am
by jonnyfortis
I now have the following code but this doesnt seem to be returning any of the desired results

on the shopping cart i have the following

Code: Select all

mysql_select_db($database_lotties, $lotties);
$query_rsVoucher = "SELECT * FROM LOTTIE_vouchercode";
$rsVoucher = mysql_query($query_rsVoucher, $lotties) or die(mysql_error());
$row_rsVoucher = mysql_fetch_assoc($rsVoucher);
$totalRows_rsVoucher = mysql_num_rows($rsVoucher);
 
 
// voucher code
if (isset($_POST['vouchCode']) && $_POST['vouchCode'] == $row_rsVoucher['VCode']) {
  $mycode = $row_rsVoucher['VCode'];
  $spos = strpos($mycode, "%");
  if ($spos !== false) {
    $myvalue = substr($mycode, $spos+1);
    $myvalue = $XCart_sumTotal * $myvalue / 100;
  } else {
    $spos = strpos($mycode, "£");
    if ($spos !== false) {
      $myvalue = substr($mycode, $spos+1);
    }
  }
  $myTotal = $XCart_sumTotal - $myvalue;
  $_SESSION['vouchCode'] = $myvalue;
} else unset($_SESSION['vouchCode']);
then the form is

Code: Select all

<td><input type="text" name="vouchCode" value="<?php echo @$_POST['vouchCode']; ?>" size="32" /></td>
                <td><input type="submit" value="update" /></td>
once the form has been submitted the echo is shown

Code: Select all

<?php echo DoFormatCurrency($myvalue, 2, ',', '.', '£ ', ''); ?>
if this working this should be then sent to the checkout....

Re: discount voucher how to set up using php

Posted: Wed May 08, 2013 12:20 pm
by requinix
So is the problem that none of the codes work?

Re: discount voucher how to set up using php

Posted: Wed May 08, 2013 1:28 pm
by jonnyfortis
So is the problem that none of the codes work?
it appears so. When a code that i have stored in the database is inputted it doesnt update

Re: discount voucher how to set up using php

Posted: Wed May 08, 2013 3:38 pm
by requinix
Does it work with the very first code in the table? Because with

Code: Select all

$query_rsVoucher = "SELECT * FROM LOTTIE_vouchercode";
you're getting everything but only retrieving one single row. That query should be searching for only the row with the matching code.

Re: discount voucher how to set up using php

Posted: Wed May 08, 2013 3:40 pm
by Christopher
I am a little confused about this code. This section seems to select all the codes in the vouchercode table and then fetch the first record in the recordset.

Code: Select all

mysql_select_db($database_lotties, $lotties);
$query_rsVoucher = "SELECT * FROM LOTTIE_vouchercode";
$rsVoucher = mysql_query($query_rsVoucher, $lotties) or die(mysql_error());
$row_rsVoucher = mysql_fetch_assoc($rsVoucher);
$totalRows_rsVoucher = mysql_num_rows($rsVoucher);

Then you do a bunch of checks to see if it is percent or amount discount. Seems like it would be better to have another field in the vouchercode table that specifies the type of discount. Or maybe make discounts < 1.0 be percent and discounts > 1.0 be amount discounts.

Code: Select all

// voucher code
if (isset($_POST['vouchCode']) && $_POST['vouchCode'] == $row_rsVoucher['VCode']) {
  $mycode = $row_rsVoucher['VCode'];
  $spos = strpos($mycode, "%");
  if ($spos !== false) {
    $myvalue = substr($mycode, $spos+1);
    $myvalue = $XCart_sumTotal * $myvalue / 100;
  } else {
    $spos = strpos($mycode, "£");
    if ($spos !== false) {
      $myvalue = substr($mycode, $spos+1);
    }
  }
  $myTotal = $XCart_sumTotal - $myvalue;
  $_SESSION['vouchCode'] = $myvalue;
} else unset($_SESSION['vouchCode']);
It is not clear how this can work.