Webmaster общности: Predpriemach.com | SearchEngines.bg

    Система за online пазаруване – Част 2-ра

    Автор: SandPrince

    Ето и 2-ра част на системата за online пазаруване. Както казах в предишния урок тук ще създадем няколко файла: index.php , cart.php , chekout.php , error.php и success.php

    Започваме с файла: index.php

    <?php
    require_once 'library/config.php';
    require_once 'library/category-functions.php';
    require_once 'library/product-functions.php';
    require_once 'library/cart-functions.php';
    
    $_SESSION['shop_return_url'] = $_SERVER['REQUEST_URI'];
    
    $catId = (isset($_GET['c']) && $_GET['c'] != '1') ? $_GET['c'] : 0;
    $pdId = (isset($_GET['p']) && $_GET['p'] != '') ? $_GET['p'] : 0;
    
    require_once 'include/header.php';
    ?>
    <table width="780" border="1" align="center" cellpadding="0" cellspacing="0">
    <tr>
    <td colspan="3">
    <?php require_once 'include/top.php'; ?>
    </td>
    </tr>
    <tr valign="top">
    <td width="150" height="400" id="leftnav">
    <?php
    require_once 'include/leftNav.php';
    ?>
    </td>
    <td>
    <?php
    if ($pdId) {
    require_once 'include/productDetail.php';
    } else if ($catId) {
    require_once 'include/productList.php';
    } else {
    require_once 'include/categoryList.php';
    }
    ?>
    </td>
    <td width="130" align="center"><?php require_once 'include/miniCart.php'; ?></td>
    </tr>
    </table>
    <?php
    require_once 'include/footer.php';
    ?>

    Както виждате най-горе има един инклуднат config.php файл който обаче ще създадем в 3-та част на тази системка. Продулжаваме със следващата страница:

    cart.php

    <?php
    require_once 'library/config.php';
    require_once 'library/cart-functions.php';
    
    $action = (isset($_GET['action']) && $_GET['action'] != '') ? $_GET['action'] : 'view';
    
    switch ($action) {
    case 'add' :
    addToCart();
    break;
    case 'update' :
    updateCart();
    break;
    case 'delete' :
    deleteFromCart();
    break;
    case 'view' :
    }
    
    $cartContent = getCartContent();
    $numItem = count($cartContent);
    
    $pageTitle = 'Shopping Cart';
    require_once 'include/header.php';
    
    // show the error message ( if we have any )
    displayError();
    
    if ($numItem > 0 ) {
    ?>
    <form action="<?php echo $_SERVER['PHP_SELF'] . "?action=update"; ?>" method="post" name="frmCart" id="frmCart">
    <table width="780" border="0" align="center" cellpadding="5" cellspacing="1" class="entryTable">
    <tr class="entryTableHeader">
    <td colspan="2" align="center">Item</td>
    <td align="center">Unit Price</td>
    <td width="75" align="center">Quantity</td>
    <td align="center">Total</td>
    <td width="75" align="center"> </td>
    </tr>
    <?php
    $subTotal = 0;
    for ($i = 0; $i < $numItem; $i++) {
    extract($cartContent[$i]);
    $productUrl = "index.php?c=$cat_id&p=$pd_id";
    $subTotal += $pd_price * $ct_qty;
    ?>
    <tr class="content">
    <td width="80" align="center"><a href="<?php echo $productUrl; ?>"><img src="<?php echo $pd_thumbnail; ?>" border="0"></a></td>
    <td><a href="<?php echo $productUrl; ?>"><?php echo $pd_name; ?></a></td>
    <td align="right"><?php echo displayAmount($pd_price); ?></td>
    <td width="75"><input name="txtQty[]" type="text" id="txtQty[]" size="5" value="<?php echo $ct_qty; ?>" class="box" onKeyUp="checkNumber(this);">
    <input name="hidCartId[]" type="hidden" value="<?php echo $ct_id; ?>">
    <input name="hidProductId[]" type="hidden" value="<?php echo $pd_id; ?>">
    </td>
    <td align="right"><?php echo displayAmount($pd_price * $ct_qty); ?></td>
    <td width="75" align="center"> <input name="btnDelete" type="button" id="btnDelete" value="Delete" onClick="window.location.href='<?php echo $_SERVER['PHP_SELF'] . "?action=delete&cid=$ct_id"; ?>';" class="box">
    </td>
    </tr>
    <?php
    }
    ?>
    <tr class="content">
    <td colspan="4" align="right">Sub-total</td>
    <td align="right"><?php echo displayAmount($subTotal); ?></td>
    <td width="75" align="center"> </td>
    </tr>
    <tr class="content">
    <td colspan="4" align="right">Shipping </td>
    <td align="right"><?php echo displayAmount($shopConfig['shippingCost']); ?></td>
    <td width="75" align="center"> </td>
    </tr>
    <tr class="content">
    <td colspan="4" align="right">Total </td>
    <td align="right"><?php echo displayAmount($subTotal + $shopConfig['shippingCost']); ?></td>
    <td width="75" align="center"> </td>
    </tr>
    <tr class="content">
    <td colspan="5" align="right"> </td>
    <td width="75" align="center">
    <input name="btnUpdate" type="submit" id="btnUpdate" value="Update Cart" class="box"></td>
    </tr>
    </table>
    </form>
    <?php
    } else {
    
    ?>
    <p> </p><table width="550" border="0" align="center" cellpadding="10" cellspacing="0">
    <tr>
    <td><p align="center">You shopping cart is empty</p>
    <p>If you find you are unable to add anything to your cart, please ensure that
    your internet browser has cookies enabled and that any other security software
    is not blocking your shopping session.</p></td>
    </tr>
    </table>
    <?php
    }
    
    $shoppingReturnUrl = isset($_SESSION['shop_return_url']) ? $_SESSION['shop_return_url'] : 'index.php';
    ?>
    <table width="550" border="0" align="center" cellpadding="10" cellspacing="0">
    <tr align="center">
    <td><input name="btnContinue" type="button" id="btnContinue" value="<< Continue Shopping" onClick="window.location.href='<?php echo $shoppingReturnUrl; ?>';" class="box"></td>
    <?php
    if ($numItem > 0) {
    ?>
    <td><input name="btnCheckout" type="button" id="btnCheckout" value="Proceed To Checkout >>" onClick="window.location.href='checkout.php?step=1';" class="box"></td>
    <?php
    }
    ?>
    </tr>
    </table>
    <?php
    require_once 'include/footer.php';
    ?>

    chekout.php

    <?php
    require_once 'library/config.php';
    require_once 'library/cart-functions.php';
    require_once 'library/checkout-functions.php';
    
    if (isCartEmpty()) {
    // the shopping cart is still empty
    // so checkout is not allowed
    header('Location: cart.php');
    } else if (isset($_GET['step']) && (int)$_GET['step'] > 0 && (int)$_GET['step'] <= 3) {
    $step = (int)$_GET['step'];
    
    $includeFile = '';
    if ($step == 1) {
    $includeFile = 'shippingAndPaymentInfo.php';
    $pageTitle = 'Checkout - Step 1 of 2';
    } else if ($step == 2) {
    $includeFile = 'checkoutConfirmation.php';
    $pageTitle = 'Checkout - Step 2 of 2';
    } else if ($step == 3) {
    $orderId = saveOrder();
    $orderAmount = getOrderAmount($orderId);
    
    $_SESSION['orderId'] = $orderId;
    
    // our next action depends on the payment method
    // if the payment method is COD then show the
    // success page but when paypal is selected
    // send the order details to paypal
    if ($_POST['hidPaymentMethod'] == 'cod') {
    header('Location: success.php');
    exit;
    } else {
    $includeFile = 'paypal/payment.php';
    }
    }
    } else {
    // missing or invalid step number, just redirect
    header('Location: index.php');
    }
    
    require_once 'include/header.php';
    ?>
    <script language="JavaScript" type="text/javascript" src="library/checkout.js"></script>
    <?php
    require_once "include/$includeFile";
    require_once 'include/footer.php';
    ?>

    error.php

    <?php
    require_once 'library/config.php';
    
    $pageTitle = 'Checkout Error';
    require_once 'include/header.php';
    ?>
    <p> </p><table width="500" border="0" align="center" cellpadding="1" cellspacing="0">
    <tr>
    <td align="left" valign="top" bgcolor="#333333"> <table width="100%" border="0" cellspacing="0" cellpadding="0">
    <tr>
    <td align="center" bgcolor="#EEEEEE"> <p> </p>
    <p>We are really sorry for this inconvenience but there
    was an error when processing your order. Please contact
    the site administrator. To go to the main page please
    <a href="index.php">click here</a></p>
    <p> </p></td>
    </tr>
    </table></td>
    </tr>
    </table>
    <br>
    <br>
    <?php
    require_once 'include/footer.php';
    ?>

    И накрая за сега създаваме success.php

    <?php
    require_once 'library/config.php';
    
    // if no order id defined in the session
    // redirect to main page
    if (!isset($_SESSION['orderId'])) {
    header('Location: ' . WEB_ROOT);
    exit;
    }
    
    $pageTitle = 'Checkout Completed Successfully';
    require_once 'include/header.php';
    
    
    // send notification email
    if ($shopConfig['sendOrderEmail'] == 'y') {
    $subject = "[New Order] " . $_SESSION['orderId'];
    $email = $shopConfig['email'];
    $message = "You have a new order. Check the order detail here \n http://" . $_SERVER['HTTP_HOST'] . WEB_ROOT . 'admin/order/index.php?view=detail&oid=' . $_SESSION['orderId'] ;
    mail($email, $subject, $message, "From: $email\r\nReturn-path: $email");
    }
    
    
    unset($_SESSION['orderId']);
    ?>
    <p> </p><table width="500" border="0" align="center" cellpadding="1" cellspacing="0">
    <tr>
    <td align="left" valign="top" bgcolor="#333333"> <table width="100%" border="0" cellspacing="0" cellpadding="0">
    <tr>
    <td align="center" bgcolor="#EEEEEE"> <p> </p>
    <p>Thank you for shopping with us! We will send the purchased
    item(s) immediately. To continue shopping please <a href="index.php">click
    here</a></p>
    <p> </p></td>
    </tr>
    </table></td>
    </tr>
    </table>
    <br>
    <br>
    <?php
    require_once 'include/footer.php';
    ?>

    Всички тези файлове ги поставяме в основната категория където ще ни се намира системата за online пазаруване. В същата категория трябва да създадем и една папка images а в нея две други папки : product и category. В основната гатегория на системата трябва да създадете и още папки : admin,include и library. В тях също ще има подпапки и файлове но тях ще ги правим в следващите части на системата за онлайн пазаруване.