Jump to content

Vouchar System


smallc28

Recommended Posts

Hello There  :)

 

I've created an Voucher In MySQL/Database system. I would like to know how would I go about programming it into my shopping cart so when a client has an secret Voucher / Coupon they can have a percentage off of the total price of the items their trying to purchase then place thats final price into paypal checkout. shopping cart and database of the Voucher shown below thanks in advance.

 

<?php 
require "connect_query.php";
 
$voucher = "CREATE TABLE `voucher_code` (
`id` INT( 11 ) NOT NULL AUTO_INCREMENT ,
`vouchercode` VARCHAR( 16 ) NOT NULL ,
`active` TINYINT( 1 ) NOT NULL ,
`min_basket_cost` FLOAT NOT NULL ,
`voucher_operation` ENUM( '-', '%', 's' ) NOT NULL ,
`voucher_amount` FLOAT NOT NULL ,
`num_vouchers` INT( 11 ) NOT NULL DEFAULT '-1',
`expiry` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ,
PRIMARY KEY ( `ID` )";
if (mysqli_query($db_conx,$voucher)){
echo "Vouchar Table has been CREATED :)";
}else{
echo "Vouchar TABLE was not a success :(";
}
?>
 

 

////////////////////////////////////// Cart.php

<?php
// Start session first thing in script 
session_start(); 
// Script Error Reporting
error_reporting(E_ALL);
ini_set('display_errors', '1');
// Connect to the MySQL database  
 include "connect_prompt/connect_query.php";  
?>
<?php 
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//       Section 1 (if user attempts to add something to the cart from the product page)
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
if (isset($_POST['pid'])) {
    $pid = $_POST['pid'];
$size = $_POST['size'];
$wasFound = false;
$i = 0;
 
// If the cart session variable is not set or cart array is empty
if (!isset($_SESSION["cart_array"]) || count($_SESSION["cart_array"]) < 1) { 
   // RUN IF THE CART IS EMPTY OR NOT SET
$_SESSION["cart_array"] = array(1 => array("item_id" => $pid, "size" => $size, "quantity" => 1));
       } else {
// RUN IF THE CART HAS AT LEAST ONE ITEM IN IT
foreach ($_SESSION["cart_array"] as $each_item) { 
     $i++;
     while (list($key, $value) = each($each_item)) {   
    if ($key == "item_id" && $value == $pid){
    if ($each_item['size'] == $size){
        // That item is in cart already so let's adjust its quantity using array_splice()
array_splice($_SESSION["cart_array"], $i-1, 1, array(array("item_id" => $pid, "size" => $size, "quantity" => $each_item['quantity'] + 1)));
 $wasFound = true;
 }} // close if condition
     } // close while loop
      } // close foreach loop
  if ($wasFound == false) {
  array_push($_SESSION["cart_array"], array("item_id" => $pid,"size" => $size, "quantity" => 1));
  }
}
header("location: cart.php"); 
    exit();
}
 
?>
 
<?php
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//       Section 2 (if user chooses to empty their shopping cart)
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
if (isset($_GET['cmd']) && $_GET['cmd'] == "emptycart") {
    unset($_SESSION["cart_array"]);
}
?>
<?php 
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//       Section 3 (if user chooses to adjust item quantity)
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
if (isset($_POST['item_to_adjust']) && $_POST['item_to_adjust'] != "") {
    // execute some code
$item_to_adjust = $_POST['item_to_adjust'];
$size = $_POST['size'];
 
$quantity = $_POST['quantity'];
$quantity = preg_replace('#[^0-9]#i', '', $quantity); // filter everything but numbers
if ($quantity >= 100) { $quantity = 99; }
if ($quantity < 1) { $quantity = 1; }
if ($quantity == "") { $quantity = 1; }
$i = 0;
foreach ($_SESSION["cart_array"] as $each_item) { 
     $i++;
     while (list($key, $value) = each($each_item)) {
 if ($key == "item_id" && $value == $item_to_adjust) {
 if ($each_item['size'] == $size ){
 // That item is in cart already so let's adjust its quantity using array_splice()
 array_splice($_SESSION["cart_array"], $i-1, 1, array(array("item_id" => $item_to_adjust, "quantity" => $quantity, "size" => $size)));
 } // close if condition
     } // close while loop
} // close foreach loop
}
}
?>
 
<?php 
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//       Section 4 (if user wants to remove an item from cart)
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
if (isset($_POST['index_to_remove']) && $_POST['index_to_remove'] != "") {
    // Access the array and run code to remove that array index
  $key_to_remove = $_POST['index_to_remove'];
if (count($_SESSION["cart_array"]) <= 1) {
unset($_SESSION["cart_array"]);
} else {
unset($_SESSION["cart_array"]["$key_to_remove"]);
sort($_SESSION["cart_array"]);
}
}
?>
<?php 
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//       Section 5  (render the cart for the user to view on the page)
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
$cartOutput = "";
$cartTotal = "";
$pp_checkout_btn = '';
$product_id_array = '';
if (!isset($_SESSION["cart_array"]) || count($_SESSION["cart_array"]) < 1) {
    $cartOutput = "<h2 align='center'>Your shopping cart is empty</h2>";
} else {
// Start PayPal Checkout Button
$pp_checkout_btn .= '<form action="[url=https://www.paypal.com/cgi-bin/webscr]https://www.paypal.com/cgi-bin/webscr[/url]" method="post">
    <input type="hidden" name="cmd" value="_cart">
    <input type="hidden" name="upload" value="1">
    <input type="hidden" name="business" value="myemail@gmail.com">';
// Start the For Each loop
$i = 0; 
    foreach ($_SESSION["cart_array"] as $each_item) { 
$item_id = $each_item['item_id']; 
$sql = "SELECT * FROM products WHERE id='$item_id' LIMIT 1";
$query = mysqli_query ($db_conx,$sql);
while ($row = mysqli_fetch_array($query)){
$product_name = $row["product_name"];
$price = $row["price"];
$details = $row["details"];
$taxes = $row["taxes"];
$shipping_one =$row['shipping_one'];
}
 
$pricetotal = $price  * $each_item['quantity'];
$cartTotal = $pricetotal  + $shipping_one + $taxes + $cartTotal;
 
setlocale(LC_MONETARY, "en_US");
        //$pricetotal = money_format("%10.2n", $pricetotal);
// Dynamic Checkout Btn Assembly
$x = $i + 1;
$pp_checkout_btn .= '
<input type="hidden" name="item_name_' . $x . '" value="' . $product_name . '">
        <input type="hidden" name="amount_' . $x . '" value="' . $price . '">
        <input type="hidden" name="quantity_' . $x . '" value="' . $each_item['quantity'] . '">
        <input type="hidden" name="handling_'. $x . '"value="'. $shipping_one .'"> 
        <input type="hidden" name="tax_'. $x . '"value="'. $taxes .'"> 
   <input type="hidden" name="on0_' . $x . '" value="' . $each_item['size'] . '">'; 
// Create the product array variable
$product_id_array .= "$item_id-".$each_item['quantity'].","; 
// Dynamic table row assembly
$cartOutput .= "<tr>";
$cartOutput .='<td><a href="product.php?id=' . $item_id . '">' . $product_name . '</a><br /><img src="inventory_images/' . $item_id . '.jpg" alt="' . $product_name. '" width="40" height="52" border="1" /></td>';
 
 
        $cartOutput .= '<td>' . $each_item['size'] .'</td>';
        $cartOutput .= '<td>' . $details . '</td>';
$cartOutput .= '<td>$' . $price . '</td>';
$cartOutput .= '<td><form action="cart.php" method="post">
<input name="quantity" type="text" value="' . $each_item['quantity'] . '" size="1" maxlength="2" />
<input name="adjustBtn' . $item_id . '" type="submit" value="change" />
<input name="item_to_adjust" type="hidden" value="' . $item_id . '" />
</form></td>';
//$cartOutput .= '<td>' . $each_item['quantity'] . '</td>';
$cartOutput .= '<td>' . $pricetotal . '</td>';
$cartOutput .= '<td><form action="cart.php" method="post"><input name="deleteBtn' . $item_id . '" type="submit" value="X" /><input name="index_to_remove" type="hidden" value="' . $i . '" /></form></td>';
$cartOutput .= '</tr>';
$i++; 
    } 
setlocale(LC_MONETARY, "en_US");
   // $cartTotal = money_format("%10.2n", $cartTotal);
$cartTotal = "<div style='font-size:18px; margin-top:12px;' align='right'>Cart Total : ".$cartTotal." USD</div>";
    // Finish the Paypal Checkout Btn
$pp_checkout_btn .= '<input type="hidden" name="custom" value="' . $product_id_array . '">
<input type="hidden" name="notify_url" value="[url=https://www.shopstorms.com/storescripts/my_ipn.php]https://www.shopstorms.com/storescripts/my_ipn.php">[/url]
<input type="hidden" name="return" value="[url=https://www.mywebsite.com/checkout_complete.php]https://www.mywebsite.com/checkout_complete.php">[/url]
<input type="hidden" name="rm" value="2">
<input type="hidden" name="cbt" value="Return to The Store">
<input type="hidden" name="cancel_return" value="[url=https://www.mywebsite.com/paypal_cancel.php]https://www.mywebsite.com/paypal_cancel.php">[/url]
<input type="hidden" name="lc" value="US">
<input type="hidden" name="currency_code" value="USD">
<input type="image" src="[url=http://www.paypal.com/en_US/i/btn/x-click-but01.gif]http://www.paypal.com/en_US/i/btn/x-click-but01.gif[/url]" name="submit" alt="Make payments with PayPal - its fast, free and secure!">
</form>';
}
?>
 
<?php 
if(!empty($pricetotal)) { ?>
<?php
        $discount ="";
 
if($pricetotal >= 50){
echo "FREE SHIPPING!";
}
elseif ($pricetotal <= 50){
echo "SPEND MORE THAN 50 GET FREE SHIPPING!";
}
 
?>
<?php } ?>
<body>
<div align="center" id="mainWrapper">
  <div id="pageContent">
    <div style="margin:24px; text-align:left;">
 
    <br />
    <table width="1024" border="1" cellspacing="0" cellpadding="6" align="center" style="background-color:white;">
      <tr>
        <td width="18%" bgcolor="#C5DFFA"><strong>Product</strong></td>
        <td align="center" width="10%" bgcolor="#C5DFFA"><strong>Size</strong></td>
        <td width="45%" bgcolor="#C5DFFA"><strong>Product Description</strong></td>
        <td width="10%" bgcolor="#C5DFFA"><strong>Unit Price</strong></td>
        <td width="9%" bgcolor="#C5DFFA"><strong>Quantity</strong></td>
        <td width="9%" bgcolor="#C5DFFA"><strong>Total</strong></td>
        <td width="9%" bgcolor="#C5DFFA"><strong>Remove</strong></td>
      </tr>
     <?php echo $cartOutput; ?>
     <!-- <tr>
        <td> </td>
        <td> </td>
        <td> </td>
        <td> </td>
        <td> </td>
        <td> </td>
      </tr> -->
    </table>
    <table width="1024" border="0" cellspacing="0" cellpadding="0" align="center">
  <tr>
    <th scope="col" align="left"><a href ="cart.php?cmd=emptycart">Click Here to Empty Your Shopping Cart</a></th>
    <th scope="col" align="center" style="color:black;">
    <?php if(!empty ($taxes)) { ?>
     <?php echo $taxes; ?>
         <?php } ?>
   <br/>
    <?php if(!empty ($shipping_one)) { ?> 
     <?php echo $shipping_one; ?>
         <?php } ?>
   <br/>
    <?php if(!empty ($cartTotal)) { ?> 
     <?php echo $cartTotal; ?>
         <?php } ?>
    <br/><?php if(!empty ($discount)) { ?>
         <?php echo $discount; ?>
             <?php } ?>
    <br/> 
    </th>
  </tr>
</table>
    <table width="1024" height="50" border="0" cellspacing="10" cellpadding="0" align="center">
  <tr>
    <th width="913" align="left" scope="col"></th>
    <th width="105" align="right" scope="col"><?php echo $pp_checkout_btn; ?> </th>
  </tr>
</table>
    <br />
<br />
 
    <br />
    <br />
    
    </div>
   <br />
  </div>
</div>
</body>
</html>
Edited by requinix
please use [code] tags when posting code
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.