<?php

    wp_enqueue_script('owl-carousel');

  	global $wpdb, $woocommerce, $woocommerce_loop, $yit_products_is_slider;

    $yit_products_is_slider = true;

  	if ( isset( $latest ) && $latest == 'yes' ) {
        $orderby = 'date';
        $order = 'desc'; 
    }
	
  	$args = array(
		'post_type'	=> array( 'product', 'product_variation' ),
		'post_status' => 'publish',
		'posts_per_page' => -1,
		'ignore_sticky_posts'	=> 1,
		'meta_query' => '',
        'fields' => 'id=>parent'
	);
	
	if(isset( $featured) && $featured == 'yes' ){
    	$args['meta_query'][] = array(
      		'key' 		=> '_featured',
      		'value' 	=> 'yes'
    	);
  	}
	
	if(isset( $best_sellers) && $best_sellers == 'yes' ){
		$args['meta_key'] = 'total_sales';
    	$args['orderby'] = 'meta_value';
    	$args['order'] = 'desc';
  	}
	
	if(isset( $on_sale) && $on_sale == 'yes' ){
		$args['meta_key'] = '_sale_price';
		$args['meta_compare'] = '>=';
		$args['meta_value'] = 0;

        $sale_products = get_posts( $args );
        $product_ids 	= array_keys( $sale_products );
        $parent_ids		= array_values( $sale_products );

        // Check for scheduled sales which have not started
        foreach ( $product_ids as $key => $id ) {
            if ( get_post_meta( $id, '_sale_price_dates_from', true ) > current_time('timestamp') ) {
                unset( $product_ids[ $key ] );
            }
        }

        $product_ids_on_sale = array_unique( array_merge( $product_ids, $parent_ids ) );

        set_transient( 'wc_products_onsale', $product_ids_on_sale );
  	}

    $query_args = array(
        'posts_per_page' 	=> $per_page,
        'no_found_rows' => 1,
        'post_status' 	=> 'publish',
        'post_type' 	=> 'product',
        'orderby' 		=> $orderby,
        'order' 		=> $order,
        'meta_query' 	=> $args['meta_query'],
    );
	
	if(isset($atts['skus'])){
		$skus = explode(',', $atts['skus']);
	  	$skus = array_map('trim', $skus);
        $query_args['meta_query'][] = array(
      		'key' 		=> '_sku',
      		'value' 	=> $skus,
      		'compare' 	=> 'IN'
    	);
  	}
	
	if(isset($atts['ids'])){
		$ids = explode(',', $atts['ids']);
	  	$ids = array_map('trim', $ids);
        $query_args['post__in'] = $ids;
	}

    if ( isset( $category ) && $category!= 'null' && $category != 'a:0:{}' && $category != '0' && $category!="0, ") {
        $query_args['product_cat'] = $category;
    }

    if ( $on_sale == 'yes' ) {
        if( empty( $product_ids_on_sale ) )
            { return; }

        $query_args['post__in'] = $product_ids_on_sale;
    }
    
    $woocommerce_loop['setLast'] = true;

	$products = new WP_Query( $query_args );
	
	$woocommerce_loop['view'] = 'grid';
    $woocommerce_loop['layout'] =  ( isset( $layout ) && $layout != 'default' ) ? $layout : '';
    $i = 0;

    ob_start();

	if ( $products->have_posts() ) :
	    echo '<div class="woocommerce">';
		echo '<div class="products-slider-wrapper" data-columns="%columns%"  data-autoplay="'.$autoplay.'"><div class="products-slider">';
		if (isset($title) && $title != '')
			echo '<h4>'.$title.'</h4>';
		else
			echo '<h4>&nbsp;</h4>';
		echo '<div class="row"><ul class="products yit_products_slider">';
        while ( $products->have_posts() ) : $products->the_post();

            if ( function_exists( 'wc_get_template_part')){
                wc_get_template_part('content', 'product');
            }else{
                woocommerce_get_template_part( 'content', 'product' );
            }

	        $i++;
		endwhile; // end of the loop.
		echo '</ul></div>';
		echo '<div class="es-nav"><span class="es-nav-prev">Previous</span><span class="es-nav-next">Next</span></div>';
		echo '</div></div><div class="es-carousel-clear"></div>';
		echo '</div>';
	endif;

    echo do_shortcode('[clear]');

    $content = ob_get_clean();
    echo str_replace('%columns%', $woocommerce_loop['columns'] ,$content);

	wp_reset_query();
	                         
	$woocommerce_loop['loop'] = 0;        
	unset( $woocommerce_loop['setLast'] );
$yit_products_is_slider = false;
?>