Почему в Woocommerce не работает фильтр внутри категорий?

Всем привет!
Пытаюсь создать фильтр для продуктов в woocommerce по атрибутам, но не могу устранить ошибку.
Есть функция создания контейнера для фильтра перед циклом продукта, а также функция фильтрации. Почему-то не работает фильтр внутри категорий (/product-category/cloth/).
Но работает корректно на главной странице магазина (/shop/).
Буду благодарен за любую информацию!

Создание контейнера
// create container for filters v.2.7
function add_container_for_filters() {
	echo '<div class="filters">';
	echo '<h2>Filters</h2>';
	echo '<form method="get" class="filters_form">';

	// Color filter
	echo '<div class="filter-option">';
	echo '<h3>Color</h3>';
	// Retrieve terms for color attribute
	$colors = get_terms( 'pa_color', array( 'hide_empty' => true ) );
	foreach ( $colors as $color ) {
			// Check if color filter is currently selected
			$checked = isset( $_GET['color'] ) && in_array( $color->slug, (array) $_GET['color'] ) ? 'checked' : '';
			echo '<label><input type="checkbox" name="color[]" value="' . esc_attr( $color->slug ) . '" ' . $checked . '>' . esc_html( $color->name ) . '</label>';
	}
	echo '</div>';
	
	// Size filter
	echo '<div class="filter-option">';
	echo '<h3>Size</h3>';
	// Retrieve terms for size attribute
	$sizes = get_terms( 'pa_size', array( 'hide_empty' => true ) );
	foreach ( $sizes as $size ) {
			// Check if size filter is currently selected
			$checked = isset( $_GET['size'] ) && in_array( $size->slug, (array) $_GET['size'] ) ? 'checked' : '';
			echo '<label><input type="checkbox" name="size[]" value="' . esc_attr( $size->slug ) . '" ' . $checked . '>' . esc_html( $size->name ) . '</label>';
	}
	echo '</div>';

	// Apply filters button
	echo '<div class="filter-button"><button class="filters_apply" type="submit">Apply Filters</button></div>';

	// Clear Filters button
	echo '<div class="filter-button"><button type="submit" name="clear_filters">Clear Filters</button></div>';
	echo '</form></div>';
}
add_action('woocommerce_before_shop_loop', 'add_container_for_filters');


Фильтрация
add_action('woocommerce_product_query', 'filter_products_by_attributes');
function filter_products_by_attributes($q) {
    if (is_shop() || is_product_category()) {
        // Check if any filters are applied
        if (isset($_GET['size']) || isset($_GET['color'])) {
            // Initialize the tax query array
            $tax_query = array('relation' => 'AND');

            // Color filter
            if (isset($_GET['color']) && is_array($_GET['color'])) {
                $color_filter = array_map('sanitize_text_field', $_GET['color']);
                if (!empty($color_filter)) {
                    $tax_query[] = array(
                        'taxonomy' => 'pa_color',
                        'field'    => 'slug',
                        'terms'    => $color_filter,
                        'operator' => 'IN',
                    );
                }
            }

            // Size filter
            if (isset($_GET['size']) && is_array($_GET['size'])) {
                $size_filter = array_map('sanitize_text_field', $_GET['size']);
                if (!empty($size_filter)) {
                    $tax_query[] = array(
                        'taxonomy' => 'pa_size',
                        'field'    => 'slug',
                        'terms'    => $size_filter,
                        'operator' => 'IN',
                    );
                }
            }

            // Get existing tax queries
            $existing_tax_query = (array) $q->get('tax_query');

            // Merge existing and new tax queries
            $q->set('tax_query', array_merge($existing_tax_query, $tax_query));
        }
    }
}
  • Вопрос задан
  • 23 просмотра
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы