• Resolved janrectus

    (@janrectus)


    Hello,

    I changed the archive-product.php in my childtheme to contain a hardcoded product filter which gives users the option to filter products on product attributes. The filter works great untill i activate a product attributes archive page. Instead of displaying the filtered products it throws the following error.

    Fatal error: Uncaught TypeError: urlencode(): Argument #1 ($string) must be of type string, array given in localhost/wp-includes/formatting.php:5590 Stack trace: #0 localhost/wp-includes/formatting.php(5590): urlencode() #1 localhost/wp-includes/class-wp-query.php(1170): wp_basename() #2 localhost/wp-includes/class-wp-query.php(940): WP_Query->parse_tax_query() #3 localhost/wp-includes/class-wp-query.php(1857): WP_Query->parse_query() #4 localhost/wp-includes/class-wp-query.php(3787): WP_Query->get_posts() #5 localhost/wp-includes/class-wp.php(663): WP_Query->query() #6 localhost/wp-includes/class-wp.php(783): WP->query_posts() #7 localhost/wp-includes/functions.php(1334): WP->main() #8 localhost/wp-blog-header.php(16): wp() #9 localhost/index.php(17): require('...') #10 {main} thrown in localhost/wp-includes/formatting.php on line 5590

    This only happens when i try to filter on a product attribute which has the archive page enabled. After vardumping my taxonomies i found the only difference to be that “[“attribute_public”]=> int(1)” on product attributes with the archive page enabled, if the archive page is disabled it returns int(0).

    The url that’s being generated after a filter has been applied is always something like this “shop/?pa_brand%5B%5D=nektar” and doesnt change depending on if the error is thrown or not. This is the code for my filter.

    <form id="filter-form" method="get">
                <?php
                $taxonomies = wc_get_attribute_taxonomies();
                foreach ($taxonomies as $taxonomy):
                    $attribute = "pa_" . $taxonomy->attribute_name;
                    $terms = get_terms($attribute, ["hide_empty" => false]);
                    if ($terms):
                        $product_attribute_terms = [];
                        $product_query = new WP_Query([
                            "post_type" => "product",
                            "post_status" => "publish",
                            "posts_per_page" => -1,
                        ]);
                        while ($product_query->have_posts()):
                            $product_query->the_post();
                            $product_attribute_terms = array_merge(
                                $product_attribute_terms,
                                wc_get_product_terms(get_the_ID(), $attribute, [
                                    "fields" => "slugs",
                                ])
                            );
                        endwhile;
                        wp_reset_postdata();
                        foreach ($terms as $term):
                            if (in_array($term->slug, $product_attribute_terms)): ?>
                                <label>
                                    <input type="checkbox" name="<?php echo esc_attr(
                                        $attribute
                                    ); ?>[]" value="<?php echo esc_attr(
        $term->slug
    ); ?>" <?php if (
        isset($_GET[$attribute]) &&
        in_array($term->slug, $_GET[$attribute])
    ) {
        echo 'checked="checked"';
    } ?>>
                                    <?php echo esc_html($term->name); ?>
                                </label>
                                <br>
                <?php endif;
                        endforeach;
                    endif;
                endforeach;
                ?>
Viewing 2 replies - 1 through 2 (of 2 total)
  • Hi there!

    I understand you are having trouble enabling the product filter on the archive page with custom code. Is there particular reason why you want to hard code the filter?

    You can use extensions like WooCommerce Product Search and add an attributes filter if you only want to add an attribute filter on the archive page.

    I do not have an exact example of how this can be achieved with custom code.

    I’ll keep this thread open for a little longer to see if anyone else can chime in to share any other hints.

    We have our developer resources portal that can help you get going, so I would recommend you to check it further from here: https://developer.woocommerce.com/

    You can also visit the WooCommerce Community Forum, the WooCommerce FB group, or the #developers channel of our Community Slack. We’re lucky to have a great community of open-source developers for WooCommerce, and many of our developers hang out there, too.

    Another option is to check our customizations page to hire an expert that can create a custom solution for you: https://woocommerce.com/experts/

    Thank you.

    LX T

    (@lxt)

    Checking your example URL “shop/?pa_brand%5B%5D=nektar”.

    You supply pa_brand as an array of terms. That’s the problem here. The fatal erros was introduced with PHP 8.0 – on 7.4 that would still work. I’m having the same problem, no solution so far.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘urlencode error for certain product attributes in url’ is closed to new replies.