Advertisement
Guest User

Untitled

a guest
Dec 1st, 2017
694
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 13.44 KB | None | 0 0
  1. <?php
  2.     /*
  3.         Template Name: Checkout Page
  4.     */
  5.    
  6.     $error = array();
  7.     if(isset($_POST['order']) && validate()){
  8.         $_SESSION['order_info'] = array(
  9.         'fullname' => $_POST['fullname'],
  10.         'email' => $_POST['email'],
  11.         'tel' => $_POST['tel'],
  12.         'addr' => $_POST['addr'],
  13.         'comment' => $_POST['comment'],
  14.         );
  15.         // save to database
  16.         if(function_exists('hdc_save_data')){
  17.             $rs = hdc_save_data();
  18.             if($rs){
  19.                 $_SESSION['order_id'] = $rs;
  20.                 $_SESSION['order_success'] = order_success_msg();
  21.                
  22.                 // send mail
  23.                 $subject = 'Đơn đặt hàng từ '.substr(get_site_url(), 7).' - Mã đơn hàng: '.$_SESSION['order_id'];
  24.                
  25.                 $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>' .
  26.                 '<tr><td style="padding:5px;"><b>Điện thoại</b></td><td>'.$_POST['tel'] . '</td></tr>' .
  27.                 '<tr><td style="padding:5px;"><b>Email</b></td><td>'.$_POST['email'] . '</td></tr>' .
  28.                 '<tr><td style="padding:5px;"><b>Địa chỉ nhận hàng</b></td><td>'.$_POST['addr'] . '</td></tr>' .
  29.                 '</table> <br/><hr></hr>' .
  30.                 '<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>
  31.                
  32.                 ';
  33.                 $sum = 0;
  34.                 foreach($_POST['item_name'] as $k=>$v){
  35.                     $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>';
  36.                     $sum += $_POST['price'][$k];
  37.                 }
  38.                 $content .= '</table> <br/> <p style="padding:5px;">Tổng cộng: '.number_format($sum, 0, ',', '.').' VNĐ</p>';
  39.                 $content .= '<br/> <p style="padding:5px;">Ghi chú: '.$_POST['comment'].'</p>';
  40.                 send_confirm_mail($_SESSION['order_info']['email']);
  41.                 send_data_mail($subject, $content, 'trathaoduocphap@yahoo.com');
  42.                 send_data_mail($subject, $content, 'trthanhtri@yahoo.com');
  43.                 send_data_mail($subject, $content, 'thuha_17@yahoo.com');
  44.  
  45.                 unset($_SESSION['num_item_in_cart']);
  46.                 unset($_SESSION['cart']);
  47.                 unset($_SESSION['order_info']);
  48.                 wp_redirect( home_url().'/thanh-toan' ); exit;
  49.             }
  50.             else{
  51.                 $_SESSION['order_error'] = true;
  52.                 wp_redirect( home_url().'/thanh-toan' ); exit;
  53.             }
  54.         }
  55.     }
  56.    
  57.     function order_success_msg(){
  58.         ob_start();
  59.     ?>
  60.     <div style="margin:20px 0 30px;">
  61.         <div class="d-cell"><div class="check_success"></div></div>
  62.         <div class="d-cell text">
  63.             <p class="uk-text-bold uk-text-large">Cám ơn quý khách đã đặt hàng</p>
  64.             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
  65.         </div>
  66.     </div>
  67.     <div class="uk-grid uk-grid-medium">
  68.         <div class="uk-width-large-2-6">
  69.             <div class="uk-panel uk-panel-box uk-panel-header">
  70.                 <h3 class="uk-panel-title">Địa chỉ giao hàng và thanh toán</h3>
  71.                 <p class=""><?php echo $_POST['fullname']; ?></p>
  72.                 <p class=""><?php echo $_POST['addr']; ?></p>
  73.             </div>
  74.         </div>
  75.         <div class="uk-width-large-4-6">
  76.             <div id="cart_tbl" class="uk-panel uk-panel-box uk-panel-header">
  77.                 <div class="uk-panel-title">Đơn hàng # <?php echo $_SESSION['order_id']; ?></div>
  78.                 <?php
  79.                     $sum = 0;
  80.                     $html = '<table class="uk-table">
  81.                     <tbody>';
  82.                    
  83.                     global $post;
  84.                     foreach($_SESSION['cart'] as $id=>$qty){
  85.                         $post = get_post($id);
  86.                        
  87.                         $actual_price = get_field('actual-price');
  88.                         if(!empty($actual_price)){
  89.                             $price = $actual_price;
  90.                         }
  91.                         else{
  92.                             $price = get_field('price');
  93.                         }
  94.                         $html .= '<tr>
  95.                         <td class="thumb"><a href="'.get_the_permalink().'">'.get_the_post_thumbnail($id, 'thumbnail').'</a></td>
  96.                         <td class="name"><a href="'.get_the_permalink().'">'.get_the_title().'</a> x '.$qty.'
  97.                         </td>
  98.                         <td class="total uk-text-right">';
  99.                         if(!is_numeric($price)) $html .= '0';
  100.                         else{
  101.                             $price = $price*$qty;
  102.                             $html .= number_format($price, 0, ',', '.').' ₫';
  103.                             $sum+=$price;
  104.                         }
  105.                         $html .= '</td></tr>';
  106.                     }
  107.                     $html .= '</tbody></table>';
  108.                     echo $html;
  109.                 ?>
  110.                 <div class="d-table full_width">
  111.                     <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>
  112.                     <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>
  113.                     <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>
  114.                 </div>
  115.                 <div style="border-top:1px solid #dddddd; margin-bottom:20px;"></div>
  116.                 <div class="d-table full_width">
  117.                     <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>
  118.                 </div>
  119.             </div>
  120.             <div class="uk-text-right" style="margin-top:20px;">
  121.                 <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>
  122.             </div>
  123.         </div>
  124.     </div>
  125.     <?php
  126.         $tmp = ob_get_contents();
  127.         ob_end_clean();
  128.         return $tmp;
  129.     }
  130.    
  131.     function validate(){
  132.         global $error;
  133.         $_POST['fullname'] = esc_html($_POST['fullname']);
  134.         $_POST['tel'] = esc_html($_POST['tel']);
  135.         $_POST['email'] = esc_html($_POST['email']);
  136.         $_POST['addr'] = esc_html($_POST['addr']);
  137.         $_POST['comment'] = esc_html($_POST['comment']);
  138.        
  139.         if(empty($_POST['fullname'])){
  140.             $error['fullname'] = 'Chưa nhập họ tên!';
  141.         }
  142.         if(empty($_POST['tel'])){
  143.             $error['tel'] = 'Chưa nhập số điện thoại!';
  144.         }
  145. /*
  146.         if(empty($_POST['email'])){
  147.             $error['email'] = 'Chưa nhập địa chỉ email!';
  148.         }
  149.  
  150.         else{
  151.             if(!is_email($_POST['email'])){
  152.                 $error['email'] = 'Địa chỉ email không hợp lệ!';
  153.             }
  154.         }
  155.         */
  156.         if(empty($_POST['addr'])){
  157.             $error['addr'] = 'Chưa nhập địa chỉ nhận hàng!';
  158.         }
  159.         if(!empty($error)) return false;
  160.         return true;
  161.     }
  162.     $GLOBALS['show_sidebar'] = false;
  163. get_header(); ?>
  164. <div class="wrap_content">
  165.     <div class="uk-container uk-container-center">
  166.         <?php the_breadcrumb();?>
  167.         <?php
  168.             if(isset($_SESSION['order_success'])){
  169.                 echo $_SESSION['order_success'];
  170.                 unset($_SESSION['order_success']);
  171.             }
  172.             elseif(empty($_SESSION['cart'])){
  173.                 if(isset($_SESSION['order_error'])){
  174.                     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>';
  175.                     unset($_SESSION['order_error']);
  176.                 }
  177.                 else{
  178.                     echo '<div class="uk-text-center uk-text-large">
  179.                     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>';
  180.                 }
  181.             }
  182.             elseif(isset($_SESSION['order_error'])){
  183.                 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>';
  184.                 unset($_SESSION['order_error']);
  185.             }
  186.             else{
  187.                
  188.             ?>
  189.             <form id="order_form" class="uk-form" method="post" action="">
  190.                 <div class="uk-grid uk-grid-medium">
  191.                     <div class="uk-width-large-1-3 uk-grid-margin">
  192.                         <div class="msg_f">
  193.                             <?php if(!empty($error)): ?>
  194.                             <div class="uk-alert uk-alert-danger">
  195.                                 <?php
  196.                                     foreach($error as $v){
  197.                                         echo '<p>'.$v.'</p>';
  198.                                     }
  199.                                 ?>
  200.                             </div>
  201.                             <?php endif;?>
  202.                         </div>
  203.                         <style type="text/css">
  204.                             .h-title {font-size:22px;}
  205.                             .checkout_form .uk-form-icon {display:block;}
  206.                             .uk-form-icon > .uk-icon-asterisk {color:#e62e03; font-size:18px; top: 16px;}
  207.                             .uk-form-icon > .uk-icon-asterisk:before {content:"*";}
  208.                         </style>
  209.                         <div class="checkout_form">
  210.                             <h3 class="h-title">Thông tin thanh toán</h3>
  211.                             <div class="uk-form-row">
  212.                                 <div class="uk-form-controls">
  213.                                     <div class="uk-form-icon">
  214.                                         <i class="uk-icon-asterisk"></i>
  215.                                         <input type="text" class="" id="fullname" name="fullname" value="<?php if(isset($_POST['fullname'])) echo $_POST['fullname']; ?>" placeholder="Họ tên" required />
  216.                                     </div>
  217.                                 </div>
  218.                             </div>
  219.                             <div class="uk-form-row">
  220.                                 <div class="uk-form-controls">
  221.                                     <div class="uk-form-icon">
  222.                                         <i class="uk-icon-asterisk"></i>
  223.                                         <input type="text" class="" id="tel" name="tel" value="<?php if(isset($_POST['tel'])) echo $_POST['tel']; ?>" required placeholder="Số điện thoại" />
  224.                                     </div>
  225.                                 </div>
  226.                             </div>
  227.                             <div class="uk-form-row">
  228.                                 <div class="uk-form-controls">
  229.                                     <div class="uk-form-icon">
  230.                                        
  231.                                         <input type="email" class="" id="email" name="email" value="<?php if(isset($_POST['email'])) echo $_POST['email']; ?>" placeholder="Email" />
  232.                                     </div>
  233.                                 </div>
  234.                             </div>
  235.                             <div class="uk-form-row">
  236.                                 <div class="uk-form-controls">
  237.                                     <div class="uk-form-icon">
  238.                                         <i class="uk-icon-asterisk"></i>
  239.                                         <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"/>
  240.                                     </div>
  241.                                 </div>
  242.                             </div>
  243.                            
  244.                             <div class="uk-form-row">
  245.                                 <div class="uk-form-controls">
  246.                                     <textarea class="" name="comment" id="comment" rows="6" placeholder="Ghi chú" style="padding-left:30px;"><?php if(isset($_POST['comment'])) echo $_POST['comment']; ?></textarea>
  247.                                 </div>
  248.                             </div>
  249.                         </div>
  250.                        
  251.                     </div>
  252.                     <div class="uk-width-large-1-3 uk-grid-margin">
  253.                         <h3 class="h-title">Vận chuyển</h3>
  254.                         <p>Giao hàng tận nơi</p>
  255.                         <h3 class="h-title">Thanh toán</h3>
  256.                         <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>
  257.                     </div>
  258.                     <div class="uk-width-large-1-3 uk-grid-margin">
  259.                         <h3 class="h-title">Đơn hàng</h3>
  260.                         <div id="cart_tbl" class="uk-overflow-container">
  261.                             <?php
  262.                                 $sum = 0;
  263.                                 $html = '<table class="uk-table" style="background:#fafafa;">
  264.                                 <tbody>';
  265.                                
  266.                                 foreach($_SESSION['cart'] as $id=>$qty):
  267.                                 $post = get_post($id);
  268.                                
  269.                                 $actual_price = get_field('actual-price');
  270.                                 if(!empty($actual_price)){
  271.                                     $price = $actual_price;
  272.                                 }
  273.                                 else{
  274.                                     $price = get_field('price');
  275.                                 }
  276.                                 $html .= '<tr>
  277.                                 <td class="thumb"><a href="'.get_the_permalink().'">'.get_the_post_thumbnail($id, 'thumbnail').'</a></td>
  278.                                 <td class="name"><a href="'.get_the_permalink().'">'.get_the_title().'</a> x '.$qty.'
  279.                                 <input type="hidden" name="id[]" value="'.$id.'" />
  280.                                 <input type="hidden" name="item_name[]" value="'.get_the_title().'" />
  281.                                 <input type="hidden" name="item_qty[]" value="'.$qty.'" />
  282.                                 <input type="hidden" name="price[]" value="'.$price.'" />
  283.                                 </td>
  284.                                 <td class="total uk-text-right">';
  285.                                 if(!is_numeric($price)) $html .= '0';
  286.                                 else{
  287.                                     $price = $price*$qty;
  288.                                     $html .= number_format($price, 0, ',', '.').' ₫';
  289.                                     $sum+=$price;
  290.                                 }
  291.                                 $html .= '</td></tr>';
  292.                                 endforeach;
  293.                                 $html .= '<tr><td colspan="2">Tạm tính</td><td class="uk-text-right">'.number_format($sum, 0, ',', '.').' ₫</td></tr>';
  294.                                 $html .= '<tr><td colspan="2">Phí ship</td><td class="uk-text-right">'.number_format(get_field('ship_fee', 'option'), 0, ',', '.').' ₫</td></tr>';
  295.                                 $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>';
  296.                                 $html .= '</tbody></table>';
  297.                                 echo $html;
  298.                             ?>
  299.                         </div>
  300.                         <div id="order_submit" class="uk-text-right" style="margin-top:20px;">
  301.                             <input type="hidden" name="order" />
  302.                             <button type="submit" class="flat_bt small" style="font-size:16px; padding:5px; width:100%;">Đặt hàng</button>
  303.                            
  304.                         </div>
  305.                     </div>
  306.                 </div>
  307.             </form>
  308.             <script src="<?php echo get_template_directory_uri(); ?>/js/jquery.validate.min.js"></script>
  309.             <script type="text/javascript">
  310.                
  311.                 $('#order_form').validate({
  312.                     errorClass: "uk-form-danger",
  313. rules: {
  314.     email: {
  315.       required: false
  316.     }
  317.   },
  318.                     messages: {
  319. /*
  320.                         email: {required: "Chưa nhập địa chỉ email!",email: "Địa chỉ email không hợp lệ!",},*/
  321.                         fullname: {required: "Chưa nhập họ tên!",},
  322.                         tel: {required: "Chưa nhập số điện thoại!",},
  323.                         addr: {required: "Chưa nhập địa chỉ nhận hàng!",},  
  324.                     },
  325.                     submitHandler: function(form) {
  326.                         //$('#order_submit button').attr('disabled', 'disabled');
  327.                         submit_loading($('#order_submit button'));
  328.                         form.submit();
  329.                     }
  330.                 });
  331.                
  332.             </script>
  333.         <?php } ?>
  334.     </div>
  335. </div>
  336. <?php get_footer(); ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement