<?php 
/**
* Home page - Product Grid Section
*/
if ( !function_exists( 'store_expert_home_grid_product_block' ) ) {
    // Function
    function store_expert_home_grid_product_block() {
    // Check if the section is enabled in customizer
    if ( get_theme_mod( 'store_expert_products_by_category_enable', 1 ) == 1 ) : 

    // Check if WooCommerce plugin is activated
    if ( store_expert_is_woocommerce_activated() ) : ?> 
        <section id="vertical-products" class="vertical-products container home-section">
            <div class="vertical-products-wrapper container">
                <div class="row">                        
                <?php
                    // Get values from customize
                    $no_of_products = get_theme_mod( 'store_expert_product_grid_counts', 5 );
                    $orderby = get_theme_mod( 'store_expert_product_grid_orderby', 'none' );
                    $order = get_theme_mod( 'store_expert_product_grid_order', 'DESC' );
                    $grid_cat_list = explode (",", get_theme_mod( 'store_expert_select_product_categories_grid', '' ) );
                    
                    if ( !empty( $grid_cat_list ) ) :
                    foreach($grid_cat_list as $grid_cat_lists) : ?>
                    <div class="col-md-6 col-lg-3 vertical-products-column">
                        <?php 
                            $args = array(
                            'post_type' => 'product',
                            'posts_per_page' => absint( $no_of_products ),
                            'orderby' => esc_html( $orderby ),
                            'order' => esc_html( $order ),
                            'tax_query' => array(
                                array(
                                    'taxonomy' => 'product_cat',
                                    'field' => 'slug',
                                    'terms' => $grid_cat_lists,
                                ),
                            ),
                        );

                        $product_query = new WP_Query($args);
                        ?>                                
                        <?php if ($product_query->have_posts()) : ?>
                            <div class="vertical-products-head clearfix">
                                <h3>
                                    <?php
                                        // Get category name by category slug
                                        $category_name = get_term_by( 'slug', $grid_cat_lists, 'product_cat' );
                                        echo esc_html( $category_name->name );
                                    ?>                                            
                                </h3>
                                <div class="stripe-line"></div>
                            </div>
                            <ul>
                                <?php while ($product_query->have_posts()) : $product_query->the_post(); ?>                                             
                                    <li class="clearfix">
                                    <?php if (has_post_thumbnail()) : ?>
                                        <?php
                                        $img = wp_get_attachment_image_src( get_post_thumbnail_id(), 'woocommerce_thumbnail' );
                                        ?>
                                        <a href="<?php the_permalink(); ?>">
                                            <img src="<?php echo esc_url( $img[0] ); ?>" alt="<?php echo esc_attr( absint( get_post_thumbnail_id() ) ); ?>">
                                        </a>
                                    <div class="vertical-products-title">
                                        <h3><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>
                                        <?php woocommerce_template_loop_price(); ?>
                                    </div>
                                    <?php endif; ?>
                                    </li>                                            
                                <?php endwhile; ?>                                    
                            </ul>
                        <?php
                            // End query
                            wp_reset_postdata();
                            endif; 
                        ?>
                    </div>
                    <?php 
                        endforeach;
                        endif;
                     ?>
                </div>                    
            </div>
        </section>
    <?php 
    endif; 
    endif;

    }

}
add_action( 'store_expert_home_grid_product_block_hook', 'store_expert_home_grid_product_block', 0 );