Jump to content

Add coupon code script to shopping cart


hashstar

Recommended Posts

Hi,

 

 

I would like to add a coupon code option to my shopping cart. How should i go about this?

 

Here is the code that i use for my cart:

 


<?php

session_start();

require_once('MCAPI.class.php');

$api = new MCAPI('39b576be70ddb86cf0fa83ff80fca85c-us5');

$list_id = "5f2d41d5db";

$api->listSubscribe($list_id, $_POST['email'], '');


include('include/includes.php');
$cart  = Cart::CreateInstance();



if(isset($_GET)){
foreach($_GET as $k=>$v){
$smarty->assign($k,$v);
}
}
if(isset($_GET['remove'])){
$cart->RemoveItems($_GET['remove'],1);
}
if(isset($_GET['removeAll'])){
$cart->RemoveItems($_GET['removeAll'],$_GET['counts']); 
}

if(isset($_GET['emptyCart'])){ 
if($_GET['emptyCart']){
$cart->EmptyCart();
header("Location: cart.html");
exit(0);
}}

if(isset($_GET['add'])){

$itemId = $_GET['add'];
$size = $_GET['size'];

if (isset($_GET['qty'])) {
$qty = $_GET['qty'];
} else {
$qty = 1;
}

$cart->addItems($itemId.'-'.$size, array($size,$itemId), $qty);
}
$cart  = Cart::CreateInstance();
$cartBox = $cart->GetCart();
//print_r($cartBox);
$total = 0;
$subTotal = array();
$db = new DB();
$prods = $db->select("SELECT friendly,name,id FROM products");
$smarty->assign('prods',$prods);
if(is_array($cartBox)){
foreach($cartBox as $key=>$item){ 
$id = $cartBox[$key]['data']->_itemData[1];
$products = $db->select("SELECT detail,image,friendly,name FROM products WHERE id='$id'");
$detailjson = json_decode($products[0]['detail']);

foreach($detailjson as $detail){
list($k, $v) = split_by_colon($detail);
if($k == $cartBox[$key]['data']->_itemData[0]){ $price = $v;}
}
$cartBox[$key]['data']->_itemData[3] = $products[0]['image'];
$cartBox[$key]['data']->_itemData[6] = $products[0]['name'];
$cartBox[$key]['data']->_itemData[2] = $products[0]['friendly'];
$cartBox[$key]['data']->_itemData[5] = $price;
$sub = (int)$item['count'] * $price; 
$cartBox[$key]['data']->_itemData[4] = $sub;
$total += $sub; 
}
} else {
$cartBox = array();
}

$count = $cart->GetItemsCount();

$shipping = (int)0;
if($total>0&&$total<5){ $shipping = 2.99;
}elseif($total>5&&$total<10){ $shipping = 3.99;
}elseif($total>10&&$total<15){ $shipping = 4.99;
}elseif($total>15&&$total<20){ $shipping = 5.99;
}elseif($total>20&&$total<25){ $shipping = 6.99;
}elseif($total>25&&$total<30){ $shipping = 7.99;
}elseif($total>30&&$total<40){ $shipping = 8.99;
}elseif($total>40&&$total<50){ $shipping = 9.99;
}elseif($total>50&&$total<55){ $shipping = 10.99;
}elseif($total>55&&$total<60){ $shipping = 11.99; 
}elseif($total>60&&$total<65){ $shipping = 12.99;
}elseif($total>65&&$total<70){ $shipping = 13.99;
}elseif($total>70&&$total<75){ $shipping = 14.99;
}elseif($total>75&&$total<90){ $shipping = 15.99;
}elseif($total>90&&$total<100){ $shipping = 16.49;
}elseif($total>100&&$total<110){ $shipping = 16.99;
}elseif($total>110&&$total<120){ $shipping = 17.99;
}elseif($total>120&&$total<130){ $shipping = 18.99;
}elseif($total>130&&$total<140){ $shipping = 19.99;
}elseif($total>140&&$total<150){ $shipping = 20.99;
}elseif($total>150&&$total<160){ $shipping = 21.99;
}elseif($total>160&&$total<170){ $shipping = 22.99;
}elseif($total>170&&$total<180){ $shipping = 23.99;
}elseif($total>180){ $shipping = 24.99;
}

