urlencode error for certain product attributes in url
-
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; ?>
- The topic ‘urlencode error for certain product attributes in url’ is closed to new replies.