<?php
defined('ABSPATH') || exit;

global $product;

// Ensure $product is valid
if (!$product || !$product->is_visible()) {
    return;
}

// Get category information
$category_ids = $product->get_category_ids();
$first_category = null;

if (!empty($category_ids)) {
    $first_category_id = $category_ids[0];
    $category = get_term($first_category_id, 'product_cat');

    if (!is_wp_error($category)) {
        $first_category = [
            'title' => $category->name,
            'link' => get_term_link($first_category_id, 'product_cat')
        ];
    }
}
?>
<div class="product-tech-two d-flex align-items-center h-100 position-relative">
    <a href="<?= get_permalink( $product->get_id() ); ?>" class="stretched-link" aria-label="<?php
$product_name = $product->get_name();
$show_full_titles = kaveh_get_option('kaveh_product_title_display', false);
if ($show_full_titles) {
    echo esc_html($product_name);
} else {
    if ( wp_is_mobile() ) {
        echo wp_trim_words( $product_name, 5, '...' );
    } else {
        echo wp_trim_words( $product_name, 8, '...' );
    }
}
?>"></a>
    <?php if ($product->is_on_sale()): ?>
    <span class="discount d-flex align-items-center justify-content-center position-absolute end-0">            <?php if (class_exists('kavehWoo')) {
                $offer_percentage = kavehWoo::showofferpercentage($product);
                echo $offer_percentage;
            } ?> </span>
    <?php endif; ?>
    <img src="<?php if (get_the_post_thumbnail_url()): ?><?php the_post_thumbnail_url('full'); ?><?php else: ?><?php echo wc_placeholder_img_src(); ?><?php endif; ?>"
        alt="<?php
$product_name = $product->get_name();
if ( wp_is_mobile() ) {
    echo wp_trim_words( $product_name, 5, '...' );
} else {
    echo wp_trim_words( $product_name, 8, '...' );
}
?>" width="76" height="92"
        class="flex-fill flex-grow-0 flex-shrink-0">
    <div class="detail flex-grow-1 w-100">
        <h2 class="title overflow-hidden mb-0"> <?php
$product_name = $product->get_name();
if ( wp_is_mobile() ) {
    echo wp_trim_words( $product_name, 5, '...' );
} else {
    echo wp_trim_words( $product_name, 8, '...' );
}
?> </h2>
        <?php if ($first_category): ?>
        <div class="category"> <a href="<?php echo esc_url($first_category['link']); ?>"><?php echo esc_html($first_category['title']); ?></a> </div>
        <?php endif; ?>
        <div class="d-flex align-items-center justify-content-between gap-2">
            <div class="price"> 
            <?php
            // Use WooCommerce's built-in price HTML output
            $price_html = $product->get_price_html();
            // If we have price HTML (not empty)
            if ($price_html) {
                echo $price_html;
            }
            ?>
            </div>
            <a href="<?= get_permalink( $product->get_id() ); ?>">
            <div class="btn-view d-none d-md-flex align-items-center justify-content-center">
                <i class="isax isax-eye3 lh-1"></i>
            </div>
            </a>
        </div>
    </div>
</div>