Пример:
<div class="container-002">
<div class="container-002-content">
<div class="container-002__items">
<?php
$taxonomy = 'product_cat';
$show_count = 0;
$pad_counts = 0;
$hierarchical = 1;
$title = '';
$empty = 0;
$args = array(
'taxonomy' => $taxonomy,
'orderby' => $orderby,
'show_count' => $show_count,
'pad_counts' => $pad_counts,
'hierarchical' => $hierarchical,
'title_li' => $title,
'hide_empty' => $empty
);
$all_categories = get_categories( $args );
foreach ($all_categories as $cat) {
if(($cat->category_parent == 0) && ($cat->term_id <> 15)) {
$category_id = $cat->term_id;
$args2 = array(
'taxonomy' => $taxonomy,
'child_of' => 0,
'parent' => $category_id,
'orderby' => $orderby,
'show_count' => $show_count,
'pad_counts' => $pad_counts,
'hierarchical' => $hierarchical,
'title_li' => $title,
'hide_empty' => $empty
);
$sub_cats = get_categories( $args2 );
if($sub_cats) {
foreach($sub_cats as $sub_category) {
$small_thumbnail_size = apply_filters( 'subcategory_archive_thumbnail_size', 'woocommerce_thumbnail' );
$dimensions = wc_get_image_size( $small_thumbnail_size );
$thumbnail_id = get_term_meta( $sub_category->term_id, 'thumbnail_id', true );
$image = wp_get_attachment_image_src( $thumbnail_id, $small_thumbnail_size );
$image = $image[0];
$args = array(
'post_type' => 'product',
'showposts' => '1',
'orderby' => 'meta_value_num ID',
'meta_key' => '_price',
'order' => 'ASC',
'tax_query' => array(
array(
'taxonomy' => 'product_cat',
'field' => 'id',
'terms' => $sub_category->term_id
)
)
);
$query = new WP_Query( $args );
while ( $query -> have_posts() ) {
$query -> the_post();
$min_price = $product->get_price_html();
};
wp_reset_postdata();
?>
<a class="container-002__items-itm-link" href="<?php echo get_term_link($sub_category->slug, 'product_cat'); ?>">
<div class="container-cat-003__items-itm">
<div class="info-up">
<div class="container-cat-003__items-itm-info">
<p class="container-cat-003__items-itm-info-title"><?php echo $sub_category->name; ?></p>
<p class="container-cat-003__items-itm-info-text"><?php echo $sub_category->description; ?></p>
</div>
<div class="container-cat-003__items-itm-image">
<img class="container-cat-003__items-itm-image-img" src="<?php echo esc_url($image);?>" alt="<?php echo $sub_category->name; ?>">
</div>
</div>
<div class="info-down">
<p class="container-cat-003__items-itm-info-maxprice">от <?php echo $min_price; ?> рублей</p>
<p class="container-cat-003__items-itm-info-link">Подробнее</p>
</div>
</div>
</a>
<?php
//echo '<br /><a href="'. get_term_link($sub_category->slug, 'product_cat') .'">'. $sub_category->name .'</a>';
}
}
}
}
?>
</div>
</div>
</div>
За страницу каталога, где выводятся все наши категории отвечает файл archive-product.php. В нем кодом создается цикл для вывода всех категорий
Код в файл archive-product.php
<div class="container-cat-003">
<div class="container-cat-003-content">
<p class="container-cat-003__title">
Вся техника с русской панелью управления!
</p>
<div class="container-cat-003__items">
<?php
if ( woocommerce_product_loop() ) {
woocommerce_product_loop_start();
if ( wc_get_loop_prop( 'total' ) ) {
while ( have_posts() ) {
the_post();
do_action( 'woocommerce_shop_loop' );
wc_get_template_part( 'content', 'product' );
}
}
woocommerce_product_loop_end();
do_action( 'woocommerce_after_shop_loop' );
} else {
do_action( 'woocommerce_no_products_found' );
}
do_action( 'woocommerce_after_main_content' );
?>
</div>
</div>
</div>
А за сам вид категорий отвечает файл content-product-cat.php, вот в него и будем добавлять код, который будет вычислять и выводить минимальную цену товара в каждой категории.
Пример:
<?php
/**
* The template for displaying product category thumbnails within loops
*
* This template can be overridden by copying it to yourtheme/woocommerce/content-product-cat.php.
*
* HOWEVER, on occasion WooCommerce will need to update template files and you
* (the theme developer) will need to copy the new files to your theme to
* maintain compatibility. We try to do this as little as possible, but it does
* happen. When this occurs the version of the template file will be bumped and
* the readme will list any important changes.
*
* @see https://docs.woocommerce.com/document/template-structure/
* @package WooCommerce\Templates
* @version 4.7.0
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
?>
<?php
$small_thumbnail_size = apply_filters( 'subcategory_archive_thumbnail_size', 'woocommerce_thumbnail' );
$dimensions = wc_get_image_size( $small_thumbnail_size );
$thumbnail_id = get_term_meta( $category->term_id, 'thumbnail_id', true );
$image = wp_get_attachment_image_src( $thumbnail_id, $small_thumbnail_size );
$image = $image[0];
//echo $category->term_id;
$args = array(
'post_type' => 'product',
'showposts' => '1',
'orderby' => 'meta_value_num ID',
'meta_key' => '_price',
'order' => 'ASC',
'tax_query' => array(
array(
'taxonomy' => 'product_cat',
'field' => 'id',
'terms' => $category->term_id
)
)
);
$query = new WP_Query( $args );
foreach($query->posts as $product_id){
$product = wc_get_product($product_id);
$product_title = get_the_title($product_id);
$product_image = get_the_post_thumbnail($product_id);
$product_price = $product->get_price();
$product_link = get_the_permalink($product_id);
$product_price = number_format($product_price, 0, '.', ' ');
$min_price = $product_price;
}
?>
<a class="container-cat-003__items-itm-info-link" href="<?php echo esc_url(get_term_link($category,'product_cat'));?>">
<div class="container-cat-003__items-itm">
<div class="info-up">
<div class="container-cat-003__items-itm-info">
<p class="container-cat-003__items-itm-info-title"><?php echo esc_html( $category->name );?></p>
<p class="container-cat-003__items-itm-info-text"><?php echo esc_html( $category->description );?></p>
</div>
<div class="container-cat-003__items-itm-image">
<img class="container-cat-003__items-itm-image-img" src="<?php echo esc_url($image);?>" alt="<?php echo esc_html( $category->name );?>">
</div>
</div>
<div class="info-down">
<p class="container-cat-003__items-itm-info-maxprice">от <?php echo $min_price;?> рублей</p>
<p class="container-cat-003__items-itm-info-link">Подробнее</p>
</div>
</div>
</a>