<?php 
/**
* Home page - Category List section 
*/

if ( !function_exists( 'store_expert_home_category_list_block' ) ) {
    // Function
    function store_expert_home_category_list_block() {
    

    // Check the section is enabled in customizer
    if ( get_theme_mod( 'store_expert_list_of_category_enable', 1 ) == 1 ) : 
    //Check if WooCommerce is activated
    if ( store_expert_is_woocommerce_activated() ) : 

    ?>
    <section id="home-categories" class="container home-section">
        <div class="home-categories-content container">
            <div class="row">
                <?php
                    // Get array of WooCommerce category list from Customizer
                    $cat_list = explode (",", get_theme_mod( 'store_expert_select_list_categories' ) );
                    foreach( $cat_list as $catlist_id ) :
                    // Get WooCommerce category id by category slug
                    $cat_tem = get_term_by( 'slug', $catlist_id, 'product_cat' );
                    $term_catid = $cat_tem->term_id; 
                ?>
                <?php if ( !empty( $term_catid ) ) : ?>
                <div class="col-lg-2 col-md-4 col-sm-4 col-4 home-category-single">
                    <?php 
                        $thumbnail_id = get_term_meta( $term_catid, 'thumbnail_id', true );
                        // get the medium-sized image url
                        $image = wp_get_attachment_image_src( $thumbnail_id, 'woocommerce_thumbnail' );
                    ?>
                    <a href="<?php echo esc_url( get_category_link( $term_catid ) ); ?>" title="<?php echo esc_attr( get_the_category_by_ID( $term_catid ) ); ?>">
                        <?php 
                            if ( !empty( $image ) ) {
                            ?>
                                <img src="<?php echo esc_url( $image[0] ); // Echo Category Image ?>">
                            <?php
                            } 
                            else { 
                            ?>
                                <img src="<?php echo STORE_EXPERT_THEME_URI . 'img/no-thumb.png'; // Echo Category Image ?>">
                            <?php
                            }

                        ?>
                        

                        <p>
                            <?php
                                // Echo Category Name 
                                echo esc_html( get_the_category_by_ID( $term_catid ) ); 
                            ?>
                        </p>
                    </a>
                </div>
                <?php
                    endif;
                    endforeach; 
                ?>
            </div>
        </div>
    </section>
    <?php 
    endif;
    endif;

    }

}
add_action( 'store_expert_home_category_list_hook', 'store_expert_home_category_list_block', 0 );