<?php
/**
 * Trending Products Section
 * 
 * @package fresh_bakers_shop
 */

$fresh_bakers_shop_classes = get_theme_mod('fresh_bakers_shop_classes_setting', false);
$fresh_bakers_shop_service_title = get_theme_mod('fresh_bakers_shop_service_title');
$fresh_bakers_shop_service_text = get_theme_mod('fresh_bakers_shop_service_text');
$fresh_bakers_shop_category_name = get_theme_mod('fresh_bakers_shop_product_category');
?>

<?php if ($fresh_bakers_shop_classes && class_exists('WooCommerce')) : ?>
    <div class="our-products wow zoomInUp" data-wow-duration="2s">
        <div class="container">
            <div class="side-border">
                <?php if ($fresh_bakers_shop_service_title) : ?>
                    <h4><?php echo esc_html($fresh_bakers_shop_service_title); ?></h4>
                <?php endif; ?>
                <?php if ($fresh_bakers_shop_service_text) : ?>
                    <h3><?php echo esc_html($fresh_bakers_shop_service_text); ?></h3>
                <?php endif; ?>
            </div>
            <?php if ($fresh_bakers_shop_category_name && $fresh_bakers_shop_category_name !== 'select') : ?>
                <div class="mt-3 owl-carousel">
                    <?php
                    $fresh_bakers_shop_args = array(
                        'post_type' => 'product',
                        'posts_per_page' => 4,
                        'ignore_sticky_posts' => true,
                        'tax_query' => array(
                            array(
                                'taxonomy' => 'product_cat',
                                'field'    => 'slug',
                                'terms'    => $fresh_bakers_shop_category_name,
                            ),
                        ),
                    );
                    $fresh_bakers_shop_loop = new WP_Query($fresh_bakers_shop_args);
                    if ($fresh_bakers_shop_loop->have_posts()) :
                        while ($fresh_bakers_shop_loop->have_posts()) : $fresh_bakers_shop_loop->the_post();
                            global $product; ?>
                            <div class="box">
                                <div class="addcart">
                                    <?php woocommerce_template_loop_add_to_cart(); ?>
                                </div>
                                <div class="product-image-wrapper">
                                    <div class="product-img">
                                        <?php if (has_post_thumbnail()) :
                                            the_post_thumbnail('medium');
                                        else : ?>
                                            <img src="<?php echo esc_url(get_stylesheet_directory_uri() . '/images/default-header.png'); ?>" alt="<?php esc_attr_e('Default', 'fresh-bakers-shop'); ?>">
                                        <?php endif; ?>
                                    </div>
                                </div>
                                <div class="product-details">
                                    <?php if (wc_review_ratings_enabled() && $product->get_average_rating()) : ?>
                                        <div class="rating">
                                            <?php echo wc_get_rating_html($product->get_average_rating()); ?>
                                            <span class="rating-count"><?php echo esc_html(number_format($product->get_average_rating(), 1)); ?><?php echo esc_html__( '/5', 'fresh-bakers-shop' ); ?></span>
                                        </div>
                                    <?php endif; ?>
                                    <h6 class="product-title">
                                        <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
                                    </h6>
                                    <div class="price">
                                        <?php echo wp_kses_post($product->get_price_html()); ?>
                                    </div>
                                </div>
                            </div>
                        <?php endwhile;
                    else : ?>
                        <p class="no-products"><?php esc_html_e('No products found.', 'fresh-bakers-shop'); ?></p>
                    <?php endif;
                    wp_reset_postdata(); ?>
                </div>
            <?php else : ?>
                <p class="no-products text-center mt-3">
                    <?php esc_html_e('Please select a category in the Customizer.', 'fresh-bakers-shop'); ?>
                </p>
            <?php endif; ?>
        </div>
    </div>
<?php endif; ?>
