<?php

defined('ABSPATH') or die("No direct script access!");

function qcld_express_cart_fragments( $fragments ) {
	global $woocommerce;

    ob_start();
    qcld_express_cart_count();
    $fragments['span.qcld_express_cart_total'] = ob_get_clean();

    ob_start();
    qcld_express_checkout_inner();
    $fragments['div.qcld-express-checkout-inner'] = ob_get_clean();
    
    return $fragments;
    
}
add_filter( 'woocommerce_add_to_cart_fragments', 'qcld_express_cart_fragments', 10, 1 );

/**
 * Cart Count function 
 */
if ( ! function_exists( 'qcld_express_cart_count' ) ) {
	function qcld_express_cart_count() { ?>		
		<span class="qcld_express_cart_total">
			<script type='text/javascript'>
			/* <![CDATA[ */
				var wiCartTotal = <?php echo WC()->cart->get_cart_contents_count(); ?>;
			/* ]]> */
			</script>
			<?php if ( WC()->cart->get_cart_contents_count() == 0 ) : ?>
				<style type="text/css" version="1.0">
					.qcld-express-container{
					    right: -50% !important;
					}
					.qcld-express-cart-header.hascart {
					    left: -70px !important;
					}
					@media (max-width: 767px){
					    .qcld-express-container{
					        right: -100% !important;
					    }
					}
				</style>
			<?php endif; ?>
			<?php echo WC()->cart->get_cart_contents_count(); ?>
		</span> <?php
	}
}

/**
 * Cart Area function
 */
if ( ! function_exists( 'qcld_express_cart_inner' ) ) {
	function qcld_express_cart_inner() { 
		include plugin_dir_path( __FILE__ ) . '/inc/cart/cart.php';
	}
}

/**
 * Checkout Area function
 */
if ( ! function_exists( 'qcld_express_checkout_inner' ) ) {
	function qcld_express_checkout_inner() { ?>
		<div class="qcld-express-checkout-inner">
			<button type="button" class="button alt" id="back_to_cart"><?php echo esc_attr('Back to cart','express-shop'); ?></button>
			<?php echo do_shortcode('[woocommerce_checkout]');  ?>
		</div> <?php
	}
}

/**
 * Add wooinstant-active class to body
 */
function qcld_express_body_classes( $classes ) {
	$classes[] = 'wooinstant-active';
	return $classes;
}
add_filter( 'body_class', 'qcld_express_body_classes' );

/**
 * Calculate Shipping on Update Order Review
 */
function qcld_express_action_woocommerce_checkout_update_order_review( $posted_data ) {
    WC()->cart->calculate_shipping();
}
add_action( 'woocommerce_checkout_update_order_review', 'qcld_express_action_woocommerce_checkout_update_order_review', 10, 1 );


// single product ajax add to cart actions
add_action('wp_ajax_qcld_express_single_ajax_add_to_cart', 'qcld_express_single_ajax_add_to_cart');
add_action('wp_ajax_nopriv_qcld_express_single_ajax_add_to_cart', 'qcld_express_single_ajax_add_to_cart');

// single product ajax add to cart actions
function qcld_express_single_ajax_add_to_cart() {

	check_ajax_referer( 'qcld_express_security_nonce', 'security');

    $product_id = apply_filters('woocommerce_add_to_cart_product_id', absint($_POST['product_id']));
    $quantity = empty($_POST['quantity']) ? 1 : wc_stock_amount($_POST['quantity']);
    $variation_id = absint($_POST['variation_id']);
    $passed_validation = apply_filters('woocommerce_add_to_cart_validation', true, $product_id, $quantity);
    $product_status = get_post_status($product_id);

    if ($passed_validation && WC()->cart->add_to_cart($product_id, $quantity, $variation_id) && 'publish' === $product_status) {

        do_action('woocommerce_ajax_added_to_cart', $product_id);

        if ('yes' === get_option('woocommerce_cart_redirect_after_add')) {
            wc_add_to_cart_message(array($product_id => $quantity), true);
        }

        WC_AJAX :: get_refreshed_fragments();
    } else {

        $data = array(
            'error' => true,
            'product_url' => apply_filters('woocommerce_cart_redirect_after_error', get_permalink($product_id), $product_id));

        echo wp_send_json($data);
    }

    wp_die();
}
// single product ajax add to cart actions
add_action('wp_ajax_qcld_express_car_count_update', 'qcld_express_car_count_update');
add_action('wp_ajax_nopriv_qcld_express_car_count_update', 'qcld_express_car_count_update');

// single product ajax add to cart actions
function qcld_express_car_count_update() {

	check_ajax_referer( 'qcld_express_security_nonce', 'security');

    $data =WC()->cart->get_cart_contents_count();

    echo wp_send_json($data);
    
    wp_die();
}