| Server IP : 104.171.130.249 / Your IP : 216.73.216.146 Web Server : nginx/1.25.5 System : Linux 0dac65491b7e 6.8.0-134-generic #134-Ubuntu SMP PREEMPT_DYNAMIC Fri Jun 26 18:43:11 UTC 2026 x86_64 User : root ( 0) PHP Version : 8.4.23 Disable Function : NONE MySQL : OFF | cURL : ON | WGET : OFF | Perl : ON | Python : OFF | Sudo : OFF | Pkexec : OFF Directory : /var/www/html/wp-content/themes/matreshka/template-parts/woocommerce/ |
Upload File : |
<?php
/**
* Template part for displaying product category page
*
* @package Matreshka
*/
$current_category = get_queried_object();
$active_subcat = isset( $_GET['subcat'] ) ? intval( $_GET['subcat'] ) : 'all';
$category_product_tax_query = array(
'relation' => 'AND',
array(
'taxonomy' => 'product_cat',
'field' => 'term_id',
'terms' => array( (int) $current_category->term_id ),
'include_children' => true,
),
);
if ( function_exists( 'wc_get_product_visibility_term_ids' ) ) {
$product_visibility_terms = wc_get_product_visibility_term_ids();
$excluded_visibility_terms = array_filter(
array(
$product_visibility_terms['exclude-from-catalog'] ?? 0,
)
);
if ( ! empty( $excluded_visibility_terms ) ) {
$category_product_tax_query[] = array(
'taxonomy' => 'product_visibility',
'field' => 'term_taxonomy_id',
'terms' => $excluded_visibility_terms,
'operator' => 'NOT IN',
);
}
}
$category_products_query = new WP_Query(
array(
'post_type' => 'product',
'post_status' => 'publish',
'posts_per_page' => -1,
'fields' => 'ids',
'orderby' => array(
'menu_order' => 'ASC',
'title' => 'ASC',
),
'order' => 'ASC',
'no_found_rows' => true,
'update_post_meta_cache' => false,
'update_post_term_cache' => true,
'tax_query' => $category_product_tax_query,
'meta_query' => array(
array(
'key' => '_stock_status',
'value' => 'instock',
),
),
)
);
$all_products = $category_products_query->posts;
$products_by_subcategory = array();
$products_per_all_section = 20;
$products_per_page = 50;
$paged = max( 1, (int) get_query_var( 'paged' ) );
$category_link = get_term_link( $current_category );
if ( is_wp_error( $category_link ) ) {
$category_link = get_permalink( wc_get_page_id( 'shop' ) );
}
foreach ( $all_products as $product_id ) {
$product_cats = wp_get_post_terms( $product_id, 'product_cat' );
$added = false;
if ( ! empty( $product_cats ) ) {
foreach ( $product_cats as $cat ) {
// Group descendant terms under the direct child of the current category.
$direct_child = null;
if ( (int) $cat->parent === (int) $current_category->term_id ) {
$direct_child = $cat;
} elseif ( (int) $cat->term_id !== (int) $current_category->term_id ) {
$ancestors = get_ancestors( $cat->term_id, 'product_cat' );
if ( in_array( (int) $current_category->term_id, array_map( 'intval', $ancestors ), true ) ) {
$child_term_id = (int) $cat->term_id;
foreach ( $ancestors as $ancestor_id ) {
$ancestor = get_term( $ancestor_id, 'product_cat' );
if ( $ancestor && ! is_wp_error( $ancestor ) && (int) $ancestor->parent === (int) $current_category->term_id ) {
$child_term_id = (int) $ancestor->term_id;
break;
}
}
$direct_child = get_term( $child_term_id, 'product_cat' );
}
}
if ( $direct_child && ! is_wp_error( $direct_child ) ) {
if ( ! isset( $products_by_subcategory[ $direct_child->term_id ] ) ) {
$products_by_subcategory[ $direct_child->term_id ] = array(
'name' => $direct_child->name,
'products' => array(),
);
}
$products_by_subcategory[ $direct_child->term_id ]['products'][] = $product_id;
$added = true;
break;
}
}
}
if ( ! $added ) {
if ( ! isset( $products_by_subcategory[0] ) ) {
$products_by_subcategory[0] = array(
'name' => $current_category->name,
'products' => array(),
);
}
$products_by_subcategory[0]['products'][] = $product_id;
}
}
// Get only direct subcategories that have visible in-stock products.
$subcategories = get_terms(
array(
'taxonomy' => 'product_cat',
'hide_empty' => true,
'pad_counts' => true,
'parent' => $current_category->term_id,
)
);
if ( is_wp_error( $subcategories ) ) {
$subcategories = array();
}
$subcategories = array_values(
array_filter(
$subcategories,
function ( $subcategory ) use ( $products_by_subcategory ) {
return ! empty( $products_by_subcategory[ $subcategory->term_id ]['products'] );
}
)
);
$has_subcategory_filters = ! empty( $subcategories );
if ( $has_subcategory_filters ) {
$ordered_products_by_subcategory = array();
foreach ( $subcategories as $subcategory ) {
$ordered_products_by_subcategory[ $subcategory->term_id ] = $products_by_subcategory[ $subcategory->term_id ];
}
if ( ! empty( $products_by_subcategory[0] ) ) {
$ordered_products_by_subcategory[0] = $products_by_subcategory[0];
}
$products_by_subcategory = $ordered_products_by_subcategory;
}
if ( 'all' !== $active_subcat && empty( $products_by_subcategory[ $active_subcat ] ) ) {
$active_subcat = 'all';
$paged = 1;
}
$is_all_subcategories_view = ( 'all' === $active_subcat && $has_subcategory_filters );
$is_full_grid_view = ( 'all' !== $active_subcat || ! $has_subcategory_filters );
?>
<!-- navigation -->
<section>
<div class="container navigation ">
<div class="d-flex w-100">
<a href="<?php echo esc_url( get_permalink( wc_get_page_id( 'shop' ) ) ); ?>">
<img src="<?php echo esc_url( get_template_directory_uri() . '/assets/icons/AltArrowLeft.svg' ); ?>" alt="Back to shop">
</a>
<h2 class="category-title montserrat-semibold"><?php echo esc_html( $current_category->name ); ?></h2>
</div>
</div>
</section>
<!-- section-subcategories -->
<?php if ( $has_subcategory_filters ) : ?>
<section>
<div class="container">
<div class="container-subcategories">
<a href="<?php echo esc_url( $category_link ); ?>" class="btn btn-secondary montserrat-medium subcategory-filter <?php echo ( $active_subcat === 'all' ) ? 'active' : ''; ?>" data-subcategory="all">
Все товары
</a>
<?php foreach ( $subcategories as $subcategory ) : ?>
<a href="<?php echo esc_url( add_query_arg( 'subcat', $subcategory->term_id, $category_link ) ); ?>" class="btn btn-secondary montserrat-medium subcategory-filter <?php echo ( $active_subcat == $subcategory->term_id ) ? 'active' : ''; ?>" data-subcategory="<?php echo esc_attr( $subcategory->term_id ); ?>">
<?php echo esc_html( $subcategory->name ); ?>
</a>
<?php endforeach; ?>
</div>
</div>
</section>
<?php endif; ?>
<!-- Loading indicator -->
<div class="category-loading" style="display: none;">
<div class="spinner-border text-primary" role="status">
<span class="visually-hidden">Loading...</span>
</div>
</div>
<!-- section-products -->
<?php
if ( ! empty( $products_by_subcategory ) ) :
// Display products grouped by subcategory
foreach ( $products_by_subcategory as $subcat_id => $subcat_data ) :
if ( empty( $subcat_data['products'] ) ) continue;
if ( 'all' !== $active_subcat && (int) $active_subcat !== (int) $subcat_id ) {
continue;
}
$total_products = count( $subcat_data['products'] );
$total_pages = $is_full_grid_view ? (int) ceil( $total_products / $products_per_page ) : 1;
$current_page = min( $paged, max( 1, $total_pages ) );
$show_more_card = $is_all_subcategories_view && $subcat_id && $total_products > $products_per_all_section;
$all_section_limit = $show_more_card ? max( 0, $products_per_all_section - 1 ) : $products_per_all_section;
$display_products = $is_full_grid_view
? array_slice( $subcat_data['products'], ( $current_page - 1 ) * $products_per_page, $products_per_page )
: array_slice( $subcat_data['products'], 0, $all_section_limit );
$product_container_class = $is_full_grid_view ? 'new-products-grid' : 'product-cards container-products';
?>
<section class="subcategory-section" data-subcategory-group="<?php echo esc_attr( $subcat_id ); ?>">
<div class="container mb-5">
<div class="container-interesting">
<?php if ( $is_all_subcategories_view && $subcat_id ) : ?>
<a href="<?php echo esc_url( add_query_arg( 'subcat', $subcat_id, $category_link ) ); ?>" class="montserrat-bold subcategory-title subcategory-title-link">
<?php echo esc_html( $subcat_data['name'] ); ?>
<img src="<?php echo esc_url( get_template_directory_uri() . '/assets/icons/AltArrowRight.svg' ); ?>" alt="arrow right">
</a>
<?php else : ?>
<h2 class="montserrat-bold subcategory-title">
<?php echo esc_html( $subcat_data['name'] ); ?>
</h2>
<?php endif; ?>
<div class="<?php echo esc_attr( $product_container_class ); ?>">
<?php
foreach ( $display_products as $product_id ) :
$product = wc_get_product( $product_id );
if ( ! $product ) continue;
$product_link = get_permalink( $product_id );
$product_image = wp_get_attachment_image_url( $product->get_image_id(), 'woocommerce_thumbnail' );
$product_title = $product->get_name();
$regular_price = $product->get_regular_price();
$sale_price = $product->get_sale_price();
$weight = $product->get_weight();
$weight_text = $weight ? $weight . 'г' : '200г';
$is_liked = matreshka_is_in_wishlist( $product_id );
$like_icon = $is_liked ? get_template_directory_uri() . '/assets/icons/Heart.svg' : get_template_directory_uri() . '/assets/icons/like.svg';
?>
<div class="product-card" data-product-id="<?php echo esc_attr( $product_id ); ?>" data-product-subcategory="<?php echo esc_attr( $subcat_id ); ?>">
<a class="link-product" href="<?php echo esc_url( $product_link ); ?>">
<div class="product-card-img">
<?php echo matreshka_get_product_card_image_html( $product, $product_title ); ?>
</div>
<p class="montserrat-medium">
<?php echo esc_html( $product_title ); ?>
</p>
<span class="montserrat-medium"><?php echo esc_html( $weight_text ); ?></span>
</a>
<div class="product-card-bottom">
<?php echo matreshka_get_product_price_html( $product ); ?>
<img src="<?php echo esc_url( $like_icon ); ?>"
alt="like icon"
class="like-icon <?php echo $is_liked ? 'active' : ''; ?>">
</div>
<button class="btn w-100 btn-secondary montserrat-bold add-to-cart-btn"
data-product_id="<?php echo esc_attr( $product_id ); ?>"
data-product_sku="<?php echo esc_attr( $product->get_sku() ); ?>">
В корзину
</button>
</div>
<?php endforeach; ?>
<?php if ( $show_more_card ) : ?>
<a class="product-card subcategory-more-card" href="<?php echo esc_url( add_query_arg( 'subcat', $subcat_id, $category_link ) ); ?>" aria-label="<?php echo esc_attr( sprintf( 'Смотреть больше товаров в категории %s', $subcat_data['name'] ) ); ?>">
<div class="subcategory-more-card__content">
<svg xmlns="http://www.w3.org/2000/svg" height="24px" viewBox="0 -960 960 960" width="24px" fill="#969696" style="
display: flex;
margin: 0 auto;
width: 60px;
height: 60px;
color: #cccccc;
"><path d="m492-240 25-84q54-13 88.5-56T640-480q0-66-47-113t-113-47q-57 0-100 34.5T324-517l-84 25q5-96 74-162t166-66q100 0 170 70t70 170q0 97-66 166t-162 74ZM139-60l-79-79 171-171-151-50 400-120L360-80l-50-151L139-60Z"></path></svg>
<div class="subcategory-more-card__title montserrat-semibold">
<?php echo esc_html( sprintf( 'Смотреть больше товаров в категории «%s»', $subcat_data['name'] ) ); ?>
</div>
<span class="subcategory-more-card__button montserrat-bold">
Смотреть больше
</span>
</div>
</a>
<?php endif; ?>
</div>
<?php if ( $is_full_grid_view && $total_pages > 1 ) : ?>
<nav class="matreshka-pagination mt-5" aria-label="Page navigation">
<ul class="pagination justify-content-center">
<?php if ( $current_page > 1 ) : ?>
<li class="page-item">
<a class="page-link" href="<?php echo esc_url( 'all' !== $active_subcat ? add_query_arg( 'subcat', $active_subcat, get_pagenum_link( $current_page - 1 ) ) : get_pagenum_link( $current_page - 1 ) ); ?>" aria-label="Previous">
<span aria-hidden="true">«</span>
</a>
</li>
<?php else : ?>
<li class="page-item disabled">
<span class="page-link">«</span>
</li>
<?php endif; ?>
<?php
$range = 2;
for ( $i = 1; $i <= $total_pages; $i++ ) :
if ( 1 === $i || $total_pages === $i || ( $i >= $current_page - $range && $i <= $current_page + $range ) ) :
$page_link = 'all' !== $active_subcat ? add_query_arg( 'subcat', $active_subcat, get_pagenum_link( $i ) ) : get_pagenum_link( $i );
if ( $i === $current_page ) :
?>
<li class="page-item active" aria-current="page">
<span class="page-link"><?php echo esc_html( $i ); ?></span>
</li>
<?php else : ?>
<li class="page-item">
<a class="page-link" href="<?php echo esc_url( $page_link ); ?>">
<?php echo esc_html( $i ); ?>
</a>
</li>
<?php endif; ?>
<?php elseif ( $i === $current_page - $range - 1 || $i === $current_page + $range + 1 ) : ?>
<li class="page-item disabled">
<span class="page-link">...</span>
</li>
<?php endif; ?>
<?php endfor; ?>
<?php if ( $current_page < $total_pages ) : ?>
<li class="page-item">
<a class="page-link" href="<?php echo esc_url( 'all' !== $active_subcat ? add_query_arg( 'subcat', $active_subcat, get_pagenum_link( $current_page + 1 ) ) : get_pagenum_link( $current_page + 1 ) ); ?>" aria-label="Next">
<span aria-hidden="true">»</span>
</a>
</li>
<?php else : ?>
<li class="page-item disabled">
<span class="page-link">»</span>
</li>
<?php endif; ?>
</ul>
</nav>
<?php endif; ?>
</div>
</div>
</section>
<?php
endforeach;
else :
?>
<section>
<div class="container mb-5">
<div class="text-center py-5" style="min-height: 400px; display: flex; flex-direction: column; justify-content: center; align-items: center;">
<h2 class="montserrat-bold" style="font-size: 2rem; margin-bottom: 1rem; color: #333;">
Здесь пока пусто
</h2>
<p class="montserrat-medium" style="font-size: 1.25rem; color: #666;">
В этой категории пока нет товаров
</p>
</div>
</div>
</section>
<?php
endif;
?>
<?php wp_reset_postdata(); ?>