Implement coupon discount in the cart shopping with php and mysqli

خرید بک لینک

good day friends.

They can help me implement a system of discount coupon in my shopping cart, fulfilling these functions please.

System discount coupon create several coupons each with a different function.

  • Limited discount coupon date: (expire-date) when does this coupon expire :date/timestamp.
  • Discount coupon single use (discount coupon valid only for a limited number.): The client can use just one time, the coupon discount.
  • Discount coupon only valid for Minimum Total Order: the customer can make use of discount coupon. only if it passes the minimum order of $ 100, $ 500 so on.
  • Unlimited coupon discount: Unlimited fixed discount coupon that the customer can use a lifetime.
  • Kind of discount coupon (two different functions discount coupon).: Discount coupons vary in two sample functions: 1.5% 5% 10% 15% so on. The following function the coupon discount is applied directly to value dollars example: $1.50 $30 $50 $100 so on.

All these functions and values using MySQLi database.

I imagine a database this way but does not fulfill the functions of detailed discounts and coupons. (ou should add more tables to the database to meet the system fucniones discount coupon).

id Coupon discount Code Minimum Order Total expire-date used 1 HT331 1.5% $30 04-03-2016 3 2 HO531 10% $100 false 3 YTW12 $1.50 $40 4 ERT45 $40 $100

  • used - has this coupon been used : boolean

This database is an idea but I think you need to add more tables to the database to meet all system functions discount coupon.

Now comes the hard part as the query and implements functions to my shopping cart for the discount coupon system works

This is the code of my cart shopping.

cart.php

<?php
session_start();
require 'connect.php';

$action = isset($_GET['a']) ? $_GET['a'] : "";
$itemCount = isset($_SESSION['cart']) ? count($_SESSION['cart']) : 0;
if (isset($_SESSION['qty']))
{
    $meQty = 0;
    foreach ($_SESSION['qty'] as $meItem)
    {
        $meQty = $meQty + $meItem;
    }
} else
{
    $meQty = 0;
}
if (isset($_SESSION['cart']) and $itemCount > 0)
{
    $itemIds = "";
    foreach ($_SESSION['cart'] as $itemId)
    {
        $itemIds = $itemIds . $itemId . ",";
    }
    $inputItems = rtrim($itemIds, ",");
    $meSql = "SELECT * FROM products WHERE id in ({$inputItems})";
    $meQuery = mysql_query($meSql);
    $meCount = mysql_num_rows($meQuery);
} else
{
    $meCount = 0;
}
?>
<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <title>Shopping cart</title>

        <!-- Bootstrap -->
        <link href="bootstrap/css/bootstrap.min.css" rel="stylesheet">
        <link href="bootstrap/css/nava.css" rel="stylesheet">
    </head>
    <body>

            <h3>Shopping basket</h3>
            <!-- Main component for a primary marketing message or call to action -->
            <?php
            if ($action == 'removed')
            {
                echo "<div class="alert alert-waing">Products have been removed</div>";
            }

            if ($meCount == 0)
            {
                echo "<div class="alert alert-waing">No items in the basket</div>";
            } else
            {
                ?>
                <form action="updatecart.php" method="post" name="fromupdate">
                    <table class="table table-striped table-bordered">
                        <thead>
                            <tr>
                                <th>#</th>
                                <th>Code</th>
                                <th>Product Name</th>
                                <th>detail</th>
                                <th>Quantity</th>
                                <th>Price by unit</th>
                                <th>Suptotal</th>
                                <th>&nbsp;</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php
                            $total_price = 0;
                            $num = 0;
                            while ($meResult = mysql_fetch_assoc($meQuery))
                            {
                                $key = array_search($meResult['id'], $_SESSION['cart']);
                                $total_price = $total_price + ($meResult['product_price'] * $_SESSION['qty'][$key]);
                                ?>
                                <tr>
                                    <td><img src="images/<?php echo $meResult['product_img_name']; ?>" border="0"></td>
                                    <td><?php echo $meResult['product_code']; ?></td>
                                    <td><?php echo $meResult['product_name']; ?></td>
                                    <td><?php echo $meResult['product_desc']; ?></td>
                                    <td>
                                        <input type="text" name="qty[<?php echo $num; ?>]" value="<?php echo $_SESSION['qty'][$key]; ?>" class="form-control" style="width: 60px;text-align: center;">
                                        <input type="hidden" name="arr_key_<?php echo $num; ?>" value="<?php echo $key; ?>">
                                    </td>
                                    <td><?php echo number_format($meResult['product_price'],2); ?></td>
                                    <td><?php echo number_format(($meResult['product_price'] * $_SESSION['qty'][$key]),2); ?></td>
                                    <td>
                                        <a class="btn btn-danger btn-lg" href="removecart.php?itemId=<?php echo $meResult['id']; ?>" role="button">
                                            <span class="glyphicon glyphicon-trash"></span>
                                            Delete</a>
                                    </td>
                                </tr>
                                <?php
                                $num++;
                            }
                            ?>
                            <tr>
                                <td colspan="8" style="text-align: right;">
                                    <h4>Total <?php echo number_format($total_price,2); ?> $</h4>
                                </td>
                            </tr>
                            <tr>
                                <td colspan="8" style="text-align: right;">
                                    <button type="submit" class="btn btn-info btn-lg">Update</button>
                                    <a href="order.php" type="button" class="btn btn-primary btn-lg">To buy</a>
                                </td>
                            </tr>
                        </tbody>
                    </table>
                </form>
                <?php
            }
            ?>


        <!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
        <script src="bootstrap/js/jquery.min.js"></script>
        <!-- Include all compiled plugins (below), or include individual files as needed -->
        <script src="bootstrap/js/bootstrap.min.js"></script>
    </body>
