Jump to content

discount code


grucker

Recommended Posts

You'll need a new mysql table to keep the codes in, as well as their associated values (such as, for example, -$3.00, -10% ...etc)

 

You'll need to randomly generate a code ID (5 - 10 chars) and insert it into the db along with it's value.

 

Then it's pretty much simple math.

 

Allow the user a place to input the code, search the mysql database for that code, figure out what variable is used for their current total (before postage as you said) and then call the shopping carts checkout page again, using the new value as the subtotal.

 

You can even do fun things like if you want it to be a one time use only coupon, simply add another field such as "status" and update that to equal 1 if they use it, rather than 0 if it hasn't been used yet.

Link to comment
https://forums.phpfreaks.com/topic/197897-discount-code/#findComment-1038495
Share on other sites

Include the field in the form, then while validating the rest of the form, check if the discount code field has a value, and if it does, validate the value. If the value is valid, apply the discount and calculate the new subtotal, otherwise, if the value is invalid, handle it like any other validation error. This is in no way exact, but should give you an idea of the logic you'll need to use.

 

if( isset($_POST['discount_code']) ) {
     // query the database, or check against an array of valid codes and get the discount percentage
     if( /*code_is_valid */ ) {
          $new_subtotal = // calculate new subtotal
     } else {
     // set an error, as code is not valid
}

Link to comment
https://forums.phpfreaks.com/topic/197897-discount-code/#findComment-1038496
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.