/**
 * Outputs the order received page
 **/
function woocommerce_thankyou($atts)
{
    global $woocommerce;
    woocommerce_nocache();
    $order = false;
    // Pay for order after checkout step
    if (isset($_GET['order'])) {
        $order_id = $_GET['order'];
    } else {
        $order_id = 0;
    }
    if (isset($_GET['key'])) {
        $order_key = $_GET['key'];
    } else {
        $order_key = '';
    }
    // Empty awaiting payment session
    unset($_SESSION['order_awaiting_payment']);
    if ($order_id > 0) {
        $order = new WC_Order($order_id);
        if ($order->order_key != $order_key) {
            unset($order);
        }
    }
    woocommerce_get_template('checkout/thankyou.php', array('order' => $order));
}
function woocommerce_order_tracking($atts)
{
    global $woocommerce;
    woocommerce_nocache();
    extract(shortcode_atts(array(), $atts));
    global $post;
    if ($_POST) {
        $woocommerce->verify_nonce('order_tracking');
        if (isset($_POST['orderid']) && $_POST['orderid'] > 0) {
            $order_id = (int) $_POST['orderid'];
        } else {
            $order_id = 0;
        }
        if (isset($_POST['order_email']) && $_POST['order_email']) {
            $order_email = trim($_POST['order_email']);
        } else {
            $order_email = '';
        }
        $order = new WC_Order($order_id);
        if ($order->id && $order_email) {
            if ($order->billing_email == $order_email) {
                woocommerce_get_template('order/tracking.php', array('order' => $order));
                return;
            }
        }
        echo '<p>' . sprintf(__('Sorry, we could not find that order id in our database. <a href="%s">Want to retry?</a>', 'woocommerce'), get_permalink($post->ID)) . '</p>';
    } else {
        woocommerce_get_template('order/form-tracking.php');
    }
}
function woocommerce_checkout($atts)
{
    global $woocommerce;
    woocommerce_nocache();
    if (sizeof($woocommerce->cart->get_cart()) == 0) {
        return;
    }
    do_action('woocommerce_check_cart_items');
    if ($woocommerce->error_count() > 0) {
        woocommerce_get_template('checkout/cart-errors.php');
    } else {
        $non_js_checkout = isset($_POST['woocommerce_checkout_update_totals']) && $_POST['woocommerce_checkout_update_totals'] ? true : false;
        if ($woocommerce->error_count() == 0 && $non_js_checkout) {
            $woocommerce->add_message(__('The order totals have been updated. Please confirm your order by pressing the Place Order button at the bottom of the page.', 'woocommerce'));
        }
        woocommerce_get_template('checkout/form-checkout.php');
    }
}
function woocommerce_cart($atts)
{
    global $woocommerce;
    woocommerce_nocache();
    if (!defined('WOOCOMMERCE_CART')) {
        define('WOOCOMMERCE_CART', true);
    }
    // Remove Discount Codes
    if (isset($_GET['remove_discounts'])) {
        $woocommerce->cart->remove_coupons($_GET['remove_discounts']);
        // Update Shipping
    } elseif (isset($_POST['calc_shipping']) && $_POST['calc_shipping'] && $woocommerce->verify_nonce('cart')) {
        $validation = $woocommerce->validation();
        $_SESSION['calculated_shipping'] = true;
        unset($_SESSION['_chosen_shipping_method']);
        $country = $_POST['calc_shipping_country'];
        $state = $_POST['calc_shipping_state'];
        $postcode = $_POST['calc_shipping_postcode'];
        if ($postcode && !$validation->is_postcode($postcode, $country)) {
            $woocommerce->add_error(__('Please enter a valid postcode/ZIP.', 'woocommerce'));
            $postcode = '';
        } elseif ($postcode) {
            $postcode = $validation->format_postcode($postcode, $country);
        }
        if ($country) {
            // Update customer location
            $woocommerce->customer->set_location($country, $state, $postcode);
            $woocommerce->customer->set_shipping_location($country, $state, $postcode);
            $woocommerce->add_message(__('Shipping costs updated.', 'woocommerce'));
        } else {
            $woocommerce->customer->set_to_base();
            $woocommerce->customer->set_shipping_to_base();
            $woocommerce->add_message(__('Shipping costs updated.', 'woocommerce'));
        }
    }
    // Calc totals
    $woocommerce->cart->calculate_totals();
    do_action('woocommerce_check_cart_items');
    if (sizeof($woocommerce->cart->get_cart()) == 0) {
        woocommerce_get_template('cart/empty.php');
    } else {
        woocommerce_get_template('cart/cart.php');
    }
}
/**
 * View Order Shortcode
 *
 * @package WooCommerce
 * @since 1.4
 */
