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

//variation for variable product
add_action( 'qcld_express_product_variation', 'woocommerce_template_single_price');
add_action( 'qcld_express_product_variation', 'woocommerce_template_single_add_to_cart');

add_action('wp_ajax_get_vp_attributes', 'qcld_express_get_v_product');
add_action('wp_ajax_nopriv_get_vp_attributes', 'qcld_express_get_v_product');

function qcld_express_get_v_product(){

    check_ajax_referer( 'qcld_express_security_nonce', 'security');

    $p_id = isset($_POST['p_id']) ? trim(sanitize_text_field($_POST['p_id'])) : '';
    $inStock = "<strong>'".esc_html('InStock', 'express-shop')."'</strong>";
    //Woocommerce product factory
    $wc_pf = new WC_Product_Factory();
    $product = $wc_pf->get_product($p_id);

    ob_start();
    ?>
    <div class="qcld-express-popup-table">
        <div class="qcld-express-popup-row">
            <div class="qcld-express-popup-col-12">
                <h2 id="qcld_epress_product_title"><?php echo  esc_html($product->get_title());?></h2>
            </div>
        </div>
        <div class="qcld-express-popup-row">
            <div class="qcld-express-popup-col-12">
                <?php
                global $woocommerce;

                $suffix      = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min';

                global $post;
                if(intval($p_id)) {

                    wp('p=' . $p_id . '&post_type=product');
                    ob_start();


                    while (have_posts()) : the_post(); ?>
                        <script>
                         
                            var wc_add_to_cart_variation_params = {"ajax_url": "\/wp-admin\/admin-ajax.php"};
                            jQuery.getScript("<?php echo $woocommerce->plugin_url(); ?>/assets/js/frontend/add-to-cart-variation.min.js");
                          
                        </script>
                        <div class="express-shop-product-variation-container">

                            <?php

                            do_action('qcld_express_product_variation');

                            ?>
                        </div>

                    <?php endwhile;
                }
                ?>
            </div>
        </div>
    </div>
    <?php
    echo  ob_get_clean();
    exit();
}

//Add to cart for simple product.
add_action('wp_ajax_sp_add_to_cart', 'qcld_express_sp_add_to_cart');
add_action('wp_ajax_nopriv_sp_add_to_cart', 'qcld_express_sp_add_to_cart');


function qcld_express_sp_add_to_cart(){

    check_ajax_referer( 'qcld_express_security_nonce', 'security');
    $product_id = isset($_POST['p_id']) ? sanitize_text_field($_POST['p_id']) : '';
    $quantity   = isset($_POST['quantity']) ? sanitize_text_field($_POST['quantity']) : '';

    $product_id = apply_filters( 'woocommerce_add_to_cart_product_id', absint( $product_id ) );
    $product_quantity = empty( $quantity ) ? 1 : apply_filters( 'woocommerce_stock_amount', $quantity );
    global $woocommerce;
    $result = $woocommerce->cart->add_to_cart($product_id,$product_quantity);
    if ($result != false) {
        echo wp_send_json('simple');
    } else {
        echo wp_send_json('error');
    }
    wp_die();

}

//Add to cart for variable product.
add_action('wp_ajax_vp_add_to_cart', 'qcld_express_vp_add_to_cart');
add_action('wp_ajax_nopriv_vp_add_to_cart', 'qcld_express_vp_add_to_cart');

function qcld_express_vp_add_to_cart(){

    check_ajax_referer( 'qcld_express_security_nonce', 'security');

    ob_start();
    $pro_id         = isset($_POST['product_id']) ? sanitize_text_field($_POST['product_id']) : '';
    $quantity       = isset($_POST['quantity']) ? sanitize_text_field($_POST['quantity']) : '';
    $variation_id   = isset($_POST['variation_id']) ? sanitize_text_field($_POST['variation_id']) : '';
    $variation      = isset($_POST['variation']) ? sanitize_text_field($_POST['variation']) : '';

    $product_id = apply_filters( 'woocommerce_add_to_cart_product_id', absint( $pro_id ) );
    $quantity = empty( $quantity ) ? 1 : apply_filters( 'woocommerce_stock_amount', $quantity );

    $passed_validation = apply_filters( 'woocommerce_add_to_cart_validation', true, $product_id, $quantity );

    if ( $passed_validation && WC()->cart->add_to_cart( $product_id, $quantity, $variation_id, $variation  ) ) {
        do_action( 'woocommerce_ajax_added_to_cart', $product_id );
        if ( get_option( 'woocommerce_cart_redirect_after_add' ) == 'yes' ) {
            wc_add_to_cart_message( $product_id );
        }

        // Return fragments
        WC_AJAX::get_refreshed_fragments();
    } else {
        $this->json_headers();

        // If there was an error adding to the cart, redirect to the product page to show any errors
        $data = array(
            'error' => true,
            'product_url' => apply_filters( 'woocommerce_cart_redirect_after_error', get_permalink( $product_id ), $product_id )
            );
        echo json_encode( $data );
    }

    wp_die();
}


function qcld_express_get_id_by_shortcode($shortcode) {
    global $wpdb;
    $sql = 'SELECT ID
        FROM ' . $wpdb->posts . '
        WHERE post_type = "page"
            AND post_status="publish"
            AND post_content LIKE "%' . $shortcode . '%" limit 1';

    $id = $wpdb->get_var($sql);

    return $id;
}

//Show cart after adding product to cart.
add_action('wp_ajax_show_cart', 'qcld_express_show_cart');
add_action('wp_ajax_nopriv_show_cart', 'qcld_express_show_cart');
function qcld_express_show_cart(){

    check_ajax_referer( 'qcld_express_security_nonce', 'security');

    // global $woocommerce;
    $qcld_express_options           = get_option('qcld_express_options');
    $qcld_express_cart_mode         = isset($qcld_express_options['qcld_express_cart_mode']) ? $qcld_express_options['qcld_express_cart_mode'] : '';
    
    $qcld_express_lang_title        = isset($qcld_express_options['qcld_express_lang_title']) ? $qcld_express_options['qcld_express_lang_title'] : '';
    $qcld_express_lang_quantity     = isset($qcld_express_options['qcld_express_lang_quantity']) ? $qcld_express_options['qcld_express_lang_quantity'] : '';
    $qcld_express_lang_price        = isset($qcld_express_options['qcld_express_lang_price']) ? $qcld_express_options['qcld_express_lang_price'] : '';
    $qcld_express_lang_no_products  = isset($qcld_express_options['qcld_express_lang_no_products']) ? $qcld_express_options['qcld_express_lang_no_products'] : '';
    $qcld_express_lang_cart_total   = isset($qcld_express_options['qcld_express_lang_cart_total']) ? $qcld_express_options['qcld_express_lang_cart_total'] : '';
    $qcld_express_lang_items        = isset($qcld_express_options['qcld_express_lang_items']) ? $qcld_express_options['qcld_express_lang_items'] : '';

    $cart_mode = $qcld_express_cart_mode;
    $html = '';
    global $woocommerce;
    $items = array();
    if ( ! is_admin() ){
        $items = $woocommerce->cart->get_cart();
    }
    $itemCount = count($items);
    if ($itemCount >= 1) {
        $html .= '<div class ="qcld-express-table">';
        $html .= '<div class="qcld-express-row"><div class="qcld-express-cell">'.esc_html('Title', 'express-shop').'</div><div class="qcld-express-cell">'.esc_html('Price', 'express-shop').'</div></div>';
        foreach ($items as $item => $values) {
            //$_product = $values['data']->post;
            $_product = apply_filters('woocommerce_cart_item_product', $values['data'], $values, $item);
            //product image
            $getProductDetail = wc_get_product($values['product_id']);
            $price = get_post_meta($values['product_id'], '_price', true);

            $html .= '<div class="qcld-express-row">
                        <div class="qcld-express-cell qcld-express-title-cell"> <h3 class="qcld-express-title">';
                        
                        $product_permalink = apply_filters( 'woocommerce_cart_item_permalink', $_product->is_visible() ? $_product->get_permalink( $values ) : '', $values, $item );
                        $product_name =  apply_filters( 'woocommerce_cart_item_name', sprintf( '<a href="%s">%s</a>', esc_url( $product_permalink ), $_product->get_title() ), $values, $item );

                        $html .= '<p>'.$product_name.'</p>';

                        $attributes = '';

                        //Variation
                        $attributes .= $_product->is_type('variable') || $_product->is_type('variation') ? wc_get_formatted_variation($_product) : '';
                       

                        if($attributes){
                           $html .= $attributes;
                        }

                           
                        $html.='</h3></div>';
            $html .= '<div class="qcld-express-cell qcld-express-price-cell"><span>' . apply_filters('woocommerce_cart_item_price', WC()->cart->get_product_price($_product), $values, $item) . '</span> </div> </div>';


        }
        $html.='<div class="qcld-express-row">
                            
                            <div class="qcld-express-cell"><strong>Total</strong></div>
                            <div class="qcld-express-cell"><strong>'.$woocommerce->cart->get_cart_total().'</strong></div>
                        </div>';
        $html .= ' </div>';
    } else {
        $html .= '<div class="qcld_no_cartprods">'.esc_html('No products in the cart', 'express-shop').'</div>';
    }
    $response=array('html'=>$html,'items'=>$itemCount,'cart_mode' => $cart_mode);
    echo wp_send_json($response);
    wp_die();
}


add_action('wp_ajax_load_more', 'qcld_express_load_more');
add_action('wp_ajax_nopriv_load_more', 'qcld_express_load_more');

