Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /*
- Template Name: Checkout Page
- */
- $error = array();
- if(isset($_POST['order']) && validate()){
- $_SESSION['order_info'] = array(
- 'fullname' => $_POST['fullname'],
- 'email' => $_POST['email'],
- 'tel' => $_POST['tel'],
- 'addr' => $_POST['addr'],
- 'comment' => $_POST['comment'],
- );
- // save to database
- if(function_exists('hdc_save_data')){
- $rs = hdc_save_data();
- if($rs){
- $_SESSION['order_id'] = $rs;
- $_SESSION['order_success'] = order_success_msg();
- // send mail
- $subject = 'Đơn đặt hàng từ '.substr(get_site_url(), 7).' - Mã đơn hàng: '.$_SESSION['order_id'];
- $content = '<h3 style="color:#23527C;">'.$subject.'</h3><br/><b>Thông tin đặt hàng</b> <br/><table><tr><td style="width:200px;padding:5px;"><b>Họ và tên</b></td><td>'.$_POST['fullname'] . '</td></tr>' .
- '<tr><td style="padding:5px;"><b>Điện thoại</b></td><td>'.$_POST['tel'] . '</td></tr>' .
- '<tr><td style="padding:5px;"><b>Email</b></td><td>'.$_POST['email'] . '</td></tr>' .
- '<tr><td style="padding:5px;"><b>Địa chỉ nhận hàng</b></td><td>'.$_POST['addr'] . '</td></tr>' .
- '</table> <br/><hr></hr>' .
- '<table><tr><th style="min-width:350px;padding:5px;">Tên sản phẩm</th><th>Số lượng</th><th style="min-width:150px;">Giá mua hàng</th></tr>
- ';
- $sum = 0;
- foreach($_POST['item_name'] as $k=>$v){
- $content .= '<tr><td style="padding:5px;">'.$v.'</td><td align="right">'.$_POST['item_qty'][$k].'</td><td align="right">'.number_format($_POST['price'][$k], 0, ',', '.').' VNĐ</td></tr>';
- $sum += $_POST['price'][$k];
- }
- $content .= '</table> <br/> <p style="padding:5px;">Tổng cộng: '.number_format($sum, 0, ',', '.').' VNĐ</p>';
- $content .= '<br/> <p style="padding:5px;">Ghi chú: '.$_POST['comment'].'</p>';
- send_confirm_mail($_SESSION['order_info']['email']);
- send_data_mail($subject, $content, 'trathaoduocphap@yahoo.com');
- send_data_mail($subject, $content, 'trthanhtri@yahoo.com');
- send_data_mail($subject, $content, 'thuha_17@yahoo.com');
- unset($_SESSION['num_item_in_cart']);
- unset($_SESSION['cart']);
- unset($_SESSION['order_info']);
- wp_redirect( home_url().'/thanh-toan' ); exit;
- }
- else{
- $_SESSION['order_error'] = true;
- wp_redirect( home_url().'/thanh-toan' ); exit;
- }
- }
- }
- function order_success_msg(){
- ob_start();
- ?>
- <div style="margin:20px 0 30px;">
- <div class="d-cell"><div class="check_success"></div></div>
- <div class="d-cell text">
- <p class="uk-text-bold uk-text-large">Cám ơn quý khách đã đặt hàng</p>
- 1 email xác nhận đã được gửi tới <?php echo $_POST['email']; ?>. Xin vui lòng kiểm tra email của bạn
- </div>
- </div>
- <div class="uk-grid uk-grid-medium">
- <div class="uk-width-large-2-6">
- <div class="uk-panel uk-panel-box uk-panel-header">
- <h3 class="uk-panel-title">Địa chỉ giao hàng và thanh toán</h3>
- <p class=""><?php echo $_POST['fullname']; ?></p>
- <p class=""><?php echo $_POST['addr']; ?></p>
- </div>
- </div>
- <div class="uk-width-large-4-6">
- <div id="cart_tbl" class="uk-panel uk-panel-box uk-panel-header">
- <div class="uk-panel-title">Đơn hàng # <?php echo $_SESSION['order_id']; ?></div>
- <?php
- $sum = 0;
- $html = '<table class="uk-table">
- <tbody>';
- global $post;
- foreach($_SESSION['cart'] as $id=>$qty){
- $post = get_post($id);
- $actual_price = get_field('actual-price');
- if(!empty($actual_price)){
- $price = $actual_price;
- }
- else{
- $price = get_field('price');
- }
- $html .= '<tr>
- <td class="thumb"><a href="'.get_the_permalink().'">'.get_the_post_thumbnail($id, 'thumbnail').'</a></td>
- <td class="name"><a href="'.get_the_permalink().'">'.get_the_title().'</a> x '.$qty.'
- </td>
- <td class="total uk-text-right">';
- if(!is_numeric($price)) $html .= '0';
- else{
- $price = $price*$qty;
- $html .= number_format($price, 0, ',', '.').' ₫';
- $sum+=$price;
- }
- $html .= '</td></tr>';
- }
- $html .= '</tbody></table>';
- echo $html;
- ?>
- <div class="d-table full_width">
- <div class="d-row"><div class="d-cell"><p>Giá</p></div><div class="d-cell uk-text-right"><?php echo number_format($sum, 0, ',', '.'); ?> ₫</div></div>
- <div class="d-row"><div class="d-cell"><p>Phí ship</p></div><div class="d-cell uk-text-right"><?php echo number_format(get_field('ship_fee', 'option'), 0, ',', '.'); ?> ₫</div></div>
- <div class="d-row"><div class="d-cell"><p>Phương thức thanh toán</p></div><div class="d-cell uk-text-right">Thanh toán khi giao hàng (COD)</div></div>
- </div>
- <div style="border-top:1px solid #dddddd; margin-bottom:20px;"></div>
- <div class="d-table full_width">
- <div class="d-row"><div class="d-cell uk-text-large"><p><b>Tổng cộng</b></p></div><div class="d-cell uk-text-right"><span class="uk-text-large"><b style="color:#009975;"><?php echo number_format($sum + get_field('ship_fee', 'option'), 0, ',', '.'); ?></b></span> ₫</div></div>
- </div>
- </div>
- <div class="uk-text-right" style="margin-top:20px;">
- <a href="<?php echo home_url(); ?>" class="flat_bt small" style="font-size:16px; padding:10px 15px;">Tiếp tục mua hàng</a>
- </div>
- </div>
- </div>
- <?php
- $tmp = ob_get_contents();
- ob_end_clean();
- return $tmp;
- }
- function validate(){
- global $error;
- $_POST['fullname'] = esc_html($_POST['fullname']);
- $_POST['tel'] = esc_html($_POST['tel']);
- $_POST['email'] = esc_html($_POST['email']);
- $_POST['addr'] = esc_html($_POST['addr']);
- $_POST['comment'] = esc_html($_POST['comment']);
- if(empty($_POST['fullname'])){
- $error['fullname'] = 'Chưa nhập họ tên!';
- }
- if(empty($_POST['tel'])){
- $error['tel'] = 'Chưa nhập số điện thoại!';
- }
- /*
- if(empty($_POST['email'])){
- $error['email'] = 'Chưa nhập địa chỉ email!';
- }
- else{
- if(!is_email($_POST['email'])){
- $error['email'] = 'Địa chỉ email không hợp lệ!';
- }
- }
- */
- if(empty($_POST['addr'])){
- $error['addr'] = 'Chưa nhập địa chỉ nhận hàng!';
- }
- if(!empty($error)) return false;
- return true;
- }
- $GLOBALS['show_sidebar'] = false;
- get_header(); ?>
- <div class="wrap_content">
- <div class="uk-container uk-container-center">
- <?php the_breadcrumb();?>
- <?php
- if(isset($_SESSION['order_success'])){
- echo $_SESSION['order_success'];
- unset($_SESSION['order_success']);
- }
- elseif(empty($_SESSION['cart'])){
- if(isset($_SESSION['order_error'])){
- echo '<p class="uk-text-danger">Chúng tôi rất tiếc nhưng thông tin giỏ hàng của bạn đã bị lỗi, xin vui lòng đặt hàng lại ở <a href="',home_url(),'">trang chủ</a>.</p>';
- unset($_SESSION['order_error']);
- }
- else{
- echo '<div class="uk-text-center uk-text-large">
- Bạn không có sản phẩm nào trong giỏ hàng. Click <a href="',home_url(),'">vào đây</a> để tiếp tục mua sắm.</div>';
- }
- }
- elseif(isset($_SESSION['order_error'])){
- echo '<p class="uk-text-danger">Chúng tôi rất tiếc nhưng vì một lý do nào đó mà đơn hàng của bạn đã bị lỗi, xin vui lòng tải lại trang và thử lại.</p>';
- unset($_SESSION['order_error']);
- }
- else{
- ?>
- <form id="order_form" class="uk-form" method="post" action="">
- <div class="uk-grid uk-grid-medium">
- <div class="uk-width-large-1-3 uk-grid-margin">
- <div class="msg_f">
- <?php if(!empty($error)): ?>
- <div class="uk-alert uk-alert-danger">
- <?php
- foreach($error as $v){
- echo '<p>'.$v.'</p>';
- }
- ?>
- </div>
- <?php endif;?>
- </div>
- <style type="text/css">
- .h-title {font-size:22px;}
- .checkout_form .uk-form-icon {display:block;}
- .uk-form-icon > .uk-icon-asterisk {color:#e62e03; font-size:18px; top: 16px;}
- .uk-form-icon > .uk-icon-asterisk:before {content:"*";}
- </style>
- <div class="checkout_form">
- <h3 class="h-title">Thông tin thanh toán</h3>
- <div class="uk-form-row">
- <div class="uk-form-controls">
- <div class="uk-form-icon">
- <i class="uk-icon-asterisk"></i>
- <input type="text" class="" id="fullname" name="fullname" value="<?php if(isset($_POST['fullname'])) echo $_POST['fullname']; ?>" placeholder="Họ tên" required />
- </div>
- </div>
- </div>
- <div class="uk-form-row">
- <div class="uk-form-controls">
- <div class="uk-form-icon">
- <i class="uk-icon-asterisk"></i>
- <input type="text" class="" id="tel" name="tel" value="<?php if(isset($_POST['tel'])) echo $_POST['tel']; ?>" required placeholder="Số điện thoại" />
- </div>
- </div>
- </div>
- <div class="uk-form-row">
- <div class="uk-form-controls">
- <div class="uk-form-icon">
- <input type="email" class="" id="email" name="email" value="<?php if(isset($_POST['email'])) echo $_POST['email']; ?>" placeholder="Email" />
- </div>
- </div>
- </div>
- <div class="uk-form-row">
- <div class="uk-form-controls">
- <div class="uk-form-icon">
- <i class="uk-icon-asterisk"></i>
- <input type="text" class="" id="addr" name="addr" value="<?php if(isset($_POST['addr'])) echo $_POST['addr']; ?>" required placeholder="Địa chỉ nhận hàng"/>
- </div>
- </div>
- </div>
- <div class="uk-form-row">
- <div class="uk-form-controls">
- <textarea class="" name="comment" id="comment" rows="6" placeholder="Ghi chú" style="padding-left:30px;"><?php if(isset($_POST['comment'])) echo $_POST['comment']; ?></textarea>
- </div>
- </div>
- </div>
- </div>
- <div class="uk-width-large-1-3 uk-grid-margin">
- <h3 class="h-title">Vận chuyển</h3>
- <p>Giao hàng tận nơi</p>
- <h3 class="h-title">Thanh toán</h3>
- <p><i class="uk-icon-check-circle uk-text-primary uk-text-large" style="font-size: 25px;"></i> <b>Thanh toán khi giao hàng (COD)</b></p>
- </div>
- <div class="uk-width-large-1-3 uk-grid-margin">
- <h3 class="h-title">Đơn hàng</h3>
- <div id="cart_tbl" class="uk-overflow-container">
- <?php
- $sum = 0;
- $html = '<table class="uk-table" style="background:#fafafa;">
- <tbody>';
- foreach($_SESSION['cart'] as $id=>$qty):
- $post = get_post($id);
- $actual_price = get_field('actual-price');
- if(!empty($actual_price)){
- $price = $actual_price;
- }
- else{
- $price = get_field('price');
- }
- $html .= '<tr>
- <td class="thumb"><a href="'.get_the_permalink().'">'.get_the_post_thumbnail($id, 'thumbnail').'</a></td>
- <td class="name"><a href="'.get_the_permalink().'">'.get_the_title().'</a> x '.$qty.'
- <input type="hidden" name="id[]" value="'.$id.'" />
- <input type="hidden" name="item_name[]" value="'.get_the_title().'" />
- <input type="hidden" name="item_qty[]" value="'.$qty.'" />
- <input type="hidden" name="price[]" value="'.$price.'" />
- </td>
- <td class="total uk-text-right">';
- if(!is_numeric($price)) $html .= '0';
- else{
- $price = $price*$qty;
- $html .= number_format($price, 0, ',', '.').' ₫';
- $sum+=$price;
- }
- $html .= '</td></tr>';
- endforeach;
- $html .= '<tr><td colspan="2">Tạm tính</td><td class="uk-text-right">'.number_format($sum, 0, ',', '.').' ₫</td></tr>';
- $html .= '<tr><td colspan="2">Phí ship</td><td class="uk-text-right">'.number_format(get_field('ship_fee', 'option'), 0, ',', '.').' ₫</td></tr>';
- $html .= '<tr><td colspan="2">Tổng cộng</td><td class="uk-text-right" style="width: 120px;"><span class="uk-text-large"><b style="color:#009975;">'.number_format($sum + get_field('ship_fee', 'option'), 0, ',', '.').'</b></span> ₫</td></tr>';
- $html .= '</tbody></table>';
- echo $html;
- ?>
- </div>
- <div id="order_submit" class="uk-text-right" style="margin-top:20px;">
- <input type="hidden" name="order" />
- <button type="submit" class="flat_bt small" style="font-size:16px; padding:5px; width:100%;">Đặt hàng</button>
- </div>
- </div>
- </div>
- </form>
- <script src="<?php echo get_template_directory_uri(); ?>/js/jquery.validate.min.js"></script>
- <script type="text/javascript">
- $('#order_form').validate({
- errorClass: "uk-form-danger",
- rules: {
- email: {
- required: false
- }
- },
- messages: {
- /*
- email: {required: "Chưa nhập địa chỉ email!",email: "Địa chỉ email không hợp lệ!",},*/
- fullname: {required: "Chưa nhập họ tên!",},
- tel: {required: "Chưa nhập số điện thoại!",},
- addr: {required: "Chưa nhập địa chỉ nhận hàng!",},
- },
- submitHandler: function(form) {
- //$('#order_submit button').attr('disabled', 'disabled');
- submit_loading($('#order_submit button'));
- form.submit();
- }
- });
- </script>
- <?php } ?>
- </div>
- </div>
- <?php get_footer(); ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement