<?php
/**
 * latest_products section
 *
 * This is the template for the content of latest_products section
 *
 * @package Theme Palace
 * @subpackage Raise Charity Pro
 * @since Raise Charity Pro 1.0.0
 */
if ( ! function_exists( 'raise_charity_pro_add_latest_products_section' ) ) :
    /**
    * Add latest_products section
    *
    *@since Raise Charity Pro 1.0.0
    */
    function raise_charity_pro_add_latest_products_section() {
    	$options = raise_charity_pro_get_theme_options();
        // Check if latest_products is enabled on frontpage
        $latest_products_enable = apply_filters( 'raise_charity_pro_section_status', true, 'latest_products_section_enable' );

        if ( ( true !== $latest_products_enable) || ! class_exists( 'WooCommerce' ) ) {
            return false;
        }

        // Render latest_products section now.
        raise_charity_pro_render_latest_products_section();
    }
endif;

if ( ! function_exists( 'raise_charity_pro_render_latest_products_section' ) ) :
  /**
   * Start latest_products section
   *
   * @return string latest_products content
   * @since Raise Charity Pro 1.0.0
   *
   */
   function raise_charity_pro_render_latest_products_section() {
        $options = raise_charity_pro_get_theme_options();

    ?>
    <div id="raise_charity_pro_latest_products_section">
        <div id="latest-products" class="relative page-section same-background">
            <div class="wrapper">
                <?php if ( is_customize_preview()):
                raise_charity_pro_section_tooltip( 'latest-products' );
                endif; ?>
                <div class="<?php echo esc_attr( $options['latest_products_column_layout'] ); ?> clear">

                    <?php for ( $i=1; $i <= $options['latest_products_count']; $i++ ) { 
                    if ( empty($options['latest_products_content_latest_products_category_'.$i]) ) {
                        continue;
                    }
                    $thumbnail_id = get_term_meta( $options['latest_products_content_latest_products_category_'.$i], 'thumbnail_id', true );
                    $image = wp_get_attachment_url( $thumbnail_id );
                    $cat_name = get_term_by( 'id', $options['latest_products_content_latest_products_category_'.$i], 'product_cat' )->name;
                    $cat_link = get_category_link( $options['latest_products_content_latest_products_category_'.$i] );
                    ?>

                    <article>
                        <div class="featured-image" style="background-image: url('<?php echo esc_url( $image ); ?>');">
                            <div class="entry-container">
                                <header class="entry-header">
                                   <h2 class="entry-title"><a href="<?php echo esc_url( $cat_link ) ?>"><?php echo esc_html( $cat_name ); ?></a></h2>
                                </header>
                            </div><!-- .entry-container -->
                        </div><!-- .featured-image -->
                    </article>

                    <?php } ?>

                </div><!-- .grid --> 
            </div><!-- .wrapper -->
        </div><!-- #latest-products -->

    </div>
   
    <?php }
endif;