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

global $product;

if (empty($product) || !$product->is_visible()) {
  return;
}
?>

<article <?php wc_product_class('fjord-product', $product); ?>>
  <div class="fjord-product__badge"><?php the_title(); ?></div>
  <?php
  $discount_percent = null;
  if ($product->is_on_sale()) {
    $regular_price = (float) $product->get_regular_price();
    $sale_price = (float) $product->get_sale_price();
    if ($regular_price > 0 && $sale_price > 0 && $sale_price < $regular_price) {
      $discount_percent = (int) round((($regular_price - $sale_price) / $regular_price) * 100);
    }
  }
  ?>
  <?php if ($discount_percent) : ?>
    <span class="fjord-product__discount"><?php echo esc_html($discount_percent); ?>% OFF</span>
  <?php endif; ?>
  <a class="fjord-product__image" href="<?php the_permalink(); ?>">
    <?php echo $product->get_image('woocommerce_single'); ?>
  </a>
  <a class="fjord-product__cta" href="<?php the_permalink(); ?>">
    <span><?php echo wp_kses_post($product->get_price_html()); ?></span>
    <span class="fjord-product__cta-link">Ver</span>
  </a>
</article>
