<?php

namespace Foxstudio\Plugins\Template\Utils;

use \Foxstudio\Plugins\Template\Repository\ProductRepository;

class ShowMinimumPrice {
    
    public function __construct() 
    {

        \add_action('woocommerce_shop_loop_item_title', [$this, 'change_product_title']);
        \remove_action('woocommerce_shop_loop_item_title', 'woocommerce_template_loop_product_title', 10);

    }

    function change_product_title()
    {
        global $product;

        if (!$product) {
            return;
        }

        // $price = 85.56;
        $sku = $product->get_sku();
        $price = $product->get_price();
        $price = $this->getLowestPrice($price);
        echo '<h2 class="woocommerce-loop-product__title">' . get_the_title() . '</h2><p style="font-weight: 700;">od ' . $price . ' zł</p>';

        
    }

    private function getLowestPrice($price) {
        
        $productRepository = new ProductRepository(SERVER_URL);
        $price = $productRepository->getLowestPrice($price);
        return $price;
    }

}