function qcld_express_load_more(){

    check_ajax_referer( 'qcld_express_security_nonce', 'security');

    //Getting option tree from backend.
    $qcld_express_options = get_option('qcld_express_options');
    $qcld_express_show_mode = isset($qcld_express_options['qcld_express_show_mode']) ? $qcld_express_options['qcld_express_show_mode'] : '';
    $qcld_express_cart_mode = isset($qcld_express_options['qcld_express_cart_mode']) ? $qcld_express_options['qcld_express_cart_mode'] : '';
    $qcld_express_sku = isset($qcld_express_options['qcld_express_sku']) ? $qcld_express_options['qcld_express_sku'] : '';
    $qcld_express_thumbnail = isset($qcld_express_options['qcld_express_thumbnail']) ? $qcld_express_options['qcld_express_thumbnail'] : '';
    $qcld_express_title = isset($qcld_express_options['qcld_express_title']) ? $qcld_express_options['qcld_express_title'] : '';
    $qcld_express_description = isset($qcld_express_options['qcld_express_description']) ? $qcld_express_options['qcld_express_description'] : '';
    $qcld_express_price = isset($qcld_express_options['qcld_express_price']) ? $qcld_express_options['qcld_express_price'] : '';
    $qcld_express_stock = isset($qcld_express_options['qcld_express_stock']) ? $qcld_express_options['qcld_express_stock'] : '';
    $qcld_express_stock_alert = isset($qcld_express_options['qcld_express_stock_alert']) ? $qcld_express_options['qcld_express_stock_alert'] : '';
    $qcld_express_action = isset($qcld_express_options['qcld_express_action']) ? $qcld_express_options['qcld_express_action'] : '';
    $qcld_express_background = isset($qcld_express_options['qcld_express_background']) ? $qcld_express_options['qcld_express_background'] : '';
    $qcld_express_altern = isset($qcld_express_options['qcld_express_altern']) ? $qcld_express_options['qcld_express_altern'] : '';
    $qcld_express_custom_style = isset($qcld_express_options['qcld_express_custom_style']) ? $qcld_express_options['qcld_express_custom_style'] : '';

    // sorting variables
    $qcld_express_details_button    = '';
    $qcld_express_product_sorting   = isset($qcld_express_options['qcld_express_product_sorting']) ? $qcld_express_options['qcld_express_product_sorting'] : '';
    $qcld_express_product_asc_desc  = isset($qcld_express_options['qcld_express_product_asc_desc']) ? $qcld_express_options['qcld_express_product_asc_desc'] : '';


    if (isset($qcld_express_options['qcld_express_category'])) {
        $qcld_express_categories = $qcld_express_options['qcld_express_category'];
    } else {
        $qcld_express_categories = array();
    }
    $qcld_express_template = isset($qcld_express_options['qcld_express_template']) ? $qcld_express_options['qcld_express_template'] : '';

    if(isset($qcld_express_options['qcld_express_product_per_page']) && !empty($qcld_express_options['qcld_express_product_per_page'])) {
        $qcld_express_product_per_page = isset($qcld_express_options['qcld_express_product_per_page']) ? $qcld_express_options['qcld_express_product_per_page'] : '';
    }else{
        $qcld_express_product_per_page = -1;
    }

    //Now filtering the categories to pass to query.
    $terms = get_terms(array(
        'taxonomy' => 'product_cat',
        'hide_empty' => false,
    ));
    $categories = array();
    foreach ($terms as $term) {
        if (!in_array($term->term_id, $qcld_express_categories)) {
            array_push($categories, $term->term_id);
        }
    }
    //$per_page = 15;
    $per_page = $qcld_express_product_per_page;
    //$per_page = $qcld_express_product_per_page;
    $offset = isset($_POST['offset']) ? sanitize_text_field($_POST['offset']) : '';
    $author = isset($_POST['author']) ? sanitize_text_field($_POST['author']) : '';

    //pulling up the product accroding to the query
    // Get WooCommerce Global from Qcld_woo_inc class.
    global $woocommerce;
    // Create query arguments array


    $query_args = array(
            'post_type'         => 'product',
            'post_status'       => 'publish',
            'posts_per_page'    => $per_page,
            'offset'            => $offset,
            'author'            => $author,
            'orderby'           => $qcld_express_product_sorting,
            'order'             => $qcld_express_product_asc_desc,
            'tax_query' => array(
                array(
                    'taxonomy' => 'product_cat',
                    'field'    => 'term_id',
                    'terms'    => $categories,
                ),
            ),
        );     

       // Add meta_query to query args
    $query_args['meta_query'] = array();

    // Check products stock status
    $query_args['meta_query'][] = $woocommerce->query->stock_status_meta_query();

    if(isset($_POST['c_id'] )){
        $category = sanitize_text_field($_POST['c_id']);
        if($category == 'all'){

            $query_args = array(
                'post_type'         =>  array('product', 'product_variation'),
                'post_status'       =>  'publish',
                'posts_per_page'    =>  $per_page,
                'offset'            =>  $offset,
                'author'            => $author,
                'orderby'           =>  $qcld_express_product_sorting,
                'order'             =>  $qcld_express_product_asc_desc,
                'tax_query'         =>  array(
                                            array(
                                                'taxonomy' => 'product_cat',
                                                'field'    => 'term_id',
                                                'terms'    => $categories,
                                            ),
                                        ),
                
                                );

        }else{
           // $category_sort = array('product_cat' => $category);
            //$query_args=array_merge($query_args,$category_sort);

            $category_sort = array(
                'tax_query' => array(
                    'relation' => 'OR',
                    array(
                        'taxonomy'  => 'product_cat',
                        'field'     => 'term_id',
                        'terms'     => $category,
                        'operator'  => 'IN'
                    ),
                )
            );

            $query_args=array_merge($query_args,$category_sort);


        }
    }


 

    //Calculating nextoffset to show button or not.
    $products = get_posts($query_args);
    $products_num = count($products);
    if ($products_num == $per_page) {
        $nextOffset = intval($offset + $per_page);
    } else {
        $nextOffset = -1;
    }
    //Creating WooCommerce product factory.
    $wc_pf = new WC_Product_Factory();
    $html = '';
    $counter = 0;
    $qcld_express_slope_type = 'left';
    foreach ($products as $P) {
        $product = $wc_pf->get_product($P->ID);
        $getProductDetail = wc_get_product($P->ID);
        //Checking for image thumb.
        $qcld_thumb = wp_get_attachment_image_src(get_post_thumbnail_id($P->ID), 'shop_thumbnail');
        if (!empty($qcld_thumb)) {
            $image = $qcld_thumb;
        } else {
            $image[0] = QCLD_EXPRESS_IMG_URL . '/placeholder.png';
        }
        if ($counter % 2 == 0 && $qcld_express_template == "slope") {
            if ($qcld_express_slope_type == 'left') {
                $qcld_express_extra_class = "left-slope";
                $qcld_express_slope_type = 'right';
            } else {
                $qcld_express_extra_class = "right-slope";
                $qcld_express_slope_type = 'left';
            }

        } else {
            $qcld_express_extra_class = "";
        }
        $html .= '<div class="qcld-express-row   qcld-express-body-content ' . $qcld_express_extra_class . '">';
        //SKu show or not
        if (isset($qcld_express_sku) && $qcld_express_sku == 'on') {
            $html .= '<div class="qcld-express-cell qcld-express-sku-cell"><p class="qcld-express-sku">' . esc_html($product->get_sku(), 'express-shop') . '</p></div>';
        }
        //Thumbnail show or not
        if (isset($qcld_express_thumbnail) && $qcld_express_thumbnail == 'on') {
            $html .= '<div class="qcld-express-cell qcld-express-thumb-cell"><p class="qcld-express-thumbnail"><img class="qlcd-express-list-thumb" src="' . esc_url($image[0], 'express-shop') . '"  alt="' . esc_attr(get_the_title($P->ID), 'express-shop') . '"/></p></div>';
        }
        //Title show or not
        if (isset($qcld_express_title) && $qcld_express_title == 'on') {
            $html .= '<div class="qcld-express-cell qcld-express-title-cell"><h3 class="qcld-express-title"><a href="' . esc_url(get_permalink($P->ID), 'express-shop') . '">' . esc_html(get_the_title($P->ID), 'express-shop') . '</a></p></div>';
        }
        //Description show or not
        if (isset($qcld_express_description) && $qcld_express_description == 'on') {
            $html .= '<div class="qcld-express-cell qcld-express-desc-cell"><p class="qcld-express-desc">' . strip_tags(substr(get_the_excerpt($P->ID), 0, 100)) . ' ... </p></div>';
        }
        //Availability show or not

        //Availability show or not
        if (isset($qcld_express_stock) && $qcld_express_stock == 'on') {
            $stock = get_post_meta( get_the_ID(), '_stock', true );
            $html .= '<div class="qcld-express-cell qcld-express-avail-cell"><p class="qcld-express-avail">';
           
            if ($stock > 0) {
                $html .= '<span class="qcld-express-avail-in"> In Stock (' . $product->get_stock_quantity() . ')</span>';
            } else if($stock=='') {
                $stock_status = get_post_meta( $P->ID, '_stock_status', true );
                if( $stock_status == "instock" ){ 
                    $html .= '<span class="qcld-express-avail-out"> '.esc_html('In Stock', 'express-shop').' </span>';
                }else if( $stock_status == "outofstock" ){
                    $html .= '<span class="qcld-express-avail-outs"> '.esc_html('Out of Stock', 'express-shop').' </span>';
                } 
            }else if($stock<0) {
                $html .= '<span class="qcld-express-avail-out"> '.esc_html('Out of Stock', 'express-shop').'</span>';
            }
            $html .= '</p></div>';
        }



        //Price show or not
        if (isset($qcld_express_price) && $qcld_express_price == 'on') {
            $html .= '<div class="qcld-express-cell qcld-express-price-cell"><p class="qcld-express-price">' . $product->get_price_html() . '</p></div>';
        }
        // Action show or not
        if (isset($qcld_express_action) && $qcld_express_action == 'on') {
            $html .= '<div class="qcld-express-cell qcld-express-action-cell"><ul class="qcld-express-action">';
          
             if ($product->get_type() == 'simple') {
                

              $stock = get_post_meta( $P->ID, '_stock', true );
              $stock_status = get_post_meta( $P->ID, '_stock_status', true );
              if( $stock > 0 ){

                if (isset($qcld_express_cart_mode) && $qcld_express_cart_mode == 'wooinstants') { 


                $qcld_woo_minmax_enable_plugin = get_option( 'qcld_woo_minmax_enable_plugin' );
                if(function_exists('qcld_woo_minmax_install') && isset($qcld_woo_minmax_enable_plugin) && $qcld_woo_minmax_enable_plugin  == 'yes'){

                    $response_min_max = qcld_woo_minmax_express_add_rules_text($P->ID,0);

                    $min_qty  = $response_min_max['min'];
                    $max_qty  = $response_min_max['max'];
                    $step_qty = $response_min_max['step'];
    
                    $html.='<li><input type="number" value="'.($min_qty ? $min_qty : 1).'" step="'.($step_qty ? $step_qty : '').'" min="'.($min_qty ? $min_qty : '').'" max="'.($max_qty ? $max_qty : '').'" name="qcld_quantity" class="qcld-express-simple-p-qnty"></li>';


                }else{
                $html.='<li><input type="number" value="1" name="qcld_quantity" class="qcld-express-simple-p-qnty"></li>';
                } 

                        $qcld_woo_minmax_enable_plugin = get_option( 'qcld_woo_minmax_enable_plugin' );
                        if(function_exists('qcld_woo_minmax_install') && isset($qcld_woo_minmax_enable_plugin) && $qcld_woo_minmax_enable_plugin  == 'yes'){

                            $response_min_max = qcld_woo_minmax_express_add_rules_text($P->ID,0);
                            $min_qty  = $response_min_max['min'];
                          }else{
                            $min_qty  = 1;

                          }

                  $html .= ' <li><a  href="#"  class="qcld_express_s_p ajax_add_to_cart add_to_cart_button product_type_simple" data-quantity="'.($min_qty ? $min_qty : 1).'" data-product_id="' . esc_attr($P->ID, 'express-shop') . '" p-id="' . esc_attr($P->ID, 'express-shop') . '"p-price="' . esc_attr($product->get_price(), 'express-shop') . '" ><i class="fa fa-shopping-cart"></i></a></li>';
                }else{
                  $html.='<li><input type="number" value="1" name="qcld_quantity" class="qcld-express-simple-p-qnty"  min="1"></li>';
                  $html .= ' <li><a  href="#"  class="qcld_express_s_p" p-id="' . esc_attr($P->ID, 'express-shop') . '"p-price="' . esc_attr($product->get_price(), 'express-shop') . '" ><i class="fa fa-shopping-cart"></i></a></li>';
                }

               } else if( $stock == "" ){
               if( $stock_status == "instock" ){

                    if (isset($qcld_express_cart_mode) && $qcld_express_cart_mode == 'wooinstants') { 
            
                $qcld_woo_minmax_enable_plugin = get_option( 'qcld_woo_minmax_enable_plugin' );
                if(function_exists('qcld_woo_minmax_install') && isset($qcld_woo_minmax_enable_plugin) && $qcld_woo_minmax_enable_plugin  == 'yes'){

                    $response_min_max = qcld_woo_minmax_express_add_rules_text($P->ID,0);

                    $min_qty  = $response_min_max['min'];
                    $max_qty  = $response_min_max['max'];
                    $step_qty = $response_min_max['step'];
    
                    $html.='<li><input type="number" value="'.($min_qty ? $min_qty : 1).'" step="'.($step_qty ? $step_qty : '').'" min="'.($min_qty ? $min_qty : '').'" max="'.($max_qty ? $max_qty : '').'" name="qcld_quantity" class="qcld-express-simple-p-qnty"></li>';


                }else{
                $html.='<li><input type="number" value="1" name="qcld_quantity" class="qcld-express-simple-p-qnty"></li>';
                } 


                        $qcld_woo_minmax_enable_plugin = get_option( 'qcld_woo_minmax_enable_plugin' );
                        if(function_exists('qcld_woo_minmax_install') && isset($qcld_woo_minmax_enable_plugin) && $qcld_woo_minmax_enable_plugin  == 'yes'){

                            $response_min_max = qcld_woo_minmax_express_add_rules_text($P->ID,0);
                            $min_qty  = $response_min_max['min'];
                          }else{
                            $min_qty  = 1;

                          }
                      $html .= ' <li><a  href="#"  class="qcld_express_s_p ajax_add_to_cart add_to_cart_button product_type_simple" data-quantity="'.($min_qty ? $min_qty : 1).'" data-product_id="' . esc_attr($P->ID, 'express-shop') . '" p-id="' . esc_attr($P->ID, 'express-shop') . '"p-price="' . esc_attr($product->get_price(), 'express-shop') . '" ><i class="fa fa-shopping-cart"></i></a></li>';
                    }else{
                      $html.='<li><input type="number" value="1" name="qcld_quantity" class="qcld-express-simple-p-qnty"  min="1"></li>';
                      $html .= ' <li><a  href="#"  class="qcld_express_s_p" p-id="' . esc_attr($P->ID, 'express-shop') . '"p-price="' . esc_attr($product->get_price(), 'express-shop') . '" ><i class="fa fa-shopping-cart"></i></a></li>';
                    }

                } else if( $stock_status == "outofstock" ){ 
                    $html .= ' <li><a href="#" class="qcld_express_v_p qcld_express_config" p-id="' . esc_attr($P->ID, 'express-shop') . '"p-price="' . esc_attr($product->get_price(), 'express-shop') . '" > <i class="fa fa-shopping-cart"></i> </a></li>';

                 } 
                }else if( $stock < 1 ){ 
                   

                    if (isset($qcld_express_cart_mode) && $qcld_express_cart_mode == 'wooinstants') { 
            
                $qcld_woo_minmax_enable_plugin = get_option( 'qcld_woo_minmax_enable_plugin' );
                if(function_exists('qcld_woo_minmax_install') && isset($qcld_woo_minmax_enable_plugin) && $qcld_woo_minmax_enable_plugin  == 'yes'){

                    $response_min_max = qcld_woo_minmax_express_add_rules_text($P->ID,0);

                    $min_qty  = $response_min_max['min'];
                    $max_qty  = $response_min_max['max'];
                    $step_qty = $response_min_max['step'];
    
                    $html.='<li><input type="number" value="'.($min_qty ? $min_qty : 1).'" step="'.($step_qty ? $step_qty : '').'" min="'.($min_qty ? $min_qty : '').'" max="'.($max_qty ? $max_qty : '').'" name="qcld_quantity" class="qcld-express-simple-p-qnty"></li>';


                }else{
                $html.='<li><input type="number" value="1" name="qcld_quantity" class="qcld-express-simple-p-qnty"></li>';
                } 
                        $qcld_woo_minmax_enable_plugin = get_option( 'qcld_woo_minmax_enable_plugin' );
                        if(function_exists('qcld_woo_minmax_install') && isset($qcld_woo_minmax_enable_plugin) && $qcld_woo_minmax_enable_plugin  == 'yes'){

                            $response_min_max = qcld_woo_minmax_express_add_rules_text($P->ID,0);
                            $min_qty  = $response_min_max['min'];
                          }else{
                            $min_qty  = 1;

                          }
                      $html .= ' <li><a  href="#"  class="qcld_express_s_p ajax_add_to_cart add_to_cart_button product_type_simple" data-quantity="'.($min_qty ? $min_qty : 1).'" data-product_id="' . esc_attr($P->ID, 'express-shop') . '" p-id="' . esc_attr($P->ID, 'express-shop') . '"p-price="' . esc_attr($product->get_price(), 'express-shop') . '" ><i class="fa fa-shopping-cart"></i></a></li>';
                    }else{
                      $html.='<li><input type="number" value="1" name="qcld_quantity" class="qcld-express-simple-p-qnty"  min="1"></li>';
                      $html .= ' <li><a  href="#"  class="qcld_express_s_p" p-id="' . esc_attr($P->ID, 'express-shop') . '"p-price="' . esc_attr($product->get_price(), 'express-shop') . '" ><i class="fa fa-shopping-cart"></i></a></li>';
                    }

                } 


            }
            if ($product->get_type() == 'variable') {
                

              $stock = get_post_meta( $P->ID, '_stock', true );
              $stock_status = get_post_meta( $P->ID, '_stock_status', true );
              if( $stock > 0 ){
              $html .= ' <li><a href="#" class="qcld_express_v_p" p-id="' . esc_attr($P->ID, 'express-shop') . '"><i class="fa fa-shopping-cart"></i></a></li>';

               } else if( $stock == "" ){
               if( $stock_status == "instock" ){
                    $html .= ' <li><a href="#" class="qcld_express_v_p" p-id="' . esc_attr($P->ID, 'express-shop') . '"><i class="fa fa-shopping-cart"></i></a></li>';

                } else if( $stock_status == "outofstock" ){ 
                    $html .= ' <li><a href="#" class="qcld_express_v_p qcld_express_config" p-id="' . esc_attr($P->ID, 'express-shop') . '"><i class="fa fa-shopping-cart"></i></a></li>';

                 } 
                }else if( $stock < 1 ){ 
                    $html .= ' <li><a href="#" class="qcld_express_v_p" p-id="' . esc_attr($P->ID, 'express-shop') . '"><i class="fa fa-shopping-cart"></i></a></li>';

                } 

            }


            $html .= '</ul>';

            $qcld_woo_minmax_dynamic_pricing_setting = get_option('qcld_woo_minmax_dynamic_pricing_setting');

            if(function_exists('qcld_woo_minmax_install') && (isset($qcld_woo_minmax_dynamic_pricing_setting['qcld_woo_minmax_price_table_on_off']) && $qcld_woo_minmax_dynamic_pricing_setting['qcld_woo_minmax_price_table_on_off'] == 'enable') ){

              $result_show = qcld_woo_minmax_express_dynamic_discount_price_table_show($P->ID);
              $html .= $result_show;
            }
            $html .= '</div>';
        }//End of show or not
        $html .= '</div>';
        $counter++;
    } //End of loop
    $output = array('offset' => $nextOffset, 'html' => $html);
    echo wp_send_json($output);


}


// Product Search by category .....

add_action('wp_ajax_product_search_by_cat', 'qcld_express_product_search_by_cat');
add_action('wp_ajax_nopriv_product_search_by_cat', 'qcld_express_product_search_by_cat');

function qcld_express_product_search_by_cat(){

    check_ajax_referer( 'qcld_express_security_nonce', 'security');

    global $woocommerce;

   // Getting option tree from backend.
    $qcld_express_options       = get_option('qcld_express_options');
    $qcld_express_show_mode     = isset($qcld_express_options['qcld_express_show_mode']) ? $qcld_express_options['qcld_express_show_mode'] : '';
    $qcld_express_cart_mode     = isset($qcld_express_options['qcld_express_cart_mode']) ? $qcld_express_options['qcld_express_cart_mode'] : '';
    $qcld_express_sku           = isset($qcld_express_options['qcld_express_sku']) ? $qcld_express_options['qcld_express_sku'] : '';
    $qcld_express_thumbnail     = isset($qcld_express_options['qcld_express_thumbnail']) ? $qcld_express_options['qcld_express_thumbnail'] : '';
    $qcld_express_thumbnails    = isset($qcld_express_options['qcld_express_thumbnail']) ? $qcld_express_options['qcld_express_thumbnail'] : '';
    $qcld_express_title         = isset($qcld_express_options['qcld_express_title']) ? $qcld_express_options['qcld_express_title'] : '';
    $qcld_express_description   = isset($qcld_express_options['qcld_express_description']) ? $qcld_express_options['qcld_express_description'] : '';
    $qcld_express_price         = isset($qcld_express_options['qcld_express_price']) ? $qcld_express_options['qcld_express_price'] : '';
    $qcld_express_stock         = isset($qcld_express_options['qcld_express_stock']) ? $qcld_express_options['qcld_express_stock'] : '';
    $qcld_express_stock_alert   = isset($qcld_express_options['qcld_express_stock_alert']) ? $qcld_express_options['qcld_express_stock_alert'] : '';
    $qcld_express_action        = isset($qcld_express_options['qcld_express_action']) ? $qcld_express_options['qcld_express_action'] : '';
    $qcld_express_background    = isset($qcld_express_options['qcld_express_background']) ? $qcld_express_options['qcld_express_background'] : '';
    $qcld_express_altern        = isset($qcld_express_options['qcld_express_altern']) ? $qcld_express_options['qcld_express_altern'] : '';
    $qcld_express_custom_style  = isset($qcld_express_options['qcld_express_custom_style']) ? $qcld_express_options['qcld_express_custom_style'] : '';

    // Language Change option ... 
    $qcld_express_lang_sku          = isset($qcld_express_options['qcld_express_lang_sku']) ? $qcld_express_options['qcld_express_lang_sku'] : '';
    $qcld_express_lang_thumbnail    = isset($qcld_express_options['qcld_express_lang_thumbnail']) ? $qcld_express_options['qcld_express_lang_thumbnail'] : '';
    $qcld_express_lang_title        = isset($qcld_express_options['qcld_express_lang_title']) ? $qcld_express_options['qcld_express_lang_title'] : '';
    $qcld_express_lang_desc         = isset($qcld_express_options['qcld_express_lang_desc']) ? $qcld_express_options['qcld_express_lang_desc'] : '';
    $qcld_express_lang_availability = isset($qcld_express_options['qcld_express_lang_availability']) ? $qcld_express_options['qcld_express_lang_availability'] : '';
    $qcld_express_lang_price        = isset($qcld_express_options['qcld_express_lang_price']) ? $qcld_express_options['qcld_express_lang_price'] : '';
    $qcld_express_lang_action       = isset($qcld_express_options['qcld_express_lang_action']) ? $qcld_express_options['qcld_express_lang_action'] : '';
    $qcld_express_lang_search       = isset($qcld_express_options['qcld_express_lang_search']) ? $qcld_express_options['qcld_express_lang_search'] : '';
    $qcld_express_lang_cart_summary = isset($qcld_express_options['qcld_express_lang_cart_summary']) ? $qcld_express_options['qcld_express_lang_cart_summary'] : '';
    $qcld_express_lang_edit_on_cart = isset($qcld_express_options['qcld_express_lang_edit_on_cart']) ? $qcld_express_options['qcld_express_lang_edit_on_cart'] : '';
    $qcld_express_lang_checkout     = isset($qcld_express_options['qcld_express_lang_checkout']) ? $qcld_express_options['qcld_express_lang_checkout'] : '';
    $qcld_express_lang_no_products  = isset($qcld_express_options['qcld_express_lang_no_products']) ? $qcld_express_options['qcld_express_lang_no_products'] : '';

        // sorting variables
    $qcld_express_details_button    = '';
    $qcld_express_product_sorting   = isset($qcld_express_options['qcld_express_product_sorting']) ? $qcld_express_options['qcld_express_product_sorting']: '';
    $qcld_express_product_asc_desc  = isset($qcld_express_options['qcld_express_product_asc_desc']) ? $qcld_express_options['qcld_express_product_asc_desc']: '';

    $qcld_express_template = isset($qcld_express_options['qcld_express_template']) ? $qcld_express_options['qcld_express_template'] : '';

    if (isset($qcld_express_options['qcld_express_category'])) {
        $qcld_express_categories = $qcld_express_options['qcld_express_category'];
    } else {
        $qcld_express_categories = array();
    }

   /* $offset=0;
    $per_page=-1;*/
    $qcld_express_product_per_page = isset($qcld_express_options['qcld_express_product_per_page']) ? $qcld_express_options['qcld_express_product_per_page'] : '';

    $offset = isset($_POST['offset']) ? sanitize_text_field($_POST['offset']) : '';
    $author = isset($_POST['author']) ? sanitize_text_field($_POST['author']) : '';

    if($qcld_express_show_mode=='more'){
        if($qcld_express_product_per_page != ''){
            $per_page=$qcld_express_product_per_page;
            
        }else{

            $per_page=-1;
        }
    }else{
        $per_page=-1;
    }

    //pulling up the product accroding to the query
    // Get WooCommerce Global from Qcld_woo_inc class
    // Create query arguments array
     $terms = get_terms( array(
        'taxonomy' => 'product_cat',
        'hide_empty' => false,
    ) );

    $categories=array();
    foreach ($terms as $term){
        if(!in_array($term->term_id, $qcld_express_categories)){
            array_push($categories,$term->term_id);
        }
    }


        $params = array(
            'post_type'         => 'product',
            'post_status'       => 'publish',
            'posts_per_page'    => $per_page,
            'offset'            => $offset,
            'author'            => $author,
            'orderby'           => $qcld_express_product_sorting,
            'order'             => $qcld_express_product_asc_desc,
            'tax_query' => array(
                array(
                    'taxonomy' => 'product_cat',
                    'field'    => 'term_id',
                    'terms'    => $categories,
                ),
            ),
        );     

         // product search by category ....
        if(isset($_POST['c_id'] )){
            $category=sanitize_text_field($_POST['c_id']);
            if($category=='all'){

              
            }else{
                $category_sort=array('product_cat' => $category);
                $params=array_merge($params,$category_sort);
            }
        }

            // Add meta_query to query args
        $params['meta_query'] = array();

        // Check products stock status
        $params['meta_query'][] = $woocommerce->query->stock_status_meta_query();

    //Now filtering the categories to pass to query.
   
    
    // product search by category ....
    if(isset($_POST['c_id'] )){
        $category=sanitize_text_field($_POST['c_id']);
        if($category=='all'){

        }else{

            $category_sort = array(
                'tax_query' => array(
                    'relation' => 'OR',
                    array(
                        'taxonomy'  => 'product_cat',
                        'field'     => 'term_id',
                        'terms'     => $category,
                        'operator'  => 'IN'
                    ),
                )
            );

            $params=array_merge($params,$category_sort);
        }
    }

    // order By sku ...
    if(isset($_POST['skuOrder'])){
        $list_order=sanitize_text_field($_POST['skuOrder']);
        //Sku sorting
        if($list_order=='sku_DESC' || $list_order=='sku_ASC'){
            $sku_url=explode("_",$list_order);
            $sku_sort=array('order' =>  $sku_url[1], 
                'orderby' => array('menu_order' => $sku_url[1], 'sku' => $sku_url[1]));
            $params=array_merge($params,$sku_sort);
        }

    }

    // order By Title ...
    if(isset($_POST['titleOrder'])){
        $list_order=sanitize_text_field($_POST['titleOrder']);
        //title sorting
        if($list_order=='title_DESC' || $list_order=='title_ASC'){
            $title_url=explode("_",$list_order);
            $title_sort=array('order' =>  $title_url[1], 
                'orderby' => array('menu_order' => $title_url[1], 'title' => $title_url[1]));
            $params=array_merge($params,$title_sort);
        }

    }

    // order By Price ...
    if(isset($_POST['priceOrder'])){
        $list_order=sanitize_text_field($_POST['priceOrder']);
        //Price sorting
        if($list_order=='price_DESC' || $list_order=='price_ASC'){

            $price_url=explode("_",$list_order);
            $price_sort=array(
                'orderby'   => 'meta_value_num',
                'meta_key'  => '_price',
                'order' => $price_url[1]
            );
            $params=array_merge($params,$price_sort);

        }

    }


    // search form by title...
    if (isset($_POST['title'])) {
        $title=sanitize_text_field($_POST['title']);
        if($title != '') {
        $titleArr = array('s' => $title);
        $params = array_merge($params, $titleArr);
        }
    }
   
    // search form by min price and max price...
    if (isset($_POST['price_min']) || isset($_POST['price_max'])) {
        $price_min = sanitize_text_field($_POST['price_min']);
        $price_max = sanitize_text_field($_POST['price_max']);
        if( $price_min != '' && $price_max == ''){
            $price_min = sanitize_text_field($_POST['price_min']);
            $price_max = sanitize_text_field($_POST['price_min']);
        }else if ($price_min == '' && $price_max != '') {
           $price_min = sanitize_text_field($_POST['price_max']);
           $price_max = sanitize_text_field($_POST['price_max']);
        }else if ($price_min != '' && $price_max != '') {
            $price_min = sanitize_text_field($_POST['price_min']);
            $price_max = sanitize_text_field($_POST['price_max']); 
        }
        if ($price_min != '' && $price_max != '') {
        $priceArr = array(array(
            'key'       => '_price',
            'value'     => array($price_min, $price_max),
            'compare'   => 'BETWEEN',
            'type'      => 'NUMERIC'
        ));

        $params = array_merge($params, array('meta_query' => $priceArr));
        }
    }

    // search form by sku...
    if (isset($_POST['sku'])) {
        $skus=sanitize_text_field($_POST['sku']);
        if ($skus != '') {
        $skuArr = array(array(
            'key'       => '_sku',
            'value'     => $skus,
            'compare'   => 'LIKE'
        ));

        $params = array_merge($params, array('meta_query' => $skuArr));
        }
    }

    // Calculating nextoffset to show button or not.
    // $products = get_posts($params);

    $query = new WP_Query();
    $products = $query->query($params);

    $products_num = count($products);
    //$products_num = $product_query->post_count;
    if ($products_num == $per_page) {
        $nextOffset = intval($offset + $per_page);
    } else {
        $nextOffset = -1;
    }
    //Creating WooCommerce product factory.
    $wc_pf = new WC_Product_Factory();
    $html = '';
    $counter = 0;
    $qcld_express_slope_type = 'left';
  

    foreach ($products as $P) {
        $product = $wc_pf->get_product($P->ID);
        $getProductDetail = wc_get_product($P->ID);
        //Checking for image thumb.
        $qcld_thumb = wp_get_attachment_image_src(get_post_thumbnail_id($P->ID), 'shop_thumbnail');
        $qcld_large = wp_get_attachment_image_src( get_post_thumbnail_id( $P->ID ), 'single-post-thumbnail' );
        if (!empty($qcld_thumb)) {
            $image = $qcld_thumb;
            $image_large =$qcld_large;
        } else {
            $image[0] = QCLD_EXPRESS_IMG_URL . '/placeholder.png';
            $image_large[0]=QCLD_EXPRESS_IMG_URL.'/placeholder.png';
        }
        if ($counter % 2 == 0 && $qcld_express_template == "slope") {
            if ($qcld_express_slope_type == 'left') {
                $qcld_express_extra_class = "left-slope";
                $qcld_express_slope_type = 'right';
            } else {
                $qcld_express_extra_class = "right-slope";
                $qcld_express_slope_type = 'left';
            }

        } else {
            $qcld_express_extra_class = "";
        }

        $html .= '<div class="qcld-express-row  qcld-express-body-content '. $qcld_express_extra_class .'">';
        //SKu show or not
        if (isset($qcld_express_sku) && $qcld_express_sku == 'on') {
            $html .= '<div class="qcld-express-cell qcld-express-sku-cell"><p class="qcld-express-sku">'. esc_html($product->get_sku(), 'express-shop') .'</p></div>';
        }


        //Thumbnail show or not
        if (isset($qcld_express_thumbnails) && $qcld_express_thumbnails == 'on') {
            $html .= '<div class="qcld-express-cell qcld-express-thumb-cell"><p class="qcld-express-thumbnail"><img class="qlcd-express-list-thumb" src="' . esc_url($image[0], 'express-shop') . '"  alt="' . esc_attr(get_the_title($P->ID), 'express-shop') . '"/></p></div>';


        }
        //Title show or not
        if (isset($qcld_express_title) && $qcld_express_title == 'on') {
            $html .= '<div class="qcld-express-cell qcld-express-title-cell"><h3 class="qcld-express-title"><a href="' . esc_url(get_permalink($P->ID), 'express-shop') . '">' . esc_html(get_the_title($P->ID), 'express-shop') . '</a></p></div>';
        }
        //Description show or not
        if (isset($qcld_express_description) && $qcld_express_description == 'on') {
            $html .= '<div class="qcld-express-cell qcld-express-desc-cell"><p class="qcld-express-desc">' . strip_tags(substr(get_the_excerpt($P->ID), 0, 100)) . ' ... ';
             if(isset($qcld_express_details_button) && $qcld_express_details_button == 'on' ){
      
              $html .='<a href="" data-fancybox data-src="#qcld-express-product-modal"  class="qcld_express_product_details" product-id="'.esc_attr(get_the_ID(), 'express-shop').'">
                  <i class="fa fa-info-circle" aria-hidden="true"></i>
              </a>';
            }
           
            $html .='</p></div>';
        }
        //Availability show or not
        if (isset($qcld_express_stock) && $qcld_express_stock == 'on') {
            $stock = get_post_meta( get_the_ID(), '_stock', true );
            $html .= '<div class="qcld-express-cell qcld-express-avail-cell"><p class="qcld-express-avail">';
           
            if ($stock > 0) {
                $html .= '<span class="qcld-express-avail-in"> In Stock (' . $product->get_stock_quantity() . ')</span>';
            } else if($stock=='') {
                $stock_status = get_post_meta( $P->ID, '_stock_status', true );
                if( $stock_status == "instock" ){ 
                    $html .= '<span class="qcld-express-avail-out"> '.esc_html('In Stock', 'express-shop').' </span>';
                }else if( $stock_status == "outofstock" ){
                    $html .= '<span class="qcld-express-avail-outs"> '.esc_html('Out of Stock', 'express-shop').'</span>';
                } 
            }else if($stock<0) {
                $html .= '<span class="qcld-express-avail-out"> '.esc_html('Out of Stock', 'express-shop').'</span>';
            }
            $html .= '</p></div>';
        }


        //Price show or not
        if (isset($qcld_express_price) && $qcld_express_price == 'on') {
            $html .= '<div class="qcld-express-cell qcld-express-price-cell"><p class="qcld-express-price">' . $product->get_price_html() . '</p></div>';
        }
        // Action show or not
        if (isset($qcld_express_action) && $qcld_express_action == 'on') {
            $html .= '<div class="qcld-express-cell qcld-express-action-cell"><ul class="qcld-express-action">';
            
            if ($product->get_type() == 'simple') {
                
              $stock = get_post_meta( get_the_ID(), '_stock', true );
              $stock_status = get_post_meta( $P->ID, '_stock_status', true );
              if( $stock > 0 ){

                    if (isset($qcld_express_cart_mode) && $qcld_express_cart_mode == 'wooinstants') { 
            
                $qcld_woo_minmax_enable_plugin = get_option( 'qcld_woo_minmax_enable_plugin' );
                if(function_exists('qcld_woo_minmax_install') && isset($qcld_woo_minmax_enable_plugin) && $qcld_woo_minmax_enable_plugin  == 'yes'){

                    $response_min_max = qcld_woo_minmax_express_add_rules_text($P->ID,0);

                    $min_qty  = $response_min_max['min'];
                    $max_qty  = $response_min_max['max'];
                    $step_qty = $response_min_max['step'];
    
                    $html.='<li><input type="number" value="'.($min_qty ? $min_qty : 1).'" step="'.($step_qty ? $step_qty : '').'" min="'.($min_qty ? $min_qty : '').'" max="'.($max_qty ? $max_qty : '').'" name="qcld_quantity" class="qcld-express-simple-p-qnty"></li>';


                }else{
                $html.='<li><input type="number" value="1" name="qcld_quantity" class="qcld-express-simple-p-qnty"></li>';
                } 
                        $qcld_woo_minmax_enable_plugin = get_option( 'qcld_woo_minmax_enable_plugin' );
                        if(function_exists('qcld_woo_minmax_install') && isset($qcld_woo_minmax_enable_plugin) && $qcld_woo_minmax_enable_plugin  == 'yes'){

                            $response_min_max = qcld_woo_minmax_express_add_rules_text($P->ID,0);
                            $min_qty  = $response_min_max['min'];
                          }else{
                            $min_qty  = 1;

                          }
                      $html .= ' <li><a  href="#"  class="qcld_express_s_p ajax_add_to_cart add_to_cart_button product_type_simple" data-quantity="'.($min_qty ? $min_qty : 1).'" data-product_id="' . esc_attr($P->ID, 'express-shop') . '" p-id="' . esc_attr($P->ID, 'express-shop') . '"p-price="' . esc_attr($product->get_price(), 'express-shop') . '" ><i class="fa fa-shopping-cart"></i></a></li>';
                    }else{
                      $html.='<li><input type="number" value="1" name="qcld_quantity" class="qcld-express-simple-p-qnty"  min="1"></li>';
                      $html .= ' <li><a  href="#"  class="qcld_express_s_p" p-id="' . esc_attr($P->ID, 'express-shop') . '"p-price="' . esc_attr($product->get_price(), 'express-shop') . '" ><i class="fa fa-shopping-cart"></i></a></li>';
                    }

               } else if( $stock == "" ){
               if( $stock_status == "instock" ){

                    if (isset($qcld_express_cart_mode) && $qcld_express_cart_mode == 'wooinstants') { 
            
                $qcld_woo_minmax_enable_plugin = get_option( 'qcld_woo_minmax_enable_plugin' );
                if(function_exists('qcld_woo_minmax_install') && isset($qcld_woo_minmax_enable_plugin) && $qcld_woo_minmax_enable_plugin  == 'yes'){

                    $response_min_max = qcld_woo_minmax_express_add_rules_text($P->ID,0);

                    $min_qty  = $response_min_max['min'];
                    $max_qty  = $response_min_max['max'];
                    $step_qty = $response_min_max['step'];
    
                    $html.='<li><input type="number" value="'.($min_qty ? $min_qty : 1).'" step="'.($step_qty ? $step_qty : '').'" min="'.($min_qty ? $min_qty : '').'" max="'.($max_qty ? $max_qty : '').'" name="qcld_quantity" class="qcld-express-simple-p-qnty"></li>';


                }else{
                $html.='<li><input type="number" value="1" name="qcld_quantity" class="qcld-express-simple-p-qnty"></li>';
                } 
                        $qcld_woo_minmax_enable_plugin = get_option( 'qcld_woo_minmax_enable_plugin' );
                        if(function_exists('qcld_woo_minmax_install') && isset($qcld_woo_minmax_enable_plugin) && $qcld_woo_minmax_enable_plugin  == 'yes'){

                            $response_min_max = qcld_woo_minmax_express_add_rules_text($P->ID,0);
                            $min_qty  = $response_min_max['min'];
                          }else{
                            $min_qty  = 1;

                          }
                      $html .= ' <li><a  href="#"  class="qcld_express_s_p ajax_add_to_cart add_to_cart_button product_type_simple" data-quantity="'.($min_qty ? $min_qty : 1).'" data-product_id="' . esc_attr($P->ID, 'express-shop') . '" p-id="' . esc_attr($P->ID, 'express-shop') . '"p-price="' . esc_attr($product->get_price(), 'express-shop') . '" ><i class="fa fa-shopping-cart"></i></a></li>';
                    }else{
                      $html.='<li><input type="number" value="1" name="qcld_quantity" class="qcld-express-simple-p-qnty"  min="1"></li>';
                      $html .= ' <li><a  href="#"  class="qcld_express_s_p" p-id="' . esc_attr($P->ID, 'express-shop') . '"p-price="' . esc_attr($product->get_price(), 'express-shop') . '" ><i class="fa fa-shopping-cart"></i></a></li>';
                    }

                } else if( $stock_status == "outofstock" ){ 
                    $html .= ' <li><a href="#" class="qcld_express_v_p qcld_express_config" p-id="' . esc_attr($P->ID, 'express-shop') . '"p-price="' . esc_attr($product->get_price(), 'express-shop') . '" > <i class="fa fa-shopping-cart"></i> </a></li>';

                 } 
                }else if( $stock < 1 ){ 

                    if (isset($qcld_express_cart_mode) && $qcld_express_cart_mode == 'wooinstants') { 
            
                $qcld_woo_minmax_enable_plugin = get_option( 'qcld_woo_minmax_enable_plugin' );
                if(function_exists('qcld_woo_minmax_install') && isset($qcld_woo_minmax_enable_plugin) && $qcld_woo_minmax_enable_plugin  == 'yes'){

                    $response_min_max = qcld_woo_minmax_express_add_rules_text($P->ID,0);

                    $min_qty  = $response_min_max['min'];
                    $max_qty  = $response_min_max['max'];
                    $step_qty = $response_min_max['step'];
    
                    $html.='<li><input type="number" name="qcld_quantity" class="qcld-express-simple-p-qnty"  value="'.($min_qty ? $min_qty : 1).'" step="'.($step_qty ? $step_qty : '').'" min="'.($min_qty ? $min_qty : '').'" max="'.($max_qty ? $max_qty : '').'"></li>';

                }else{
                    $html.='<li><input type="number" value="1" name="qcld_quantity" class="qcld-express-simple-p-qnty"  min="1"></li>';
                } 

                      


                         $qcld_woo_minmax_enable_plugin = get_option( 'qcld_woo_minmax_enable_plugin' );
                        if(function_exists('qcld_woo_minmax_install') && isset($qcld_woo_minmax_enable_plugin) && $qcld_woo_minmax_enable_plugin  == 'yes'){

                            $response_min_max = qcld_woo_minmax_express_add_rules_text($P->ID,0);
                            $min_qty  = $response_min_max['min'];
                          }else{
                            $min_qty  = 1;

                          }
                      $html .= ' <li><a  href="#"  class="qcld_express_s_p ajax_add_to_cart add_to_cart_button product_type_simple" data-quantity="'.($min_qty ? $min_qty : 1).'" data-product_id="' . esc_attr($P->ID, 'express-shop') . '" p-id="' . esc_attr($P->ID, 'express-shop') . '"p-price="' . esc_attr($product->get_price(), 'express-shop') . '" ><i class="fa fa-shopping-cart"></i></a></li>';
                    }else{
                      $html.='<li><input type="number" value="1" name="qcld_quantity" class="qcld-express-simple-p-qnty"  min="1"></li>';
                      $html .= ' <li><a  href="#"  class="qcld_express_s_p" p-id="' . esc_attr($P->ID, 'express-shop') . '"p-price="' . esc_attr($product->get_price(), 'express-shop') . '" ><i class="fa fa-shopping-cart"></i></a></li>';
                    }

                } 


            }
            if ($product->get_type() == 'variable') {
                

              $stock = get_post_meta( get_the_ID(), '_stock', true );
              $stock_status = get_post_meta( $P->ID, '_stock_status', true );
              if( $stock > 0 ){
              $html .= ' <li><a href="#" class="qcld_express_v_p" p-id="' . esc_attr($P->ID, 'express-shop') . '"><i class="fa fa-shopping-cart"></i></a></li>';

               } else if( $stock == "" ){
               if( $stock_status == "instock" ){
                    $html .= ' <li><a href="#" class="qcld_express_v_p" p-id="' . esc_attr($P->ID, 'express-shop') . '"><i class="fa fa-shopping-cart"></i></a></li>';

                } else if( $stock_status == "outofstock" ){ 
                    $html .= ' <li><a href="#" class="qcld_express_v_p qcld_express_config" p-id="' . esc_attr($P->ID, 'express-shop') . '"><i class="fa fa-shopping-cart"></i></a></li>';

                 } 
                }else if( $stock < 1 ){ 
                    $html .= ' <li><a href="#" class="qcld_express_v_p" p-id="' . esc_attr($P->ID, 'express-shop') . '"><i class="fa fa-shopping-cart"></i></a></li>';

                } 

            }

            $html .= '</ul>';

            $qcld_woo_minmax_dynamic_pricing_setting = get_option('qcld_woo_minmax_dynamic_pricing_setting');

            if(function_exists('qcld_woo_minmax_install') && (isset($qcld_woo_minmax_dynamic_pricing_setting['qcld_woo_minmax_price_table_on_off']) && $qcld_woo_minmax_dynamic_pricing_setting['qcld_woo_minmax_price_table_on_off'] == 'enable') ){

              $result_show = qcld_woo_minmax_express_dynamic_discount_price_table_show($P->ID);
              $html .= $result_show;
            }
            $html .= '</div>';
        }//End of show or not
        $html .= '</div>';
        $counter++;
    } //End of loop
    wp_reset_postdata();
    $response = array('offset' => $nextOffset, 'cat_type_id' => sanitize_text_field($_POST['c_id']),  'html' => $html);
    echo wp_send_json($response);

}



