@msdoc11

Не работает кастомный фильтр woocommerce, как исправить?

Привет, не фильтрует продукты, по атрибутам выводит все товары, даже если выбран один атрибут, по категориям ошибка

atal error: Uncaught TypeError: urlencode(): Argument #1 ($string) must be of type string, array given in D:\OpenServer\domains\woo-test\wp-includes\formatting.php:5567 Stack trace: #0 D:\OpenServer\domains\woo-test\wp-includes\formatting.php(5567): urlencode() #1 D:\OpenServer\domains\woo-test\wp-includes\class-wp-query.php(1170): wp_basename() #2 D:\OpenServer\domains\woo-test\wp-includes\class-wp-query.php(940): WP_Query->parse_tax_query() #3 D:\OpenServer\domains\woo-test\wp-includes\class-wp-query.php(1857): WP_Query->parse_query() #4 D:\OpenServer\domains\woo-test\wp-includes\class-wp-query.php(3787): WP_Query->get_posts() #5 D:\OpenServer\domains\woo-test\wp-includes\class-wp.php(663): WP_Query->query() #6 D:\OpenServer\domains\woo-test\wp-includes\class-wp.php(783): WP->query_posts() #7 D:\OpenServer\domains\woo-test\wp-includes\functions.php(1334): WP->main() #8 D:\OpenServer\domains\woo-test\wp-blog-header.php(16): wp() #9 D:\OpenServer\domains\woo-test\index.php(17): require('D:\\OpenServer\\d...') #10 {main} thrown in D:\OpenServer\domains\woo-test\wp-includes\formatting.php on line 5567
На сайте возникла критическая ошибка.

Сам код:
add_action( 'woocommerce_before_shop_loop', 'custom_product_filter' );

function custom_product_filter() {
    $args = array(
        'orderby'           => 'name',
        'order'             => 'ASC',
        'hide_empty'        => false,
        'hierarchical'      => 1,
        'taxonomy'          => 'product_cat'
    );
    $categories = get_categories( $args );
    $attribute_taxonomies = wc_get_attribute_taxonomies();

    if ( ! empty( $categories ) || ! empty( $attribute_taxonomies ) ) {
        echo '<div class="custom-product-filter">';
        echo '<form action="' . esc_url( home_url( '/' ) ) . '" method="GET">';

        // Product categories
        if ( ! empty( $categories ) ) {
            echo '<div class="custom-product-filter-categories">';
            echo '<h3>' . esc_html__( 'Categories', 'your-textdomain' ) . '</h3>';
            echo '<ul>';
            foreach ( $categories as $category ) {
                printf(
                    '<li><input type="checkbox" name="product_cat[]" value="%s" %s> %s (%s)</li>',
                    esc_attr( $category->slug ),
                    ( isset( $_GET['product_cat'] ) && is_array( $_GET['product_cat'] ) && in_array( $category->slug, $_GET['product_cat'] ) ) ? 'checked' : '',
                    esc_html( $category->name ),
                    esc_html( $category->count )
                );
            }
            echo '</ul>';
            echo '</div>';
        }

        // Product attributes
        if ( ! empty( $attribute_taxonomies ) ) {
            echo '<div class="custom-product-filter-attributes">';
            echo '<h3>' . esc_html__( 'Attributes', 'your-textdomain' ) . '</h3>';
            foreach ( $attribute_taxonomies as $attribute ) {
                $attribute_name = wc_attribute_taxonomy_name( $attribute->attribute_name );
                $attribute_terms = get_terms( $attribute_name, array( 'hide_empty' => false ) );

                if ( ! empty( $attribute_terms ) ) {
                    echo '<div class="custom-product-filter-attribute">';
                    echo '<h4>' . esc_html( $attribute->attribute_label ) . '</h4>';
                    echo '<ul>';
                    foreach ( $attribute_terms as $term ) {
                        printf(
                            '<li><input type="checkbox" name="%s" value="%s" %s> %s (%s)</li>',
                            esc_attr( $attribute_name . '[]' ),
                            esc_attr( $term->slug ),
                            ( isset( $_GET[$attribute_name] ) && is_array( $_GET[$attribute_name] ) && in_array( $term->slug, $_GET[$attribute_name] ) ) ? 'checked' : '',
                            esc_html( $term->name ),
                            esc_html( $term->count )
                        );
                    }
                    echo '</ul>';
                    echo '</div>';
                }
            }
        }

        echo '<input type="hidden" name="post_type" value="product">';
        echo '<input type="submit" value="' . esc_attr__( 'Filter', 'your-textdomain' ) . '">';
        echo '</form>';
        echo '</div>';
    }
}
  • Вопрос задан
  • 65 просмотров
Пригласить эксперта
Ваш ответ на вопрос

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

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