/*
0 - 4.99 = 2.99
5 - 9.99 = 3.99
10 - 14.99 = 4.99
15 - 19.99 = 5.99
20 - 29.99 = 6.99
30 - 39.99 = 7.99
40 - 49.99 = 8.99
50 - 69.99 = 9.99
70 - 99.99 = 10.99
100+ = 12.99

*/

if(isset($_GET['eushipping'])||(isset($_POST['rec_country'])&&$_POST['rec_country']!='uk')){

if($_POST['rec_country']=='au' || $_GET['country']=='au'){
//Set shipping for Austria
$shipping = ceil($total/60.00)*30.00;
}elseif($_POST['rec_country']=='be' || $_GET['country']=='be'){
//Set shipping for Belgium
$shipping = ceil($total/60.00)*20.00;
}elseif($_POST['rec_country']=='bu' || $_GET['country']=='bu'){
//Set shipping for Bulgaria
$shipping = ceil($total/60.00)*50.00;
}elseif($_POST['rec_country']=='cy' || $_GET['country']=='cy'){
//Set shipping for Cyprus
$shipping = ceil($total/60.00)*108.00;
}elseif($_POST['rec_country']=='cz' || $_GET['country']=='cz'){
//Set shipping for Czech Republic
$shipping = ceil($total/60.00)*33.00;
}elseif($_POST['rec_country']=='de' || $_GET['country']=='de'){
//Set shipping for Denmark
$shipping = ceil($total/60.00)*30.00;
}elseif($_POST['rec_country']=='es' || $_GET['country']=='es'){
//Set shipping for Estonia
$shipping = ceil($total/60.00)*45.00;
}elseif($_POST['rec_country']=='fi' || $_GET['country']=='fi'){
//Set shipping for Finalnd
$shipping = ceil($total/60.00)*30.00;
}elseif($_POST['rec_country']=='fr' || $_GET['country']=='fr'){
//Set shipping for France
$shipping = ceil($total/60.00)*28.00;
}elseif($_POST['rec_country']=='ge' || $_GET['country']=='ge'){
//Set shipping for Germany
$shipping = ceil($total/60.00)*25.00;
}elseif($_POST['rec_country']=='gr' || $_GET['country']=='gr'){
//Set shipping for Greece
$shipping = ceil($total/60.00)*50.00;
}elseif($_POST['rec_country']=='hu' || $_GET['country']=='hu'){
//Set shipping for Hungary
$shipping = ceil($total/60.00)*25.00;
}elseif($_POST['rec_country']=='ir' || $_GET['country']=='ir'){
//Set shipping for Ireland
$shipping = ceil($total/60.00)*25.00;
}elseif($_POST['rec_country']=='it' || $_GET['country']=='it'){
//Set shipping for Italy
$shipping = ceil($total/60.00)*30.00;
}elseif($_POST['rec_country']=='la' || $_GET['country']=='la'){
//Set shipping for Latvia
$shipping = ceil($total/60.00)*42.00;
}elseif($_POST['rec_country']=='li' || $_GET['country']=='li'){
//Set shipping for Lithuania
$shipping = ceil($total/60.00)*35.00;
}elseif($_POST['rec_country']=='lu' || $_GET['country']=='lu'){
//Set shipping for Luxembourg
$shipping = ceil($total/60.00)*20.00;
}elseif($_POST['rec_country']=='ma' || $_GET['country']=='ma'){
//Set shipping for Malta
$shipping = ceil($total/60.00)*108.00;
}elseif($_POST['rec_country']=='ne' || $_GET['country']=='ne'){
//Set shipping for Netherlands
$shipping = ceil($total/60.00)*25.00;
}elseif($_POST['rec_country']=='no' || $_GET['country']=='no'){
//Set shipping for Norway
$shipping = ceil($total/60.00)*37.00;
}elseif($_POST['rec_country']=='pol' || $_GET['country']=='pol'){
//Set shipping for Poland
$shipping = ceil($total/60.00)*35.00;
}elseif($_POST['rec_country']=='por' || $_GET['country']=='por'){
//Set shipping for Portugal
$shipping = ceil($total/60.00)*30.00;
}elseif($_POST['rec_country']=='ro' || $_GET['country']=='ro'){
//Set shipping for Romania
$shipping = ceil($total/60.00)*48.00;
}elseif($_POST['rec_country']=='slv' || $_GET['country']=='slv'){
//Set shipping for Slovakia
$shipping = ceil($total/60.00)*32.00;
}elseif($_POST['rec_country']=='slk' || $_GET['country']=='slk'){
//Set shipping for Slovenia
$shipping = ceil($total/60.00)*29.00;
}elseif($_POST['rec_country']=='sp' || $_GET['country']=='sp'){
//Set shipping for Spain
$shipping = ceil($total/60.00)*36.00;
}elseif($_POST['rec_country']=='swe' || $_GET['country']=='swe'){
//Set shipping for Sweden
$shipping = ceil($total/60.00)*32.00;
}elseif($_POST['rec_country']=='swi' || $_GET['country']=='swi'){
//Set shipping for Switzerland
$shipping = ceil($total/60.00)*55.00;
}elseif($_POST['rec_country']=='ukh' || $_GET['country']=='ukh'){
//Set shipping for UK - Highlnads and Islands
$shipping = ceil($total/60.00)*18.00;
}elseif($_POST['rec_country']=='ukn' || $_GET['country']=='ukn'){
//Set shipping for UK - Northern Island
$shipping = ceil($total/60.00)*18.00;
}elseif($_POST['rec_country']=='jng' || $_GET['country']=='jng'){
//Set shipping for Jersey and Guernsey
$shipping = ceil($total/60.00)*34.00;
}
}



/*

*/ 
if(isset($_GET['count'])){
echo (int)$count;
exit;
}

if(isset($_GET['order'])){
if($_GET['order']){

}}

if(isset($_GET['ajax'])){
$noAjax = $_GET['ajax'];
}else{
$noAjax = false;
}

$smarty->assign('noAjax',$noAjax);


if(isset($_GET['t'])){
$t=$_GET['t'].'.tpl';
} else {
$t = 'cart.tpl';
}

if(isset($_POST['cmd'])) {
$_SESSION['paypal'] = $_POST;
//print_r($_POST);
}
if(isset($_POST['rec_houseA1'])){

$filename = time().'-'.rand(1,499);
$paypal = $_SESSION['paypal'];
unset($paypal['checkout_x']);
unset($paypal['checkout_y']);

$_SESSION['addresses'] = $_POST;


$File = 'sales/'.$filename.'.php'; 
$Handle = fopen($File, 'x');
fwrite($Handle,'<? 
');
fwrite($Handle,'$cart = array(');

foreach($cartBox as $k=>$v){

fwrite($Handle,'"'.$k.'"=>array(');
foreach($v as $key=>$value){

if(is_object($value)){
fwrite($Handle,'"'.$key.'"=>(object)array(');

foreach($value as $keys=>$values){
if(is_array($values)){
fwrite($Handle,'"'.$keys.'"=>array(');
foreach($values as $keyd=>$valued){
fwrite($Handle,  '"'.$keyd.'"=>"'.$valued.'",');
}
fwrite($Handle,'""=>""),');

}else{
fwrite($Handle,  '"'.$keys.'"=>"'.$values.'",'); }
} fwrite($Handle,'""=>""),'); 
}else{fwrite($Handle,  '"'.$key.'"=>"'.$value.'",');} 
}
fwrite($Handle,'""=>""),');

}
//$total += $_POST['shipping']; 
fwrite($Handle,'"total"=>"'.$total.'");');
 fwrite($Handle,' 

'); 
fwrite($Handle,'$detail = array(');
foreach($_POST as $k=>$v){
if(is_array($v)){
fwrite($Handle,'array(');
foreach($v as $key=>$value){

if(is_array($value)){
fwrite($Handle,'array(');
foreach($value as $keys=>$values){
fwrite($Handle,  '"'.$keys.'"=>"'.$values.'",'); 
}
fwrite($Handle,')');
}else{fwrite($Handle,  '"'.$key.'"=>"'.$value.'",');} 
}
fwrite($Handle,')');
}else{
fwrite($Handle,  '"'.$k.'"=>"'.$v.'",');
}
}
fwrite($Handle,'""=>"");');
 fwrite($Handle,' 

'); 
fwrite($Handle,'$paypal = array(');
foreach($paypal as $k=>$v){
if(is_array($v)){
fwrite($Handle,'array(');
foreach($v as $key=>$value){

if(is_array($value)){
fwrite($Handle,'array(');
foreach($value as $keys=>$values){
fwrite($Handle,  '"'.$keys.'"=>"'.$values.'",'); 
}
fwrite($Handle,')');
}else{fwrite($Handle,  '"'.$key.'"=>"'.$value.'",');} 
}
fwrite($Handle,')');
}else{
fwrite($Handle,  '"'.$k.'"=>"'.$v.'",');
}
}
fwrite($Handle,'""=>"");');

fwrite($Handle,' 

'); 

fwrite($Handle,'$shipping = "'.$shipping.'";
');
fwrite($Handle,'$subtotal = "'.$total.'";
');
$totals = $total+$shipping;
fwrite($Handle,'$totals = "'.$totals.'";');
fclose($Handle);


$smarty->assign('filename',$filename);
$smarty->assign('shipping',$shipping);
$smarty->assign('paypal',$paypal);
//print_r($paypal);
$smarty->display('paypal.tpl');
}else{
$smarty->assign('total',$total+$shipping);
$smarty->assign('itemsInBox',$count);
$smarty->assign('cartBox',$cartBox);
$smarty->assign('shipping',$shipping);
$style = $db->select("SELECT * FROM styles WHERE `name`='default'"); 
$smarty->assign('style',$style);
$smarty->display($t);
}
?>
<?php

// form processing code, test if a form submitted to this page
if(strtolower($_SERVER['REQUEST_METHOD']) == 'post'){
// was a form, process the form data

$errors = array(); // use an array to hold validation errors

// filter/validate the expected data
$email = trim($_POST['email']); // trim any starting/ending white-space
if($email == ''){
$errors[] = "Email cannot be empty.";
} else {
// test if value has the correct format
if(function_exists('filter_var')){ // php5.2 or better
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) { 
$errors[] = "Email address format is not valid.";
}
} else {
// alternate code to validate email format would go here (php < 5.2) ...

}
}

// other validation tests as needed by your application ...

// if no validation errors, use the form data here...
if(empty($errors)){
$con = mysql_connect("organicgrowshopcouk.fatcowmysql.com","worm","*aldoushuxley2012*");
if (!$con){
die('Could not connect: ' . mysql_error());
}
mysql_select_db("ogs_mailinglist1", $con);

$sql=sprintf("INSERT INTO mailinglist (email) VALUES ('%s')",
mysql_real_escape_string($email));

if (!mysql_query($sql,$con)){
die('Error: ' . mysql_error());
}

mysql_close($con);
}
} else {
// not a form submission, handle that state here...

// what you do here depends on if this page only handles the form submission or if the form itself is on this same page...
}

// display any errors
if(!empty($errors)){
echo "Please correct the following errors:<br />";
foreach($errors as $error){
echo "$error<br />";
}
}

// if the form is on this same page, you would display/redisplay it here, otherwise output a link back to the form page or store any errors and submitted data in session variables and redirect back to the form page .
?>

 

 

Any help is much appreciated!

Link to comment
https://forums.phpfreaks.com/topic/273876-add-coupon-code-script-to-shopping-cart/
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.