// ajax search product .... 
/* Ajax search */
add_action('wp_ajax_qcld_express_shop_ajax_search', 'qcld_express_shop_ajax_search');
add_action('wp_ajax_nopriv_qcld_express_shop_ajax_search', 'qcld_express_shop_ajax_search');
if ( !function_exists('qcld_express_shop_ajax_search') ) {

    function qcld_express_shop_ajax_search() {

        check_ajax_referer( 'qcld_express_security_nonce', 'security');

        global $wpdb, $post;

        $return = array(
            'status' => 'error',
            'html' => ''
        );    
        if( isset( $_REQUEST['s'] ) && $_REQUEST['s'] != '') {    
          
                $taxonomy = 'product_cat';
                $post_type = 'product';
           
            $number = 10;
            $num_result = isset($number) ? (int) $number : 10;
            $query = isset($_REQUEST['s']) ? sanitize_text_field($_REQUEST['s']) : '';
            $author = isset($_REQUEST['author']) ? sanitize_text_field($_REQUEST['author']) : '';
            $args = array(
                'post_status'       => 'publish',
                'post_type'         => $post_type, 
                'author'            => $author,
                's'                 => $query, 
                'posts_per_page'    => $num_result,
                'tax_query'         => array()
            );
            //if ($has_woocommerce) {
                $args['meta_query'] = WC()->query->get_meta_query();
                $args['tax_query'] = WC()->query->get_tax_query();
           // }
          
            $results = new WP_Query($args);



            if ($results->have_posts()) {

                $return['status'] = 'success';
                $return['html'] .= '<ul class="qcld-express-list-products">';
                while ($results->have_posts()) {
                 ob_start();
                 $results->the_post();

                 $image = '';
                 if ($post_type == 'product') {
                    $product = wc_get_product($post->ID);
                    $image = $product->get_image();
                } else if (has_post_thumbnail($post->ID)) {
                    $image = get_the_post_thumbnail($post->ID, 'thumbnail');
                }

                $return['html'] .= '<li>';
                $return['html'] .= '<div class="search-form-wrapper-image">';
                $return['html'] .= '<a href="#" class="qcld-express-search-single" p-id="'.$post->ID.'">' . $image . '</a>';
                $return['html'] .= '</div>';
                $return['html'] .= '<div class="search-form-wrapper-meta item-information">';
                $return['html'] .= '<a href="#" p-id="'.$post->ID.'" class="product-title product-name qcld-express-search-single">' . esc_attr($post->post_title) . '</a>';
                if ($post_type == 'product') {
                    if ($price_html = $product->get_price_html()) {
                        $return['html'] .= '<span class="price">' . $price_html . '</span>';
                    }
                }
                $return['html'] .= '</div>';
                $return['html'] .= '</li>';
            }
            $return['html'] .= '</ul>';

            if (isset($results->post_count, $results->found_posts) && $results->found_posts > $results->post_count) {
                $return['status'] = 'error';
                $view_all_text = sprintf(esc_html__('View all %d results', 'express-shop-pro'), $results->found_posts);

            }
            wp_reset_postdata();
        }
        else {
            $return['html'] = '<span class="no-result">'.esc_html__('No matches found.', 'express-shop-pro').'</span>';
            wp_reset_postdata();
        }
    }
    echo json_encode($return);
    die();
    }
} 



//Load more product
add_action('wp_ajax_qcld_express_search_product_single_view', 'qcld_express_search_product_single_view');
add_action('wp_ajax_nopriv_qcld_express_search_product_single_view', 'qcld_express_search_product_single_view');

if ( !function_exists('qcld_express_search_product_single_view') ) {

function qcld_express_search_product_single_view(){

    check_ajax_referer( 'qcld_express_security_nonce', 'security');

    global $woocommerce;

   // Getting option tree from backend.
    $qcld_express_options       = get_option('qcld_express_options');
    $qcld_express_show_mode     = isset($qcld_express_options['qcld_express_show_mode']) ? $qcld_express_options['qcld_express_show_mode'] : '';
    $qcld_express_cart_mode     = isset($qcld_express_options['qcld_express_cart_mode']) ? $qcld_express_options['qcld_express_cart_mode'] : '';
    $qcld_express_sku           = isset($qcld_express_options['qcld_express_sku']) ? $qcld_express_options['qcld_express_sku'] : '';
    $qcld_express_thumbnail     = isset($qcld_express_options['qcld_express_thumbnail']) ?$qcld_express_options['qcld_express_thumbnail'] : '';
    $qcld_express_title         = isset($qcld_express_options['qcld_express_title']) ? $qcld_express_options['qcld_express_title'] : '';
    $qcld_express_description   = isset($qcld_express_options['qcld_express_description']) ? $qcld_express_options['qcld_express_description'] : '';
    $qcld_express_price         = isset($qcld_express_options['qcld_express_price']) ? $qcld_express_options['qcld_express_price'] : '';
    $qcld_express_stock         = isset($qcld_express_options['qcld_express_stock']) ? $qcld_express_options['qcld_express_stock'] : '';
    $qcld_express_stock_alert   = isset($qcld_express_options['qcld_express_stock_alert']) ? $qcld_express_options['qcld_express_stock_alert'] : '';
    $qcld_express_action        = isset($qcld_express_options['qcld_express_action']) ? $qcld_express_options['qcld_express_action'] : '';
    $qcld_express_background    = isset($qcld_express_options['qcld_express_background']) ? $qcld_express_options['qcld_express_background'] : '';
    $qcld_express_altern        = isset($qcld_express_options['qcld_express_altern']) ? $qcld_express_options['qcld_express_altern'] : '';
    $qcld_express_custom_style  = isset($qcld_express_options['qcld_express_custom_style']) ? $qcld_express_options['qcld_express_custom_style'] : '';
    $qcld_express_short_description_set_character        = isset($qcld_express_options['qcld_express_short_description_set_character']) ? $qcld_express_options['qcld_express_short_description_set_character'] : '';

    $qcld_express_hide_search_option= isset($qcld_express_options['qcld_express_hide_search_option']) ? $qcld_express_options['qcld_express_hide_search_option']: '';
    $qcld_express_category_show_above_tite= isset($qcld_express_options['qcld_express_category_show_below_tite']) ? $qcld_express_options['qcld_express_category_show_below_tite'] : '';

    // Language Change option ... 
    $qcld_express_lang_sku          = isset($qcld_express_options['qcld_express_lang_sku']) ? $qcld_express_options['qcld_express_lang_sku'] : '';
    $qcld_express_lang_thumbnail    = isset($qcld_express_options['qcld_express_lang_thumbnail']) ? $qcld_express_options['qcld_express_lang_thumbnail'] : '';
    $qcld_express_lang_title        = isset($qcld_express_options['qcld_express_lang_title']) ? $qcld_express_options['qcld_express_lang_title'] : '';
    $qcld_express_lang_desc         = isset($qcld_express_options['qcld_express_lang_desc']) ? $qcld_express_options['qcld_express_lang_desc'] : '';
    $qcld_express_lang_availability = isset($qcld_express_options['qcld_express_lang_availability']) ? $qcld_express_options['qcld_express_lang_availability'] : '';
    $qcld_express_lang_price        = isset($qcld_express_options['qcld_express_lang_price']) ? $qcld_express_options['qcld_express_lang_price'] : '';
    $qcld_express_lang_action       = isset($qcld_express_options['qcld_express_lang_action']) ? $qcld_express_options['qcld_express_lang_action'] : '';
    $qcld_express_lang_search       = isset($qcld_express_options['qcld_express_lang_search']) ? $qcld_express_options['qcld_express_lang_search'] : '';
    $qcld_express_lang_cart_summary = isset($qcld_express_options['qcld_express_lang_cart_summary']) ? $qcld_express_options['qcld_express_lang_cart_summary']: '';
    $qcld_express_lang_edit_on_cart = isset($qcld_express_options['qcld_express_lang_edit_on_cart']) ? $qcld_express_options['qcld_express_lang_edit_on_cart'] : '';
    $qcld_express_lang_checkout     = isset($qcld_express_options['qcld_express_lang_checkout']) ? $qcld_express_options['qcld_express_lang_checkout'] : '';
    $qcld_express_lang_no_products  = isset($qcld_express_options['qcld_express_lang_no_products']) ? $qcld_express_options['qcld_express_lang_no_products'] : '';

        // sorting variables
    $qcld_express_details_button    = '';
    $qcld_express_product_sorting   = isset($qcld_express_options['qcld_express_product_sorting']) ? $qcld_express_options['qcld_express_product_sorting'] : '';
    $qcld_express_product_asc_desc  = isset($qcld_express_options['qcld_express_product_asc_desc']) ? $qcld_express_options['qcld_express_product_asc_desc'] : '';

    $qcld_express_template = isset($qcld_express_options['qcld_express_template']) ? $qcld_express_options['qcld_express_template'] : '';

    $qcld_express_product_per_page = isset($qcld_express_options['qcld_express_product_per_page']) ? $qcld_express_options['qcld_express_product_per_page'] : '';


    if(isset($qcld_express_options['qcld_express_lang_title_placeholder']) && $qcld_express_options['qcld_express_lang_title_placeholder'] !=''){
    $qcld_express_lang_title_placeholder = $qcld_express_options['qcld_express_lang_title_placeholder'];
    }else{
    $qcld_express_lang_title_placeholder = 'Title';

    }    
    if(isset($qcld_express_options['qcld_express_lang_sku_placeholder']) && $qcld_express_options['qcld_express_lang_sku_placeholder'] !=''){
    $qcld_express_lang_sku_placeholder = $qcld_express_options['qcld_express_lang_sku_placeholder'];
    }else{
    $qcld_express_lang_sku_placeholder = 'SKU';

    }
    if(isset($qcld_express_options['qcld_express_lang_in_stock']) && $qcld_express_options['qcld_express_lang_in_stock'] !=''){
    $qcld_express_lang_in_stock = $qcld_express_options['qcld_express_lang_in_stock'];
    }else{
    $qcld_express_lang_in_stock = 'In Stock';
    }
    if(isset($qcld_express_options['qcld_express_lang_out_of_stock']) && $qcld_express_options['qcld_express_lang_out_of_stock'] !=''){
    $qcld_express_lang_out_of_stock = $qcld_express_options['qcld_express_lang_out_of_stock'];
    }else{
    $qcld_express_lang_out_of_stock = 'Out of Stock';
    }
    if(isset($qcld_express_options['qcld_express_lang_from']) && $qcld_express_options['qcld_express_lang_from'] !=''){
    $qcld_express_lang_from = $qcld_express_options['qcld_express_lang_from'];
    }else{
    $qcld_express_lang_from = 'Form';
    }
    if(isset($qcld_express_options['qcld_express_lang_to']) && $qcld_express_options['qcld_express_lang_to'] !=''){
    $qcld_express_lang_to = $qcld_express_options['qcld_express_lang_to'];
    }else{
    $qcld_express_lang_to = 'To';
    }

    if(isset($qcld_express_options['qcld_express_tag']) && $qcld_express_options['qcld_express_tag'] !=''){
    $qcld_express_tag = $qcld_express_options['qcld_express_tag'];
    }else{
    $qcld_express_tag = 'off';

    }

    if(isset($qcld_express_options['qcld_express_tag_lang']) && $qcld_express_options['qcld_express_tag_lang'] !=''){
    $qcld_express_tag_lang = $qcld_express_options['qcld_express_tag_lang'];
    }else{
    $qcld_express_tag_lang = 'Tag';

    }


    $p_id               = isset($_POST['p_id']) ? sanitize_text_field($_POST['p_id']) : '';
    $offset             = isset($_POST['offset']) ? sanitize_text_field($_POST['offset']) : '';
    $category_ids       = isset($_POST['category_ids']) ? sanitize_text_field($_POST['category_ids']) : '';
    $link_title_to_modal = isset($_POST['link_title_to_modal']) ? sanitize_text_field($_POST['link_title_to_modal']) : '';
    $search_type        = isset($_POST['search_type']) ? sanitize_text_field($_POST['search_type']) : '';

    if($qcld_express_show_mode=='more' || $qcld_express_show_mode=='scroll'){
        if($qcld_express_product_per_page != ''){
            $per_page=$qcld_express_product_per_page;
            
        }else{

            $per_page=-1;
        }
    }else{
        $per_page=-1;
    }



    //pulling up the product accroding to the query
    // Get WooCommerce Global from Qcld_woo_inc class
    // Create query arguments array
    $category_ids_perameter = '';
     if ($category_ids != '') {
        // if shortcode define category then work here...
        $category_ids = $category_ids;
        $category_ids_perameter = $category_ids;
        $qcld_express_categories = explode(',', $category_ids);

         //Now filtering the categories to pass to query.
        
        $terms = get_terms( array(
            'taxonomy' => 'product_cat',
            'hide_empty' => false,
        ) );

        $categories=array();
        foreach ($terms as $term){
            if(in_array($term->term_id, $qcld_express_categories)){
                array_push($categories,$term->term_id);
            }
        }

    }else{
        // if admin define category then work here...
        if (isset($qcld_express_options['qcld_express_category'])) {
            $qcld_express_categories = $qcld_express_options['qcld_express_category'];
        } else {
            $qcld_express_categories = array();
        }

       
        $terms = get_terms( array(
            'taxonomy' => 'product_cat',
            'hide_empty' => false,
        ) );

        $categories=array();
        foreach ($terms as $term){
            if(!in_array($term->term_id, $qcld_express_categories)){
                array_push($categories,$term->term_id);
            }
        }

    }

        $params = array(
            'posts_per_page'    => $per_page,
            'offset'            => $offset,
            'post_status'       => 'publish',
            'post_type'         => array('product', 'product_variation'),
            'post__in'          => [$p_id],
            'orderby'           => $qcld_express_product_sorting,
            'order'             => $qcld_express_product_asc_desc,
     
        );     

            // Add meta_query to query args
        $params['meta_query'] = array();

        // Check products stock status
        //$params['meta_query'][] = $woocommerce->query->stock_status_meta_query();

        // Check products stock status
        $params['meta_query'][] = $woocommerce->query->visibility_meta_query();
        $params['meta_query'][] = $woocommerce->query->stock_status_meta_query();

    //Now filtering the categories to pass to query.
   

   

    // Calculating nextoffset to show button or not.
    // $products = get_posts($params);

    $query = new WP_Query();
    $products = $query->query($params);

    $products_num = count($products);
    if ($products_num == $per_page) {
        $nextOffset = intval($offset + $per_page);
    } else {
        $nextOffset = -1;
    }
    //Creating WooCommerce product factory.
    $wc_pf = new WC_Product_Factory();
    $html = '';
    $counter = 0;
    $qcld_express_slope_type = 'left';
        $html .= '<div class="qcld-express-row qcld_header header">';
         //Sku show or not
        if(isset($qcld_express_sku) && $qcld_express_sku=='on' ){
             
            $html .='<div class="qcld-express-cell qcld-express-sku-cell">
            <p class="qcld-express-sku-heading"> <a id="sku-sort" ';
            if(isset($_POST['skuOrder'])){
                if($_POST['skuOrder'] == 'sku_ASC'){
                    $html .=' category_ids="'.$category_ids_perameter.'" title-modal="'.$link_title_to_modal.'" order_by_sku="sku_DESC" href="#"> '.$qcld_express_lang_sku.' </a> </p>';   
                }
                if($_POST['skuOrder'] == 'sku_DESC'){
                    $html .=' category_ids="'.$category_ids_perameter.'" title-modal="'.$link_title_to_modal.'" order_by_sku="sku_ASC" href="#"> '.$qcld_express_lang_sku.' </a> </p>';   
                }
            }else{
                    $html .=' category_ids="'.$category_ids_perameter.'" title-modal="'.$link_title_to_modal.'" order_by_sku="sku_ASC" href="#"> '.$qcld_express_lang_sku.' </a> </p>';
            }
            $html .='';
            if(!isset($_GET['category'] )){
                if(isset($_POST['sku'])){
                    $sku=sanitize_text_field($_POST['sku']);
                }else{
                    $sku="";
                }

                if(isset($qcld_express_hide_search_option) && $qcld_express_hide_search_option != 'on'){

                    $html .=' <input type="text" name="sku" id="qcld_sku" value="'.$sku.'" placeholder="'.$qcld_express_lang_sku_placeholder.'">';
                }

            }
                $html .='</div>';
        } 

         //Image thumnail show or not
        if(isset($qcld_express_thumbnail) && $qcld_express_thumbnail=='on' ){
      
            $html .='<div class="qcld-express-cell qcld-express-thumb-cell">
                <p class="qcld-express-thumbnail-heading">'.$qcld_express_lang_thumbnail.'</p>
                </div>';
        }
         //Title show or not
        if(isset($qcld_express_title) && $qcld_express_title=='on' ){
    
            $html .='<div class="qcld-express-cell qcld-express-title-cell">
            <p class="qcld-express-title-heading"> <a id="title-sort" ';
            if(isset($_POST['titleOrder'])){
                if($_POST['titleOrder'] == 'title_ASC'){
                    $html .=' category_ids="'.$category_ids_perameter.'" title-modal="'.$link_title_to_modal.'" order_by_title="title_DESC" href="#">'.$qcld_express_lang_title.' </a> </p>';   
                }
                if($_POST['titleOrder'] == 'title_DESC'){
                    $html .=' category_ids="'.$category_ids_perameter.'" title-modal="'.$link_title_to_modal.'" order_by_title="title_ASC" href="#">'.$qcld_express_lang_title.' </a> </p>';   
                }
            }else{
                    $html .=' category_ids="'.$category_ids_perameter.'" title-modal="'.$link_title_to_modal.'" order_by_title="title_ASC" href="#">'.$qcld_express_lang_title.' </a> </p>';
            }
            $html .='';
            if(!isset($_GET['category'] )){
                if(isset($_POST['title'])){
                    $title=sanitize_text_field($_POST['title']);
                }else{
                    $title="";
                }
                if(isset($qcld_express_hide_search_option) && $qcld_express_hide_search_option != 'on'){
                    
                    $html .='<div class="qcld-express-search-form-wrapper">
                <form method="get" class="qcld_express_searchform" action="'.esc_url(home_url('/')).'" role="search">
                    <div class="qcld_express_search_products">';
                        $html .=' <input type="text" name="title" id="qcld_title" value="'.$title.'" placeholder="Search" autocomplete="off">';
                    $html .=' </div></form></div>';

                }
            } 
            $html .='</div>';
        }

        //Description show or not
        if(isset($qcld_express_description) && $qcld_express_description=='on' ){
     
            $html .='<div class="qcld-express-cell qcld-express-desc-cell">
                <p class="qcld-express-desc-heading"> '.$qcld_express_lang_desc.' </p>
                </div>';
        }

        //Tag show or not
        if(isset($qcld_express_tag) && $qcld_express_tag=='on' ){
     
            $html .='<div class="qcld-express-cell qcld-express-desc-cell">
                <p class="qcld-express-desc-heading"> '.$qcld_express_tag_lang.' </p>
                </div>';
        }

        //Availability show or not
        if(isset($qcld_express_stock) && $qcld_express_stock=='on' ){
      
            $html .='<div class="qcld-express-cell qcld-express-avail-cell">
                <p class="qcld-express-avail-heading">'.$qcld_express_lang_availability.'</p>
                </div>';
        }

        //Price show or not
        if(isset($qcld_express_price) && $qcld_express_price=='on' ){
     
            $html .='<div class="qcld-express-cell qcld-express-price-cell">
          <p class="qcld-express-price-heading"><a id="price-sort" ';
            if(isset($_POST['priceOrder'])){
                if($_POST['priceOrder'] == 'price_ASC'){
                    $html .=' category_ids="'.$category_ids_perameter.'" title-modal="'.$link_title_to_modal.'" order_by_price="price_DESC" href="#"> '.$qcld_express_lang_price.' </a> </p>';   
                }
                if($_POST['priceOrder'] == 'price_DESC'){
                    $html .=' category_ids="'.$category_ids_perameter.'" title-modal="'.$link_title_to_modal.'" order_by_price="price_ASC" href="#"> '.$qcld_express_lang_price.' </a> </p>';   
                }
            }else{
                    $html .=' category_ids="'.$category_ids_perameter.'" title-modal="'.$link_title_to_modal.'" order_by_price="price_ASC" href="#"> '.$qcld_express_lang_price.' </a> </p>';
            }
            $html .='';
        if(!isset($_GET['category'] )){
            if(isset($_POST['price_min'])){
                $price_min=sanitize_text_field($_POST['price_min']);
            }else{
                $price_min="";
            }
            if(isset($_POST['price_max'])){
                $price_max=sanitize_text_field($_POST['price_max']);
            }else{
                $price_max="";
            }

            if(isset($qcld_express_hide_search_option) && $qcld_express_hide_search_option != 'on'){
              //  $html .=' <input type="number" name="price_min" value="'.$price_min.'" id="qcld_price_min" placeholder="'.$qcld_express_lang_from.'" min="0" > - <input type="number" min="0" name="price_max"  value="'.$price_max.'" id="qcld_price_max" placeholder="'.$qcld_express_lang_to.'" >';
            }
        } 
            $html .='</div>';
        }

        // Action show or not
        if(isset($qcld_express_action) && $qcld_express_action=='on' ){

            $html .='<div class="qcld-express-cell qcld-express-action-cell">
          <p class="qcld-express-action-heading">  </p>';
        if(!isset($_GET['category'] )){

            if(isset($qcld_express_hide_search_option) && $qcld_express_hide_search_option != 'on'){
                $html .='<button type="submit" id="submit_search"> '.$qcld_express_lang_search.' </button>';
            }
        }
            $html .='</div>';
        } 

        $html .='</div>';

    foreach ($products as $P) {
        $product = $wc_pf->get_product($P->ID);
        $getProductDetail = wc_get_product($P->ID);
        //Checking for image thumb.
        $qcld_thumb = wp_get_attachment_image_src(get_post_thumbnail_id($P->ID), 'shop_thumbnail');
        $qcld_large = wp_get_attachment_image_src( get_post_thumbnail_id( $P->ID ), 'single-post-thumbnail' );
        if (!empty($qcld_thumb)) {
            $image = $qcld_thumb;
            $image_large =$qcld_large;
        } else {
            $image[0] = QCLD_EXPRESS_IMG_URL . '/placeholder.png';
            $image_large[0]=QCLD_EXPRESS_IMG_URL.'/placeholder.png';
        }
        if ($counter % 2 == 0 && $qcld_express_template == "slope") {
            if ($qcld_express_slope_type == 'left') {
                $qcld_express_extra_class = "left-slope";
                $qcld_express_slope_type = 'right';
            } else {
                $qcld_express_extra_class = "right-slope";
                $qcld_express_slope_type = 'left';
            }

        } else {
            $qcld_express_extra_class = "";
        }

        $html .= '<div class="qcld-express-row   qcld-express-body-content '. $qcld_express_extra_class .'">';
        //SKu show or not
        if (isset($qcld_express_sku) && $qcld_express_sku == 'on') {
            $html .= '<div class="qcld-express-cell qcld-express-sku-cell"><p class="qcld-express-sku">'. $product->get_sku() .'</p></div>';
        }
        //Thumbnail show or not
        if (isset($qcld_express_thumbnail) && $qcld_express_thumbnail == 'on') {
            $html .= '<div class="qcld-express-cell qcld-express-thumb-cell"><img class="qlcd-express-list-thumb" src="' . $image[0] . '"  alt="' . get_the_title($P->ID) . '"/></div>';


        }
        //Title show or not
        if (isset($qcld_express_title) && $qcld_express_title == 'on') {

            $html .= '<div class="qcld-express-cell qcld-express-title-cell"><h3 class="qcld-express-title">';
            if(isset($link_title_to_modal) && ($link_title_to_modal == '1' || $link_title_to_modal == '01')){
            $html .='<a href="#" class="qcld_express_product_details"  product-id="'.$P->ID.'">' . get_the_title($P->ID) . '</a>';
            }else{ 
            $html .='<a href="'.get_the_permalink($P->ID).'"  product-id="'.$P->ID.'">' . get_the_title($P->ID) . '</a>';
            }

            if(isset($qcld_express_category_show_above_tite) && $qcld_express_category_show_above_tite == 'on' ){
                $html .= '<p class="qcld-express-categorie">'.wc_get_product_category_list($P->ID).'</p>';
            }

            $html .= '</h3></div>';

        }
        //Description show or not
        if (isset($qcld_express_description) && $qcld_express_description == 'on') {

                $html .= '<div class="qcld-express-cell qcld-express-desc-cell"><p class="qcld-express-desc">' . strip_tags(substr(get_the_excerpt($P->ID), 0, $qcld_express_short_description_set_character)) . ' ... ';

             if(isset($qcld_express_details_button) && $qcld_express_details_button == 'on' ){
      
              $html .='<a href="#"  class="qcld_express_product_details" product-id="'.$P->ID.'">
                  <i class="fa fa-info-circle" aria-hidden="true"></i>
              </a>';
            }
           
            $html .='</p></div>';
        }

        //Tag show or not
        if(isset($qcld_express_tag) && $qcld_express_tag == 'on' ){
         
        $html .='<div class="qcld-express-cell qcld-express-desc-cell">';
              $html .='<p class="qcld-express-desc">';
                
                $qcld_output = array();

                $qcld_terms = wp_get_post_terms( $P->ID, 'product_tag' );

                if( count($qcld_terms) > 0 ){
                    foreach($qcld_terms as $term){
                        $term_id = $term->term_id;
                        $term_name = $term->name;
                        $term_slug = $term->slug;
                        $term_link = get_term_link( $term, 'product_tag' );

                        $qcld_output[] = '<a href="'.$term_link.'">'.$term_name.'</a>';
                    }
                    $qcld_output = implode( ', ', $qcld_output );

                    $html .= $qcld_output;
                }
                
             $html .='</p></div>';
        }


        //Availability show or not
        if (isset($qcld_express_stock) && $qcld_express_stock == 'on') {
            $stock = get_post_meta( get_the_ID(), '_stock', true );
            $html .= '<div class="qcld-express-cell qcld-express-avail-cell"><p class="qcld-express-avail">';
           
            if ($stock > 0) {
                $html .= '<span class="qcld-express-avail-in"> '.$qcld_express_lang_in_stock.' (' . $product->get_stock_quantity() . ')</span>';
            } else if($stock=='') {
                $stock_status = get_post_meta( $P->ID, '_stock_status', true );
                if( $stock_status == "instock" ){ 
                    $html .= '<span class="qcld-express-avail-out"> '.$qcld_express_lang_in_stock.' </span>';
                }else if( $stock_status == "outofstock" ){
                    $html .= '<span class="qcld-express-avail-outs"> '.$qcld_express_lang_out_of_stock.' </span>';
                } 
            }else if($stock<0) {
                $html .= '<span class="qcld-express-avail-out">'.$qcld_express_lang_out_of_stock.'</span>';
            }
            $html .= '</p></div>';
        }


        //Price show or not
        if (isset($qcld_express_price) && $qcld_express_price == 'on') {
            $html .= '<div class="qcld-express-cell qcld-express-price-cell"><p class="qcld-express-price">' . $product->get_price_html() . '</p></div>';
        }
        // Action show or not
        if (isset($qcld_express_action) && $qcld_express_action == 'on') {
            $html .= '<div class="qcld-express-cell qcld-express-action-cell"><ul class="qcld-express-action">';
            
            if ($product->get_type() == 'simple') {



                $qcld_woo_minmax_enable_plugin = get_option( 'qcld_woo_minmax_enable_plugin' );
                if(function_exists('qcld_woo_minmax_install') && isset($qcld_woo_minmax_enable_plugin) && $qcld_woo_minmax_enable_plugin  == 'yes'){

                    $response_min_max = qcld_woo_minmax_express_add_rules_text($P->ID,0);

                    $min_qty  = $response_min_max['min'];
                    $max_qty  = $response_min_max['max'];
                    $step_qty = $response_min_max['step'];

                $html.='<li><input type="number" name="qcld_quantity" class="qcld-express-simple-p-qnty" value="'.($min_qty ? $min_qty : 1).'" step="'.($step_qty ? $step_qty : '').'" min="'.($min_qty ? $min_qty : '').'" max="'.($max_qty ? $max_qty : '').'"></li>';
                }else{

                $html.='<li><input type="number" value="1" name="qcld_quantity" class="qcld-express-simple-p-qnty"></li>';
                } 
                

              $stock = get_post_meta( $P->ID, '_stock', true );
              $stock_status = get_post_meta( $P->ID, '_stock_status', true );
              if( $stock > 0 ){

                    if (isset($qcld_express_cart_mode) && $qcld_express_cart_mode == 'wooinstants') {

                        $qcld_woo_minmax_enable_plugin = get_option( 'qcld_woo_minmax_enable_plugin' );
                        if(function_exists('qcld_woo_minmax_install') && isset($qcld_woo_minmax_enable_plugin) && $qcld_woo_minmax_enable_plugin  == 'yes'){

                            $response_min_max = qcld_woo_minmax_express_add_rules_text($P->ID,0);
                            $min_qty  = $response_min_max['min'];
                          }else{
                            $min_qty  = 1;

                          }
                     
                     $html .= ' <li><a  href="#"  class="ajax_add_to_cart add_to_cart_button product_type_simple" data-quantity="'.($min_qty ? $min_qty : 1).'" data-product_id="' . $P->ID . '" p-id="' . $P->ID . '" p-price="' . $product->get_price() . '" ><i class="fa fa-shopping-cart"></i></a></li>';
                    }else{
                     $html .= ' <li><a  href="#"  class="qcld_express_s_p" p-id="' . $P->ID . '"p-price="' . $product->get_price() . '" ><i class="fa fa-shopping-cart"></i></a></li>';
                    }

               } else if( $stock == "" ){
               if( $stock_status == "instock" ){
                     if (isset($qcld_express_cart_mode) && $qcld_express_cart_mode == 'wooinstants') {

                        $qcld_woo_minmax_enable_plugin = get_option( 'qcld_woo_minmax_enable_plugin' );
                        if(function_exists('qcld_woo_minmax_install') && isset($qcld_woo_minmax_enable_plugin) && $qcld_woo_minmax_enable_plugin  == 'yes'){

                            $response_min_max = qcld_woo_minmax_express_add_rules_text($P->ID,0);
                            $min_qty  = $response_min_max['min'];
                          }else{
                            $min_qty  = 1;

                          }
                     
                     $html .= ' <li><a  href="#"  class="ajax_add_to_cart add_to_cart_button product_type_simple" data-quantity="'.($min_qty ? $min_qty : 1).'" data-product_id="' . $P->ID . '" p-id="' . $P->ID . '" p-price="' . $product->get_price() . '" ><i class="fa fa-shopping-cart"></i></a></li>';
                    }else{
                     $html .= ' <li><a  href="#"  class="qcld_express_s_p" p-id="' . $P->ID . '"p-price="' . $product->get_price() . '" ><i class="fa fa-shopping-cart"></i></a></li>';
                    }

                } else if( $stock_status == "outofstock" ){ 
                    $html .= ' <li><a href="#" class="qcld_express_out_of_pro" p-id="' . $P->ID . '"p-price="' . $product->get_price() . '" > <i class="fa fa-shopping-cart"></i> </a></li>';

                 } 
                }else if( $stock < 1 ){ 
                     $html .= ' <li><a  href="#"  class="qcld_express_s_p" p-id="' . $P->ID . '"p-price="' . $product->get_price() . '" ><i class="fa fa-shopping-cart"></i></a></li>';

                } 


            }
            
            if($product->get_type() == 'external'){

              $link = sprintf( '<a rel="nofollow" href="%s" data-quantity="%s" data-product_id="%s" data-product_sku="%s" class="%s" target="_blank">%s</a>',
                      esc_url( $product->add_to_cart_url() ),
                      esc_attr( isset( $quantity ) ? $quantity : 1 ),
                      esc_attr( $product->get_id() ),
                      esc_attr( $product->get_sku() ),
                      esc_attr( isset( $class ) ? $class : 'qcld_product_type_external' ),
                      esc_html( $product->add_to_cart_text() )
                      );
              $html .= $link;

            }else if ($product->get_type() == 'variable') {
                

              $stock = get_post_meta( $P->ID, '_stock', true );
              $stock_status = get_post_meta( $P->ID, '_stock_status', true );
              if( $stock > 0 ){
              $html .= ' <li><a href="#" class="qcld_express_v_p" p-id="' . $P->ID . '"><i class="fa fa-shopping-cart"></i></a></li>';

               } else if( $stock == "" ){
               if( $stock_status == "instock" ){
                    $html .= ' <li><a href="#" class="qcld_express_v_p" p-id="' . $P->ID . '"><i class="fa fa-shopping-cart"></i></a></li>';

                } else if( $stock_status == "outofstock" ){ 
                    $html .= ' <li><a href="#" class="qcld_express_out_of_pro" p-id="' . $P->ID . '"><i class="fa fa-shopping-cart"></i></a></li>';

                 } 
                }else if( $stock < 1 ){ 
                    $html .= ' <li><a href="#" class="qcld_express_v_p" p-id="' . $P->ID . '"><i class="fa fa-shopping-cart"></i></a></li>';
                } 

            }

            $html .= '</ul>';

            $qcld_woo_minmax_dynamic_pricing_setting = get_option('qcld_woo_minmax_dynamic_pricing_setting');

            if(function_exists('qcld_woo_minmax_install') && (isset($qcld_woo_minmax_dynamic_pricing_setting['qcld_woo_minmax_price_table_on_off']) && $qcld_woo_minmax_dynamic_pricing_setting['qcld_woo_minmax_price_table_on_off'] == 'enable') ){

              $result_show = qcld_woo_minmax_express_dynamic_discount_price_table_show($P->ID);
              $html .= $result_show;
            }
            $html .= '</div>';
        }//End of show or not
        $html .= '</div>';

        $counter++;
    } //End of loop


    $response = array( 'offset' => $nextOffset, 'cat_type_id' => $p_id, 'html' => $html );
    echo wp_send_json($response);


}
}