Skip to main content
Inspiring
March 6, 2013
Question

discount voucher how to set up using php

  • March 6, 2013
  • 1 reply
  • 3680 views

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

This topic has been closed for replies.

1 reply

Rob Hecker2
Legend
March 6, 2013

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.

Inspiring
March 8, 2013

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

Inspiring
March 26, 2013

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!)


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

thanks for your help so far.