<?php
/**
 * The Template for displaying product archives, including the main shop page which is a post type archive.
 *
 * This template can be overridden by copying it to yourtheme/woocommerce/archive-product.php.
 *
 * HOWEVER, on occasion WooCommerce will need to update template files and you
 * (the theme developer) will need to copy the new files to your theme to
 * maintain compatibility. We try to do this as little as possible, but it does
 * happen. When this occurs the version of the template file will be bumped and
 * the readme will list any important changes.
 *
 * @see https://woocommerce.com/document/template-structure/
 * @package WooCommerce\Templates
 * @version 8.6.0
 */

 defined( 'ABSPATH' ) || exit;

 get_header( 'shop' ); ?>
 
 <?php
  $classic_toy_store_shop_page_sidebar = get_theme_mod( 'classic_toy_store_shop_page_sidebar',false );
  if ( false == $classic_toy_store_shop_page_sidebar ) {
	$classic_toy_store_colmd = 'col-lg-12 col-md-12';
  } else { 
	$classic_toy_store_colmd = 'col-lg-9 col-md-9';
  } 
 ?>
 
 <div class="box-image">
   <div class="single-page-img"></div>
  	<div class="page-header">
    	<h2><?php woocommerce_page_title(); ?></h2>  
     	<span><?php classic_toy_store_the_breadcrumb(); ?></span>
  	</div>
 </div>

 <div class="container">
	 <main role="main" id="maincontent">
		 <div class="row m-0">
			 <?php if(get_theme_mod( 'classic_toy_store_shop_page_sidebar_position','Right Sidebar') == 'Right Sidebar'){ ?>
				 <div class="<?php echo esc_attr( $classic_toy_store_colmd ); ?>">
					 <?php
					 /**
					  * Hook: woocommerce_before_main_content.
					  *
					  * @hooked woocommerce_output_content_wrapper - 10 (outputs opening divs for the content)
					  * @hooked woocommerce_breadcrumb - 20
					  * @hooked WC_Structured_Data::generate_website_data() - 30
					  */
					 do_action( 'woocommerce_before_main_content' );
 
					 /**
					  * Hook: woocommerce_shop_loop_header.
					  *
					  * @since 8.6.0
					  *
					  * @hooked woocommerce_product_taxonomy_archive_header - 10
					  */
					 do_action( 'woocommerce_shop_loop_header' );
 
					 if ( woocommerce_product_loop() ) {
 
						 /**
						  * Hook: woocommerce_before_shop_loop.
						  *
						  * @hooked woocommerce_output_all_notices - 10
						  * @hooked woocommerce_result_count - 20
						  * @hooked woocommerce_catalog_ordering - 30
						  */
						 do_action( 'woocommerce_before_shop_loop' );
 
						 woocommerce_product_loop_start(); ?>

							<div class="row">
								<?php
								if ( wc_get_loop_prop( 'total' ) ) {
								while ( have_posts() ) {
										the_post();
								global $product;
								global $product_id;
								global $loop;

								/**
								 * Hook: woocommerce_shop_loop.
								 *
								 * @hooked WC_Structured_Data::generate_product_data() - 10
								 */
								do_action( 'woocommerce_shop_loop' );

								$classic_toy_store_assigned_category = get_the_terms( $product_id, 'product_cat' );

								$classic_toy_store_assigned_category_name = '';$classic_toy_store_category_link = '#';
								if (!empty($classic_toy_store_assigned_category)) {
								$classic_toy_store_assigned_category_name = $classic_toy_store_assigned_category[0]->name;
								$classic_toy_store_category_link = get_category_link( $classic_toy_store_assigned_category[0]->term_id );
								}
								$classic_toy_store_regular_price = $product->get_regular_price();
                  $classic_toy_store_sale_price = $product->get_sale_price();
                  $classic_toy_store_percentage_discount = 0;

                  if ($classic_toy_store_regular_price && $classic_toy_store_sale_price) {
                      $classic_toy_store_percentage_discount = 1 * round( ( ($classic_toy_store_regular_price - $classic_toy_store_sale_price) / $classic_toy_store_regular_price ) * 100 ); 
                  }
								?>
								<div class="col-xl-3 col-lg-3 col-md-6 col-12 mb-4">
									<div class="product-content position-relative">
										<div class="product-image position-relative">
											<?php if($classic_toy_store_percentage_discount != 0) { ?>
												<div class="discount-badge position-absolute"><?php echo esc_html__('OFF' ,'classic-toy-store'); ?> <?php echo $classic_toy_store_percentage_discount; ?>%</div>
											<?php } ?>

											<?php
											if (has_post_thumbnail()) {
												the_post_thumbnail();
											} else {
												echo '<img src="' . esc_url(wc_placeholder_img_src()) . '"/>';
											}
											?>
										</div>
										<div class="row">
											<div class="col-xl-9 col-lg-9 col-md-9 col-12">
												<?php 
													$classic_toy_store_product_categories = get_the_terms( get_the_ID(), 'product_cat' );
													if ( $classic_toy_store_product_categories ) {
														foreach ( $classic_toy_store_product_categories as $classic_toy_store_category ) {
															echo '<div class="product-cat text-capitalize"><a href="' . get_term_link( $classic_toy_store_category ) . '">' . $classic_toy_store_category->name . '</a></div>';
														}
												}?>
											</div>
											<div class="col-xl-3 col-lg-3 col-md-3 col-12">
												<?php if( $product->is_type( 'simple' ) ){ ?>
													<div class="ratings">
														<?php
														$classic_toy_store_average_rating = $product->get_average_rating();
														$classic_toy_store_rating_count = $product->get_rating_count();
														if ($classic_toy_store_rating_count > 0) {
															echo '<span class="star-rating"><i class="fa-solid fa-star"></i></span>';
															echo '<span class="numeric-rating">' . esc_html($classic_toy_store_average_rating) . '</span>';
														}
														?>
													</div>
												<?php } ?>
											</div>
										</div>
										
										<h3 class="text-capitalize product-heading">
											<a href="<?php echo esc_url(get_permalink()); ?>">
												<?php the_title(); ?>
											</a>
										</h3>
										<div class="row">
											<div class="col-xl-9 col-lg-9 col-md-9 col-12">
												<div class="price">
													<?php echo $product->get_price_html(); ?>
												</div>
											</div>
											<div class="col-xl-3 col-lg-3 col-md-3 col-12">
												<div class="product-btn">
													<a href="<?php the_permalink(); ?>"><i class="fa-solid fa-plus"></i></a>
												</div>
											</div>
										</div>
									</div>
								</div>
								<?php }
								} ?>
							</div>

							<?php woocommerce_product_loop_end();
 
						 /**
						  * Hook: woocommerce_after_shop_loop.
						  *
						  * @hooked woocommerce_pagination - 10
						  */
						 do_action( 'woocommerce_after_shop_loop' );
					 } else {
						 /**
						  * Hook: woocommerce_no_products_found.
						  *
						  * @hooked wc_no_products_found - 10
						  */
						 do_action( 'woocommerce_no_products_found' );
					 }
 
					 /**
					  * Hook: woocommerce_after_main_content.
					  *
					  * @hooked woocommerce_output_content_wrapper_end - 10 (outputs closing divs for the content)
					  */
					 do_action( 'woocommerce_after_main_content' ); ?>
				 </div>
				 <?php if ( false != $classic_toy_store_shop_page_sidebar ) {?>
					 <div class="col-lg-3 col-md-3 mt-3" id="sidebar">
						 <?php dynamic_sidebar('woocommerce-sidebar'); ?>
					 </div>
				 <?php } ?>
			 <?php } elseif (get_theme_mod( 'classic_toy_store_shop_page_sidebar_position','Right Sidebar') == 'Left Sidebar') {?>
				 <?php if ( false != $classic_toy_store_shop_page_sidebar ) {?>
					 <div class="col-lg-3 col-md-3 mt-3" id="sidebar">
						 <?php dynamic_sidebar('woocommerce-sidebar'); ?>
					 </div>
				 <?php } ?>
				 <div class="<?php echo esc_attr( $classic_toy_store_colmd ); ?>">
					 <?php
					 /**
					  * Hook: woocommerce_before_main_content.
					  *
					  * @hooked woocommerce_output_content_wrapper - 10 (outputs opening divs for the content)
					  * @hooked woocommerce_breadcrumb - 20
					  * @hooked WC_Structured_Data::generate_website_data() - 30
					  */
					 do_action( 'woocommerce_before_main_content' );
 
					 /**
					  * Hook: woocommerce_shop_loop_header.
					  *
					  * @since 8.6.0
					  *
					  * @hooked woocommerce_product_taxonomy_archive_header - 10
					  */
					 do_action( 'woocommerce_shop_loop_header' );
 
					 if ( woocommerce_product_loop() ) {
 
						 /**
						  * Hook: woocommerce_before_shop_loop.
						  *
						  * @hooked woocommerce_output_all_notices - 10
						  * @hooked woocommerce_result_count - 20
						  * @hooked woocommerce_catalog_ordering - 30
						  */
						 do_action( 'woocommerce_before_shop_loop' );
 
						 woocommerce_product_loop_start(); ?>

							<div class="row">
								<?php
								if ( wc_get_loop_prop( 'total' ) ) {
								while ( have_posts() ) {
										the_post();
								global $product;
								global $product_id;
								global $loop;

								/**
								 * Hook: woocommerce_shop_loop.
								 *
								 * @hooked WC_Structured_Data::generate_product_data() - 10
								 */
								do_action( 'woocommerce_shop_loop' );

								$classic_toy_store_assigned_category = get_the_terms( $product_id, 'product_cat' );

								$classic_toy_store_assigned_category_name = '';$classic_toy_store_category_link = '#';
								if (!empty($classic_toy_store_assigned_category)) {
								$classic_toy_store_assigned_category_name = $classic_toy_store_assigned_category[0]->name;
								$classic_toy_store_category_link = get_category_link( $classic_toy_store_assigned_category[0]->term_id );
								}
								$classic_toy_store_regular_price = $product->get_regular_price();
                  $classic_toy_store_sale_price = $product->get_sale_price();
                  $classic_toy_store_percentage_discount = 0;

                  if ($classic_toy_store_regular_price && $classic_toy_store_sale_price) {
                    $classic_toy_store_percentage_discount = 1 * round( ( ($classic_toy_store_regular_price - $classic_toy_store_sale_price) / $classic_toy_store_regular_price ) * 100 ); 
                  }
								?>
								<div class="col-xl-3 col-lg-3 col-md-6 col-12 mb-4">
									<div class="product-content position-relative">
										<div class="product-image position-relative">
											<?php if($classic_toy_store_percentage_discount != 0) { ?>
												<div class="discount-badge position-absolute"><?php echo esc_html__('OFF' ,'classic-toy-store'); ?> <?php echo $classic_toy_store_percentage_discount; ?>%</div>
											<?php } ?>

											<?php
											if (has_post_thumbnail()) {
												the_post_thumbnail();
											} else {
												echo '<img src="' . esc_url(wc_placeholder_img_src()) . '"/>';
											}
											?>
										</div>
										<div class="row">
											<div class="col-xl-9 col-lg-9 col-md-9 col-12">
												<?php 
													$classic_toy_store_product_categories = get_the_terms( get_the_ID(), 'product_cat' );
													if ( $classic_toy_store_product_categories ) {
														foreach ( $classic_toy_store_product_categories as $classic_toy_store_category ) {
															echo '<div class="product-cat text-capitalize"><a href="' . get_term_link( $classic_toy_store_category ) . '">' . $classic_toy_store_category->name . '</a></div>';
														}
												}?>
											</div>
											<div class="col-xl-3 col-lg-3 col-md-3 col-12">
												<?php if( $product->is_type( 'simple' ) ){ ?>
													<div class="ratings">
														<?php
														$classic_toy_store_average_rating = $product->get_average_rating();
														$classic_toy_store_rating_count = $product->get_rating_count();
														if ($classic_toy_store_rating_count > 0) {
															echo '<span class="star-rating"><i class="fa-solid fa-star"></i></span>';
															echo '<span class="numeric-rating">' . esc_html($classic_toy_store_average_rating) . '</span>';
														}
														?>
													</div>
												<?php } ?>
											</div>
										</div>
										
										<h3 class="text-capitalize product-heading">
											<a href="<?php echo esc_url(get_permalink()); ?>">
												<?php the_title(); ?>
											</a>
										</h3>
										<div class="row">
											<div class="col-xl-9 col-lg-9 col-md-9 col-12">
												<div class="price">
													<?php echo $product->get_price_html(); ?>
												</div>
											</div>
											<div class="col-xl-3 col-lg-3 col-md-3 col-12">
												<div class="product-btn">
													<a href="<?php the_permalink(); ?>"><i class="fa-solid fa-plus"></i></a>
												</div>
											</div>
										</div>
									</div>
								</div>
								<?php }
								} ?>
							</div>

							<?php woocommerce_product_loop_end();
 
						 /**
						  * Hook: woocommerce_after_shop_loop.
						  *
						  * @hooked woocommerce_pagination - 10
						  */
						 do_action( 'woocommerce_after_shop_loop' );
					 } else {
						 /**
						  * Hook: woocommerce_no_products_found.
						  *
						  * @hooked wc_no_products_found - 10
						  */
						 do_action( 'woocommerce_no_products_found' );
					 }
 
					 /**
					  * Hook: woocommerce_after_main_content.
					  *
					  * @hooked woocommerce_output_content_wrapper_end - 10 (outputs closing divs for the content)
					  */
					 do_action( 'woocommerce_after_main_content' ); ?>
				 </div>
			 <?php }?>
		 </div>
	 </main>
 </div>
 <?php get_footer( 'shop' );  ?>  