</html>
<?php
mysql_close();

removecart.php

session_start();

$itemId = isset($_GET['itemId']) ? $_GET['itemId'] : "";

if (!isset($_SESSION['cart']))
{
    $_SESSION['cart'] = array();
    $_SESSION['qty'][] = array();
}

$key = array_search($itemId, $_SESSION['cart']);
$_SESSION['qty'][$key] = "";

$_SESSION['cart'] = array_diff($_SESSION['cart'], array($itemId));
header('location:cart.php?a=remove');

updatecart.php

<?php

session_start();
$itemId = isset($_GET['itemId']) ? $_GET['itemId'] : "";
if ($_POST)
{
    for ($i = 0; $i < count($_POST['qty']); $i++)
    {
        $key = $_POST['arr_key_' . $i];
        $_SESSION['qty'][$key] = $_POST['qty'][$i];
        header('location:cart.php');
    }
} else
{
    if (!isset($_SESSION['cart']))
    {
        $_SESSION['cart'] = array();
        $_SESSION['qty'][] = array();
    }

    if (in_array($itemId, $_SESSION['cart']))
    {
        $key = array_search($itemId, $_SESSION['cart']);
        $_SESSION['qty'][$key] = $_SESSION['qty'][$key] + 1;
        header('location:index.php?a=exists');
    } else
    {
        array_push($_SESSION['cart'], $itemId);
        $key = array_search($itemId, $_SESSION['cart']);
        $_SESSION['qty'][$key] = 1;
        header('location:index.php?a=add');
    }
}

updateorder.php

<?php
session_start();
$formid = isset($_SESSION['formid']) ? $_SESSION['formid'] : "";
if ($formid != $_POST['formid']) {
    echo "E00001!! SESSION ERROR RETRY AGAINT.";
} else {
    unset($_SESSION['formid']);
    if ($_POST) {
        require 'connect.php';
        $order_fullname = mysql_real_escape_string($_POST['order_fullname']);
        $order_address = mysql_real_escape_string($_POST['order_address']);
        $order_phone = mysql_real_escape_string($_POST['order_phone']);

        $meSql = "INSERT INTO orders (order_date, order_fullname, order_address, order_phone) VALUES (NOW(),'{$order_fullname}','{$order_address}','{$order_phone}') ";
        $meQeury = mysql_query($meSql);
        if ($meQeury) {
            $order_id = mysql_insert_id();
            for ($i = 0; $i < count($_POST['qty']); $i++) {
                $order_detail_quantity = mysql_real_escape_string($_POST['qty'][$i]);
                $order_detail_price = mysql_real_escape_string($_POST['product_price'][$i]);
                $product_id = mysql_real_escape_string($_POST['product_id'][$i]);
                $lineSql = "INSERT INTO order_details (order_detail_quantity, order_detail_price, product_id, order_id) ";
                $lineSql .= "VALUES (";
                $lineSql .= "'{$order_detail_quantity}',";
                $lineSql .= "'{$order_detail_price}',";
                $lineSql .= "'{$product_id}',";
                $lineSql .= "'{$order_id}'";
                $lineSql .= ") ";
                mysql_query($lineSql);
            }
            mysql_close();
            unset($_SESSION['cart']);
            unset($_SESSION['qty']);
            header('location:index.php?a=order');
        }else{
            mysql_close();
            header('location:index.php?a=orderfail');
        }
    }
}

order.php

<?php
session_start();
require 'connect.php';

$action = isset($_GET['a']) ? $_GET['a'] : "";
$itemCount = isset($_SESSION['cart']) ? count($_SESSION['cart']) : 0;
$_SESSION['formid'] = sha1('misitio.com' . microtime());
if (isset($_SESSION['qty'])) {
    $meQty = 0;
    foreach ($_SESSION['qty'] as $meItem) {
        $meQty = $meQty + $meItem;
    }
} else {
    $meQty = 0;
}
if (isset($_SESSION['cart']) and $itemCount > 0) {
    $itemIds = "";
    foreach ($_SESSION['cart'] as $itemId) {
        $itemIds = $itemIds . $itemId . ",";
    }
    $inputItems = rtrim($itemIds, ",");
    $meSql = "SELECT * FROM products WHERE id in ({$inputItems})";
    $meQuery = mysql_query($meSql);
    $meCount = mysql_num_rows($meQuery);
} else {
    $meCount = 0;
}
?>
<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <title>shopping cart</title>

        <!-- Bootstrap -->
        <link href="bootstrap/css/bootstrap.min.css" rel="stylesheet">
        <link href="bootstrap/css/nava.css" rel="stylesheet">

