Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fatal Error when using Perfect WooCommerce Brands filter #54

Open
calliaweb opened this issue Jun 19, 2020 · 2 comments
Open

Fatal Error when using Perfect WooCommerce Brands filter #54

calliaweb opened this issue Jun 19, 2020 · 2 comments

Comments

@calliaweb
Copy link

This issue only happens when you have the following plugins active:

  1. Genesis Connect for WooCommerce
  2. WooCommerce
  3. Perfect WooCommerce Brands (PWB)

And

  1. You have breadcrumbs enabled on archives
  2. You've used PWB "Filter by Brand" widget on a product category archive page, And
  3. You have parent/child "brands" (PWB have made their taxonomy hierarchical) and products within them
  4. On the front end you select a child brand term (it works fine if you select a parent term)

You get:

Recoverable fatal error: Object of class WP_Error could not be converted to string in /wp-includes/formatting.php on line 1117 There has been a critical error on your website.

I have traced this error back to genesis-connect-woocommerce/lib/breadcrumb.php line 204 in the function:

function gencwooc_get_crumb_link( $url, $title, $content, $sep = false ) {

	$link = sprintf(
		'<a href="%s" title="%s">%s</a>',
		esc_attr( $url ),
		esc_attr( $title ),
		esc_html( $content )
	);

	if ( $sep ) {
		$link .= $sep;
	}

	return $link;

}

It is because the $url is an error passed by line number 104 in the same file:

$item   = get_term_by( 'id', $parent, get_query_var( 'taxonomy' ) );
$crumb .= gencwooc_get_crumb_link( get_term_link( $item->slug, 'product_cat' ), $item->name, $item->name, $args['sep'] );

There needs to be a check on $item to ensure it is not an error before the slug is passed to gencwooc_get_crumb_link().

Why is it an error in this case?

There is an assumption that the taxonomy is a product_cat (which is fair enough as we're in a "is_tax( 'product_cat' )" conditional).

But when the filter is triggered, even though we are on a product_cat archive, the term return on line 87 by:

get_term_by( 'slug', get_query_var( 'term' ), get_query_var( 'taxonomy' ) );

is actually a pwb-brand taxonomy and not a product_cat

I appreciate this is a very edge case, but if you change line 104 to the below then it fixes the error (hopefully without any bad consequence?)

$crumb .= gencwooc_get_crumb_link( get_term_link( $item->slug, get_query_var( 'taxonomy' ) ), $item->name, $item->name, $args['sep'] );
@nickcernis
Copy link
Contributor

Thank you for the report and suggested fix, @calliaweb!

@calliaweb
Copy link
Author

calliaweb commented Jun 19, 2020

Looking at this some more... the issue is that get_query_var( 'taxonomy' ); and get_query_var( 'term' ); are returning the pwb-brands taxonomy and term, even though we are on the product_cat archive.

My "fix" produces a hybrid ( home > parent brand > product category ) non-sensical breadcrumb.

I've noticed that on this product_cat archive, when the filter is active:

  • get_query_var( 'taxonomy' ) & get_query_var( 'term' ) returns pwb_brands and the pwb_brands term,
  • get_queried_object returns product_cat taxonomy and term.

Maybe we should use get_queried_object instead?

For example:

if ( is_tax( 'product_cat' ) ) {

		$term = get_queried_object();
		$parents = array();

		$parent  = $term->parent;

		while ( $parent ) {
			$parents[]  = $parent;
			$new_parent = get_term_by( 'id', $parent, $term->taxonomy );
			$parent     = $new_parent->parent;
		}

		$crumb .= $prepend;

		if ( ! empty( $parents ) ) {
			$parents = array_reverse( $parents );

			foreach ( $parents as $parent ) {
				$item   = get_term_by( 'id', $parent, $term->taxonomy );
				$crumb .= gencwooc_get_crumb_link( get_term_link( $item->slug, $term->taxonomy ), $item->name, $item->name, $args['sep'] );
			}
		}

		$crumb .= single_term_title( '', false );

		return $crumb;
	}

And now I noticed that my product category archive title and description also change when I select something in this filter!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants