<?php
/**
 * Related Products
 *
 * This template can be overridden by copying it to yourtheme/woocommerce/single-product/related.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     10.3.0
 */

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

if ( $related_products ) :
	/**
	 * Ensure all images of related products are lazy loaded by increasing the
	 * current media count to WordPress's lazy loading threshold if needed.
	 * Because wp_increase_content_media_count() is a private function, we
	 * check for its existence before use.
	 */
	if ( function_exists( 'wp_increase_content_media_count' ) ) {
		$content_media_count = wp_increase_content_media_count( 0 );
		if ( $content_media_count < wp_omit_loading_attr_threshold() ) {
			wp_increase_content_media_count( wp_omit_loading_attr_threshold() - $content_media_count );
		}
	}
	?>

	<section class="related products">

		<?php
		$heading = apply_filters( 'woocommerce_product_related_products_heading', __( 'Related products', 'classic-toy-store' ) );

		if ( $heading ) :
			?>
			<h2><?php echo esc_html( $heading ); ?></h2>
		<?php endif; ?>
        <?php woocommerce_product_loop_start(); ?>
            <div class="row">
                <?php foreach ( $related_products as $related_product ) : 
                    $post_object = get_post( $related_product->get_id() );
                    setup_postdata( $GLOBALS['post'] = $post_object ); 
                    global $product;

                    $classic_toy_store_regular_price = $product->get_regular_price();
                    $classic_toy_store_sale_price = $product->get_sale_price();
                    $classic_toy_store_discount_percentage = 0;

                    if ($classic_toy_store_regular_price && $classic_toy_store_sale_price) {
                        $classic_toy_store_discount_percentage = 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_discount_percentage != 0) { ?>
                                <div class="discount-badge position-absolute">
                                    <?php echo esc_html__('OFF', 'classic-toy-store'); ?> <?php echo $classic_toy_store_discount_percentage; ?>%
                                </div>
                            <?php } ?>

                            <?php 
                            if (has_post_thumbnail()) {
                                the_post_thumbnail('shop_catalog');
                            } else {
                                echo '<img src="' . esc_url(wc_placeholder_img_src()) . '"/>';
                            } 
                            ?>
                        </div>
                        <h3 class="text-capitalize product-heading">
                            <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
                        </h3>
                        <div class="row">
                            <div class="col-xl-9 col-lg-8 col-md-9 col-12">
                                <div class="price"><?php echo $product->get_price_html(); ?></div>
                            </div>
                            <div class="col-xl-3 col-lg-4 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 endforeach; ?>
            </div>
        <?php woocommerce_product_loop_end(); ?>
	</section>
	<?php
endif;

wp_reset_postdata();