<?php
if(get_theme_mod('popular_area_disable') != 'on' ){
	if( luzuk_lite_is_wc_active() ) {
	?>
<?php 
	if( get_theme_mod('popular_areaTpadding',true) ) {
		$popular_areaTpadding = 'padding-top:'.esc_attr(get_theme_mod('popular_areaTpadding')).';';
	}
	if( get_theme_mod('popular_areaBpadding',true) ) {
		$popular_areaBpadding = 'padding-bottom:'.esc_attr(get_theme_mod('popular_areaBpadding')).';';
	}
?>			
<div class="popular-area " id="popular">		  
	<div class="container" >
		<?php
			$showStatic = true;
			for( $i = 1; $i < 11; $i++ ){
				$popularus_page_id = get_theme_mod('popularus_page_icon'.$i); 
				if(!empty($popularus_page_id)){
					$showStatic = false;
					break;
				}
			}
		?>
	    <?php
	        $popular_title = get_theme_mod('popular_title', 'Popular Product');  
	        $popular_text = get_theme_mod('popular_text', 'Discover the Southern Flavors Within Our Wines');  
	        $popbtn_text = get_theme_mod('popbtn_text', 'SHOP ONLINE NOW');
	    ?>
	    <?php if( $popular_title || $popular_text){ ?>
	    	<div class="section-title" >
		    		<?php if( $popular_title){ ?>
		          <h2><?php echo ($popular_title);  ?></h2>
		         <?php } ?>
		        <?php if( $popular_text){ ?>
		          <p><?php echo ($popular_text);  ?></p>
		         <?php } ?>
		    </div>   
	    <?php } ?>
		<div class="popularus-post-wrap">
			<div class="popularus-post-boxes">
				<div class="row">
				    <?php
					    $args = array(
					        'post_type' => 'product',
					        'meta_key' => 'total_sales',
					        'orderby' => 'popularity',
					        'order' => 'DESC',
					        'posts_per_page' => 8,
					    );
					    $loop = new WP_Query($args);
					    while ($loop->have_posts()) : $loop->the_post();
					        global $product;
					        $product_id = $product->get_id();
							//$wishlist_url = esc_url(add_query_arg('add_to_wishlist', $product_id));
					        $sale_text = get_post_meta(get_the_ID(), '_custom_product_salenew_field', true);
					        $discount_text = get_post_meta(get_the_ID(), '_custom_product_discount_field', true);
				    ?>
				    <div class="col-xl-3 col-lg-3 col-md-6 col-sm-6">
				      <div class="popularbx" data-wow-duration="1s">
				        <div class="popularus-single">
				          <div class="popular-box">
				            <div class="hi-icon">
						        <div class="product-gallery">
		                    		<a id="id-<?php the_id(); ?>" href="<?php the_permalink(); ?>" title="<?php the_title(); ?>">
			                    			<?php
				                            if (has_post_thumbnail($loop->post->ID)) {
				                                echo get_the_post_thumbnail($loop->post->ID, 'shop_catalog');
				                            } else {
				                                echo '<img src="' . get_template_directory_uri() . '/images/default.png" alt="featured products" />';
				                            }
			                          ?>
			                      	</a>
					              	<div class="popular-icons">
										<div class="popular-list tooltip-container">
											<?php 
												$user_id = get_current_user_id();
												$product_id = $product->get_id();
												$wishlist = get_user_meta($user_id, 'wishlist', true);
												$button_class = in_array($product_id, (array)$wishlist) ? 'added' : '';
												$nonce = wp_create_nonce('update_wishlist_' . $product_id);

												$wishlist_url = esc_url(add_query_arg(
												    array(
												        'update_wishlist' => $product_id,
												        '_wpnonce' => $nonce
												    )
												));

												echo '<a href="' . $wishlist_url . '" class="add-to-wishlist-button ' . $button_class . '">
												    <span class="spinner"></span>
												    <i class="fa fa-heart' . ($button_class ? '' : '-o') . '" aria-hidden="true"></i>
												    <span class="tooltip-text">' . ($button_class ? 'Added To Wishlist' : 'Add To Wishlist') . '</span>
												</a>'; 
											?>
										</div>
									    <div class="popular-list tooltip-container">
									        <a href="<?php echo esc_url( $product->add_to_cart_url() ); ?>" data-quantity="1" class="button product_type_<?php echo esc_attr( $product->get_type() ); ?> add_to_cart_button">
									            <span class="spinner"></span><i class="fa fa-shopping-cart"></i><span class="tooltip-text">Add to Cart</span>
									        </a>
									    </div>
									    <div class="popular-list tooltip-container">
									        <a href="#quick-view-<?php the_id(); ?>" class="quick-view" data-product-id="<?php echo esc_attr( $product->get_id() ); ?>">
									            <span class="spinner"></span><i class="fa fa-eye"></i><span class="tooltip-text">Quick View</span>
									        </a>
									    </div>
									</div>
							    </div>
			                    <div class="product_label">
				                    <?php  if (!empty($sale_text)) : ?>
					                    <div class="product-discount-data">
											<span class="product-discount"><?php echo ($sale_text);  ?></span>
										</div>
									<?php endif ?>
									<?php  if (!empty($discount_text)) : ?>
							          	<div class="product-discount-data">
											<span class="product-discount"><?php echo ($discount_text);  ?></span>
										</div>
									<?php endif ?>
								</div>
			              	</div>
			                <div id="quick-view-<?php the_id(); ?>" class="quick-view-popup">
								<div class="quick-view-content">
							        <button class="close-quick-view">
							        	<svg viewBox="0 0 28 28" fill="none" >
							        		<g id="SVGRepo_bgCarrier" stroke-width="0"></g>
							        		<g id="SVGRepo_tracerCarrier" stroke-linecap="round" stroke-linejoin="round"></g>
							        		<g id="SVGRepo_iconCarrier"> <path fill-rule="evenodd" clip-rule="evenodd" d="M5.46967 5.46967C5.76256 5.17678 6.23744 5.17678 6.53033 5.46967L18.5303 17.4697C18.8232 17.7626 18.8232 18.2374 18.5303 18.5303C18.2374 18.8232 17.7626 18.8232 17.4697 18.5303L5.46967 6.53033C5.17678 6.23744 5.17678 5.76256 5.46967 5.46967Z" fill="#000000"></path> 
							        			<path fill-rule="evenodd" clip-rule="evenodd" d="M18.5303 5.46967C18.8232 5.76256 18.8232 6.23744 18.5303 6.53033L6.53035 18.5303C6.23745 18.8232 5.76258 18.8232 5.46969 18.5303C5.17679 18.2374 5.17679 17.7626 5.46968 17.4697L17.4697 5.46967C17.7626 5.17678 18.2374 5.17678 18.5303 5.46967Z" fill="#000000"></path> 
							        		</g>
							        	</svg>
							        </button>
									<div class="product-details">
							        	<div class="row">
							        		<div class="col-lg-4 col-md-5">
							        			<div class="product-image">
									                <?php
									                if (has_post_thumbnail($loop->post->ID)) {
									                    echo get_the_post_thumbnail($loop->post->ID, 'shop_single');
									                } else {
									                    echo '<img src="' . get_template_directory_uri() . '/images/default.png" alt="featured products" />';
									                }
									                ?>
									            </div>
							        		</div>
							        		<div class="col-lg-8 col-md-7">
							        			<h2><?php the_title(); ?></h2>
							        			<div class="product-price">
									                <?php echo $product->get_price_html(); ?>
									            </div>
							        			 <div class="product-description">
									                <?php the_content(); ?>
									            </div>
									            <div class="product-button">
										            <div class="product-add-to-cart">
										              	<a href="<?php echo esc_url( $product->add_to_cart_url() ); ?>" data-quantity="1" class="button product_type_<?php echo esc_attr( $product->get_type() ); ?> add_to_cart_button">
													            <i class="fa fa-shopping-cart"></i><span class="tooltip-text">Add to Cart</span>
													    </a>
										            </div>
											        <div class="product-wishlist">
											            <a href="<?php echo $wishlist_url; ?>" class="add-to-wishlist-button <?php echo $button_class; ?>">
													        <!-- <span class="spinner"></span> -->
													        <i class="fa fa-heart<?php echo $button_class ? '' : '-o'; ?>" aria-hidden="true"></i>
													        <span class="tooltip-text"><?php echo $button_class ? 'Added To Wishlist' : 'Add To Wishlist'; ?></span>
													    </a>
										            </div>
										            <div class="product-buy-now">
										            	<?php 
										            		$buy_now_url = wc_get_cart_url() . '?add-to-cart=' . $product->get_id();
										            	?>
												        <a href="<?php echo esc_url($buy_now_url); ?>" class="button buy-now-button">
												            <i class="fa fa-credit-card"></i><span class="tooltip-text">Buy Now</span>
												        </a>
												    </div>
										        </div>
							        		</div>
							        	</div>
							        </div>
						    	</div>
							</div>
			            </div>
		                <div class="popular-area-data">
		                    <a id="id-<?php the_id(); ?>" href="<?php the_permalink(); ?>" title="<?php the_title(); ?>">
		                        <h4 class="inner-area-title"><?php the_title(); ?></h4>
		                    </a>
		                    
		                    <div class="price">
		                        <div class="popular-price"><?php echo $product->get_price_html(); ?></div>
		                    </div>
		                </div>
		            </div>
		        </div>
		    </div>
		<?php endwhile;
		wp_reset_query(); ?>
	</div>
</div>
<div class="popular-btn">
		<?php if($popbtn_text ){ ?>	
		<a href="<?php echo esc_url(wc_get_page_permalink('shop')); ?>">
        	<?php echo ($popbtn_text);  ?>
		</a>
	<?php }?>
</div>
</div>
</div>
</div>	<?php } ?><?php }  