<?php
/**
 * The template for displaying product content within loops
 *
 * This template can be overridden by copying it to yourtheme/woocommerce/content-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 9.4.0
 */

defined( 'ABSPATH' ) || exit;

global $product;

// Check if the product is a valid WooCommerce product and ensure its visibility before proceeding.
if ( ! is_a( $product, WC_Product::class ) || ! $product->is_visible() ) {
	return;
}

if ( ot_get_option( 'shopgclayout' ) === 'shopwithputsidebar' ) {
    $gc_shop_product_layout_class = 'col-12 col-sm-6 col-md-4 col-lg-3';
  } else {
    $gc_shop_product_layout_class = 'col-12 col-sm-6 col-md-4';
}

?>
<div class="<?php echo $gc_shop_product_layout_class; ?>">
	<div <?php wc_product_class( 'productareagm', $product ); ?>>
		<a href="<?php the_permalink(); ?>">
			<figure <?php if ( !$product->is_in_stock() ) { echo 'class="gc-outstock-img-filter"'; } ?>>
				<?php $image_gcarch = wp_get_attachment_image_src( get_post_thumbnail_id( $id ), 'single-post-thumbnail' );
					if (!$image_gcarch) { echo wc_placeholder_img(); } else { the_post_thumbnail( 'main-thumb' ); }
				?>
			</figure>
			<div class="pddata">
				<div class="pdname">
					<h3><?php the_title(); ?></h3>
				</div>
				<div class="pdprice">
					<div class="afterpricegm">
						<?php if ( $product->is_in_stock() ) { echo $product -> get_price_html(); } else {echo '<div class="gc-archive-outstock">ناموجود</div>';} ?>
					</div>
				</div>
			</div>
<?php $is_on_sale = $product->is_on_sale();

if ( $is_on_sale && !$product->is_type( 'variable' )) { ?>
<div class="pdoffpercent">
%<?php echo gajcommerce_change_number($percentage = round( ( ( $product->get_regular_price() - $product->get_sale_price() ) / $product->get_regular_price() ) * 100 )); ?>
</div>
<?php } elseif ($is_on_sale && $product->is_type( 'variable' )) { ?>
<div class="pdoffpercent">حراج</div>
<?php } ?>
		</a>
		<?php if ( $product->is_type( 'simple' ) && $product->is_in_stock() && ot_get_option( 'archiveaddtocartbuttongm', 'on' ) === 'on'  ) { ?>
			<?php echo sprintf( '<a href="%s" data-quantity="1" class="%s" %s> <i class="fas fa-cart-plus"></i> %s</a>',
					esc_url( $product->add_to_cart_url() ),
					esc_attr( implode( ' ', array_filter( array(
							'button', 'product_type_' . $product->get_type(),
							$product->is_purchasable() && $product->is_in_stock() ? 'add_to_cart_button' : '',
							$product->supports( 'ajax_add_to_cart' ) ? 'ajax_add_to_cart' : '', 'hoveraddtocartgm', 'single_add_to_cart_button',
					) ) ) ),
					wc_implode_html_attributes( array(
							'data-product_id'  => $product->get_id(),
							'data-product_sku' => $product->get_sku(),
							'aria-label'       => $product->add_to_cart_description(),
							'rel'              => 'nofollow',
					) ),
					esc_html( $product->add_to_cart_text() )
			); ?>
		<?php } elseif ($product->is_type( 'variable' ) && ot_get_option( 'archiveaddtocartbuttongm', 'on' ) === 'on' ) { ?>
			<a href="<?php the_permalink(); ?>" class="hoveraddtocartgm" target="_blank"><i class="fas fa-list-ul"></i> مشاهده گزینه‌ها</a>
		<?php } ?>
	</div>
</div>