function woocommerce_view_order()
{
    global $woocommerce;
    woocommerce_nocache();
    if (!is_user_logged_in()) {
        return;
    }
    $user_id = get_current_user_id();
    $order_id = isset($_GET['order']) ? $_GET['order'] : 0;
    $order = new WC_Order($order_id);
    if ($order_id == 0 || $order->user_id != $user_id) {
        echo '<div class="woocommerce_error">' . __('Invalid order.', 'woocommerce') . ' <a href="' . get_permalink(woocommerce_get_page_id('myaccount')) . '">' . __('My Account &rarr;', 'woocommerce') . '</a>' . '</div>';
        return;
    }
    $status = get_term_by('slug', $order->status, 'shop_order_status');
    echo '<p>' . sprintf(__('Order <mark>#%s</mark> made on <mark>%s</mark>', 'woocommerce'), $order->id, date_i18n(get_option('date_format'), strtotime($order->order_date))) . sprintf(__('. Order status: <mark>%s</mark>', 'woocommerce'), __($status->name, 'woocommerce')) . '.</p>';
    $notes = $order->get_customer_order_notes();
    if ($notes) {
        ?>
		<h2><?php 
        _e('Order Updates', 'woocommerce');
        ?>
</h2>
		<ol class="commentlist notes">	
			<?php 
        foreach ($notes as $note) {
            ?>
			<li class="comment note">
				<div class="comment_container">			
					<div class="comment-text">
						<p class="meta"><?php 
            echo date_i18n('l jS \\of F Y, h:ia', strtotime($note->comment_date));
            ?>
</p>
						<div class="description">
							<?php 
            echo wpautop(wptexturize($note->comment_content));
            ?>
						</div>
		  				<div class="clear"></div>
		  			</div>
					<div class="clear"></div>			
				</div>
			</li>
			<?php 
        }
        ?>
		</ol>
		<?php 
    }
    do_action('woocommerce_view_order', $order_id);
}
/**
 * Outputs the pay page - payment gateways can hook in here to show payment forms etc
 **/
function woocommerce_pay()
{
    global $woocommerce;
    woocommerce_nocache();
    do_action('before_woocommerce_pay');
    $woocommerce->show_messages();
    if (isset($_GET['pay_for_order']) && isset($_GET['order']) && isset($_GET['order_id'])) {
        // Pay for existing order
        $order_key = urldecode($_GET['order']);
        $order_id = (int) $_GET['order_id'];
        $order = new WC_Order($order_id);
        if ($order->id == $order_id && $order->order_key == $order_key && in_array($order->status, array('pending', 'failed'))) {
            // Set customer location to order location
            if ($order->billing_country) {
                $woocommerce->customer->set_country($order->billing_country);
            }
            if ($order->billing_state) {
                $woocommerce->customer->set_state($order->billing_state);
            }
            if ($order->billing_postcode) {
                $woocommerce->customer->set_postcode($order->billing_postcode);
            }
            // Show form
            woocommerce_get_template('checkout/form-pay.php', array('order' => $order));
        } elseif (!in_array($order->status, array('pending', 'failed'))) {
            $woocommerce->add_error(__('Your order has already been paid for. Please contact us if you need assistance.', 'woocommerce'));
            $woocommerce->show_messages();
        } else {
            $woocommerce->add_error(__('Invalid order.', 'woocommerce'));
            $woocommerce->show_messages();
        }
    } else {
        // Pay for order after checkout step
        if (isset($_GET['order'])) {
            $order_id = $_GET['order'];
        } else {
            $order_id = 0;
        }
        if (isset($_GET['key'])) {
            $order_key = $_GET['key'];
        } else {
            $order_key = '';
        }
        if ($order_id > 0) {
            $order = new WC_Order($order_id);
            if ($order->order_key == $order_key && in_array($order->status, array('pending', 'failed'))) {
                ?>
				<ul class="order_details">
					<li class="order">
						<?php 
                _e('Order:', 'woocommerce');
                ?>
						<strong># <?php 
                echo $order->id;
                ?>
</strong>
					</li>
					<li class="date">
						<?php 
                _e('Date:', 'woocommerce');
                ?>
						<strong><?php 
                echo date_i18n(get_option('date_format'), strtotime($order->order_date));
                ?>
</strong>
					</li>
					<li class="total">
						<?php 
                _e('Total:', 'woocommerce');
                ?>
						<strong><?php 
                echo woocommerce_price($order->order_total);
                ?>
</strong>
					</li>
					<li class="method">
						<?php 
                _e('Payment method:', 'woocommerce');
                ?>
						<strong><?php 
                echo $order->payment_method_title;
                ?>
</strong>
					</li>
				</ul>
				
				<?php 
                do_action('woocommerce_receipt_' . $order->payment_method, $order_id);
                ?>
				
				<div class="clear"></div>
				<?php 
            } elseif (!in_array($order->status, array('pending', 'failed'))) {
                $woocommerce->add_error(__('Your order has already been paid for. Please contact us if you need assistance.', 'woocommerce'));
                $woocommerce->show_messages();
            }
        } else {
            $woocommerce->add_error(__('Invalid order.', 'woocommerce'));
            $woocommerce->show_messages();
        }
    }
    do_action('after_woocommerce_pay');
}