<script type="text/javascript">
    function updateSubmit(){
        if(document.formupdate.order_fullname.value == ""){
            alert('Please include your name too!');
            document.formupdate.order_fullname.focus();
            retu false;
        }
            if(document.formupdate.order_address.value == ""){
            alert('Please include your address!');
            document.formupdate.order_address.focus();
            retu false;
        }
            if(document.formupdate.order_phone.value == ""){
            alert('Please include a phone number!');
            document.formupdate.order_phone.focus();
            retu false;
        }
        document.formupdate.submit();
        retu false;
    }
</script>
    </head>
    <body>

            <h3>Order</h3>
            <!-- Main component for a primary marketing message or call to action -->
            <?php
            if ($action == 'removed')
            {
                echo "<div class="alert alert-waing">Products have been removed</div>";
            }

            if ($meCount == 0)
            {
                echo "<div class="alert alert-waing">No items in the basket</div>";
            } else
            {
                ?>

                <form action="updateorder.php" method="post" name="formupdate" role="form" id="formupdate" onsubmit="JavaScript:retu updateSubmit();">
                    <div class="form-group">
                        <label for="exampleInputEmail1">The name - last name</label>
                        <input type="text" class="form-control" id="order_fullname" placeholder="Enter your name" style="width: 300px;" name="order_fullname">
                    </div>
                    <div class="form-group">
                        <label for="exampleInputAddress">address</label>
                        <textarea class="form-control" rows="3" style="width: 500px;" name="order_address" id="order_address"></textarea>
                    </div>
                    <div class="form-group">
                        <label for="exampleInputPhone">phone</label>
                        <input type="text" class="form-control" id="order_phone" placeholder="Enter the phone number to call." style="width: 300px;" name="order_phone">
                    </div>
                    <table class="table table-striped table-bordered">
                        <thead>
                            <tr>
                                <th>Code</th>
                                <th>Product name</th>
                                <th>Detail</th>
                                <th>Quantity</th>
                                <th>Price by unit</th>
                                <th>Suptotal</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php
                            $total_price = 0;
                            $num = 0;
                            while ($meResult = mysql_fetch_assoc($meQuery))
                            {
                                $key = array_search($meResult['id'], $_SESSION['cart']);
                                $total_price = $total_price + ($meResult['product_price'] * $_SESSION['qty'][$key]);
                                ?>
                                <tr>
                                    <td><?php echo $meResult['product_code']; ?></td>
                                    <td><?php echo $meResult['product_name']; ?></td>
                                    <td><?php echo $meResult['product_desc']; ?></td>
                                    <td>
                                        <?php echo $_SESSION['qty'][$key]; ?>
                                        <input type="hidden" name="qty[]" value="<?php echo $_SESSION['qty'][$key]; ?>" />
                                        <input type="hidden" name="product_id[]" value="<?php echo $meResult['id']; ?>" />
                                        <input  type="hidden" name="product_price[]" value="<?php echo $meResult['product_price']; ?>" />
                                    </td>
                                    <td><?php echo number_format($meResult['product_price'], 2); ?></td>
                                    <td><?php echo number_format(($meResult['product_price'] * $_SESSION['qty'][$key]), 2); ?></td>
                                </tr>
                                <?php
                                $num++;
                                }
                            ?>
                            <tr>
                                <td colspan="8" style="text-align: right;">
                                    <h4>Total <?php echo number_format($total_price, 2); ?> $</h4>
                                </td>
                            </tr>
                            <tr>
                                <td colspan="8" style="text-align: right;">
                                    <input type="hidden" name="formid" value="<?php echo $_SESSION['formid']; ?>"/>
                                    <a href="cart.php" type="button" class="btn btn-danger btn-lg">Behind</a>
                                    <button type="submit" class="btn btn-primary btn-lg">Order</button>
                                </td>
                            </tr>
                        </tbody>
                    </table>
                </form>
                <?php
                }
            ?>



        <!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
        <script src="bootstrap/js/jquery.min.js"></script>
        <!-- Include all compiled plugins (below), or include individual files as needed -->
        <script src="bootstrap/js/bootstrap.min.js"></script>
    </body>
</html>
<?php
mysql_close();

I hope and I can help with the discount coupon system. It helps implement the necessary functions in my shopping cart

my code there is no discount coupon form please implement it with the code discount coupon system for full operation

form for discount for total order price in my cart shopping

I hope I have been clear to have no confusion and to be easier implemetar system

hank you very much hope your answers.

I forgot some of my shopping cart is programmed with MySQL. Please modify all the code if necessary with the new extecion MySQLi.

- - , .

Recent Questions...

ما را در سایت Recent Questions دنبال می‌کنید

برچسب: نویسنده: استخدام کار بازدید: 112 تاريخ: يکشنبه 9 اسفند 1394 ساعت: 5:31

صفحه بندی