Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
0

discount voucher how to set up using php

Engaged ,
Mar 06, 2013 Mar 06, 2013

Copy link to clipboard

Copied

what is the best way to setup a discount voucher so when the user inputs a set number provided by us it will give a % discount. and this is pllied to the the total of the shopping cart

i know my question is vague but i dont know where to start

thanks

TOPICS
Server side applications

Views

3.5K
Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guru ,
Mar 06, 2013 Mar 06, 2013

Copy link to clipboard

Copied

For this kind of feature I use a table with the following fields:

promo_code (the promo or voucher text/number the customer will enter)

promo_deduction (the percentage of the deduction)

promo_expiration (a date you want the offer to end)

promo_id (auto increment primary key)

feature_id (if you want the offer to apply to only a specific product or service, this prevents it being applied to any product/service)

Then, if PHP is performing the calculation, in your POST data you would first check to see if they entered a voucher code, then compare it to your table of valid codes and if there is a match, return the deduction amount as a variable. This is accomplished with a simple WHERE clause in your query  (...WHERE promo_code=:promo_code_entered AND promo_expiration <=:currentdate AND feature_id =:feature_id)

Then multiply the variable by the total, like so:

$total = $pretotal * $promo_deduction;

You could alternatively use Javascript to create a live response, so that when they enter a code in the voucher field, it will update the total or respond with a message that the voucher was not valid.

Votes

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Mar 08, 2013 Mar 08, 2013

Copy link to clipboard

Copied

Thanks for that, i will see if i can use your idea and link it in with my existing cart

Votes

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Mar 08, 2013 Mar 08, 2013

Copy link to clipboard

Copied

you could just hand code this into your shopping cart.

First add a promo code field in your checkout page (called 'offcd' below) than in your PHP calcluation page simply add some discount coding if the code matches, you can add some error messages for miss matched codes etc, and then save the code used in your order table in the database for future pricing reference

Below would give 25% off the cart total for code CD458545

if ($offcd == CD458545)

{

          $i = "25";

          $d = "100";

          $discount = ($carttotal  *$i / $d);

          $newitems = ($carttotal - $discount);

          $carttotal = $newitems;

          $totalpayable = ($carttotal + $deliverytotal );

}

Votes

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Mar 08, 2013 Mar 08, 2013

Copy link to clipboard

Copied

this looks interesting aswell.

just am looking how to include both of these in my checkout page

thanks

Votes

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Mar 11, 2013 Mar 11, 2013

Copy link to clipboard

Copied

i have been trying to figure out how to integrate this into my checout page but am proper stuck

i take it i will need a text field where the user can input the discount code (your example CD458545)

i have a db built for the discounts to come from

vouchID        int(11)




No
auto_increment






vouchCodevarchar(200)latin1_swedish_ci
No









voucherDiscountfloat

this is so the new discounts can be added

for example promotion 1 - 10% discount

or

promotion 2 = 30%

the grand total is computed with all the other variable below

// *** Compute Grand Total

$XC_GranTotal = $XCart_sumTotal+$XC_SalesTax+$XC_Shipping;

so do i just need to create a variable for discount and - that?

Votes

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Mar 25, 2013 Mar 25, 2013

Copy link to clipboard

Copied

can anyone else help me on my discount voucher problem as explained above?

thanks in advance

Votes

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Mar 25, 2013 Mar 25, 2013

Copy link to clipboard

Copied

If you have the database set up with codes and discount already stored the next thing to do is to add the discount form to the cart area, somthing like:

<form id="form1" name="form1" method="post">

      <label for="DiscountCode">Discount Code:</label>

      <input type="text" name="DiscountCode" id="DiscountCode">

      <input name="update" type="hidden" id="update" value="update">

      <input type="submit" name="submit" id="submit" value="Update">

</form>

You'll see i use a hidden field, this is so when you submit this form on the cart page you can use php to start your query only if this form has been submitted

<? if ($update == 'update') {

perform the database query here ...

} ?>

so when this form is used the customer will have entered a promo code, if the text field is blank return an error message, if the promo code is not matched to a database code return an error message, if the code matches a code in the database then you'll return the discount amount and that will be used in your calculations and the $XCart_sumTotal will be reduced by that amount

Votes

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Mar 25, 2013 Mar 25, 2013

Copy link to clipboard

Copied

ok

i have the following now

$editFormAction = $_SERVER['PHP_SELF'];

if (isset($_SERVER['QUERY_STRING'])) {

  $editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);

}

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"));

and the form

          <form action="<?php echo $editFormAction; ?>" method="post" name="form2" id="form2">

<input type="text" name="vouchCode" value="<?php echo htmlentities($row_rsVoucher['vouchCode'], ENT_COMPAT, 'utf-8'); ?>"/>

<input type="submit" value="update" />

<input type="hidden" name="MM_update" value="form2" />

<input type="hidden" name="vouchID" value="<?php echo $row_rsVoucher['vouchID']; ?>" />

          </form>

is this correct so far?

>so when this form is used the customer will have entered a promo code, if the text field is blank return an error message,

i can do this with srpy

> if the promo code is not matched to a database code return an error message

do i echo an error out?

> if the code matches a code in the database then you'll return the discount amount and that will be used in your calculations and the $XCart_sumTotal will be reduced by that amount

then can i echo this out then put the results in the equation you showed in a previous post?

thanks in advance

Votes

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Mar 25, 2013 Mar 25, 2013

Copy link to clipboard

Copied

nope ...

i think you still don't have the concept right in your head

The above code has the customer updating the dtabase with a voucher code, the form does not update your voucher code table.

Your database table holds pre-set info you've entered and doesn't need to be updated so a voucher code held in the database table might be code1234 and discount 20

the form you have made then simply checks the code the customer has entered against those held in the database, it doesn't get stored in the database.

so when the form is submitted to enter a voucher promo code the PHP simply gets all the stored codes first from the database, then it checks if the form feild the user has entered a code in matches any code in the database, if it does it will return the discount amount to a variable to be used in the cart totals calculation

the form doesn't update any database unless in the order you want to record what promo code was used (but thats another issue!)

Votes

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Mar 26, 2013 Mar 26, 2013

Copy link to clipboard

Copied

LATEST

i thought i got the concept wrong i will try again and post my results.

thanks for your help so far.

Votes

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines