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 is_search() with custom page / query #1936

Closed
1 of 2 tasks
jnz31 opened this issue May 10, 2021 · 8 comments · Fixed by #2246
Closed
1 of 2 tasks

Fatal error is_search() with custom page / query #1936

jnz31 opened this issue May 10, 2021 · 8 comments · Fixed by #2246
Labels
feature: pixel events & conversion tracking Related to Facebook pixel, events, and conversion tracking. priority: low The issue/PR is low priority—not many people are affected or there’s a workaround, etc. type: bug The issue is a confirmed bug.

Comments

@jnz31
Copy link

jnz31 commented May 10, 2021

  • I have confirmed this occurs in the most recent version of WordPress, WooCommerce, and Facebook for WooCommerce.
  • I have confirmed this occurs when only WooCommerce and Facebook for WooCommerce are active and when using a default WordPress or WooCommerce theme.

✍️ Describe the bug

Fatal error: Uncaught Error: Call to a member function is_search() on bool in /xxx/wp-includes/query.php:703 Stack trace: #0 /xxx/wp-content/plugins/facebook-for-woocommerce/facebook-commerce-events-tracker.php(378): is_search() #1 /xxx/wp-includes/class-wp-hook.php(292): WC_Facebookcommerce_EventsTracker->inject_search_event(Object(WP_Query)) #2 /xxx/wp-includes/class-wp-hook.php(316): WP_Hook->apply_filters(NULL, Array) #3 /xxx/wp-includes/plugin.php(551): WP_Hook->do_action(Array) #4 /xxx/wp-includes/class-wp-query.php(1784): do_action_ref_array('pre_get_posts', Array) #5 /xxx/wp-includes/class-wp-qu in /xxx/wp-includes/query.php on line 703

🚶‍♀️ Steps to reproduce

i have a custom page for sale items, where i take the "normal" $wc_query and add taxonomy (f.ex. sale/women) and 'post__in' => wc_get_product_ids_on_sale(). it is based on woocommerce's archive-product.php.

here is my tax_query

global $wp_query, $language;
$tax_query = WC()->query->get_tax_query();

if (array_key_exists('product_cat', $wp_query->query)) :
    $term = explode('/', $wp_query->query['product_cat']);
    if ((is_array($term) && array_key_exists(0, $term) && $term[0] != 'page')) :
        $tax_query[] = [
            'taxonomy' => 'product_cat',
            'field' => 'slug',
            'terms' => $term[0]
        ];
    endif;
else :
    $term = 'all';
endif;

meta_query (only show in-stock items)

$meta_query = WC()->query->get_meta_query();
$meta_query[] = [
    'key' => '_stock_status',
    'value' => 'instock'
];

...custom query:

$args = [
        'post_status' => 'publish',
        'post_type' => 'product',
        'meta_query' => $meta_query,
        'tax_query' => $tax_query,
        'post__in' => wc_get_product_ids_on_sale(),
        'posts_per_page' => 59,
        'orderby' => 'menu_order title',
        'order' => 'ASC',
        'paged' => $paged
];
$wp_query = new WP_Query($args);

✔️ Expected behavior

this is new behavior (client pointed me to the failing sale page on friday), it worked with no issues before (> 6 month), so not sure, what causes this issue.

i now uncommented the contents of inject_search_event() and it works for me.

i attached my template file, the is a function called jnz_output_category_navigation(). it is not related to this issue.
page-sale.php.zip

@budzanowski
Copy link
Contributor

i now uncommented the contents of inject_search_event() and it works for me.

Do you want to say that it started to work again?

@jnz31
Copy link
Author

jnz31 commented May 10, 2021

well, yes. if i remove all functionality from inject_search_event() it works. guess, that is not the purpose of the function..

the function generates the error. it is gone now, since i removed all its contents.. guess it will break something else, but at least my template is now working properly..

@haszari haszari added the needs feedback The issue/PR needs a response from any of the parties involved in the issue. label May 11, 2021
@haszari
Copy link
Contributor

haszari commented May 11, 2021

Hi there @jnz31 - thanks for reaching out.

It sounds like you have some customizations that might be a factor here - we haven't had any reports from other users of this fatal error.

Can you try to reproduce this issue on a clean site running current versions of WordPress, WooCommerce and Facebook for WooCommerce, and a standard theme such as Storefront or Twenty Twenty? If you can reproduce the fatal error without any custom PHP templates, add the details here so we can figure out what's happening.

On the other hand, if this issue is caused by an interaction with your custom template code and a recent update to Facebook for WooCommerce, you'll need to debug your template & query.

@jnz31
Copy link
Author

jnz31 commented May 11, 2021

ciao @haszari

i boiled it down a little further: you inject inject_search_event via add_action('pre_get_posts', [$this, 'inject_search_event']); (line 112) so it runs pre query. and inside inject_search_event you call is_search(). thing is: these conditional functions do not work (always return false) since the query did not even run, yet. so this at least throws a php notice.

pre_get_posts runs before WP_Query has been set up. Some template tags and conditional functions that rely on WP_Query will not work. For example, is_front_page() will not work, although is_home() will work. In such cases, you will need to work directly with the query vars, which are passed to the pre_get_posts hook as an argument ($query in examples on this page).
link

here is the fix:

public function inject_search_event($query) {

			if ( ! $this->is_pixel_enabled() ) {
				return;
			}

			if ( ! is_admin() && $query->is_search() && '' !== get_search_query() && 'product' === get_query_var( 'post_type' ) ) {

				if ( $this->pixel->is_last_event( 'Search' ) ) {
					return;
				}

				// needs to run before wc_template_redirect, normally hooked with priority 10
				add_action( 'template_redirect',            [ $this, 'send_search_event' ], 5 );
				add_action( 'woocommerce_before_shop_loop', [ $this, 'actually_inject_search_event' ] );
			}
		}

aka: passing $query as an argument, and using $query->is_search() instead of is_search()
😘

@jnz31
Copy link
Author

jnz31 commented May 11, 2021

here is another solution, also involving the use of the $query argument:

        public function inject_search_event($query)
        {

            if (is_admin() || !$query->is_main_query() || !$this->is_pixel_enabled()) {
                return;
            }

            if (is_search() && '' !== get_search_query() && 'product' === get_query_var('post_type')) {

                if ($this->pixel->is_last_event('Search')) {
                    return;
                }

                // needs to run before wc_template_redirect, normally hooked with priority 10
                add_action('template_redirect', [$this, 'send_search_event'], 5);
                add_action('woocommerce_before_shop_loop', [$this, 'actually_inject_search_event']);
            }
        }

@haszari
Copy link
Contributor

haszari commented May 11, 2021

Aha, thanks for digging @jnz31 !

@haszari haszari added priority: low The issue/PR is low priority—not many people are affected or there’s a workaround, etc. type: bug The issue is a confirmed bug. and removed needs feedback The issue/PR needs a response from any of the parties involved in the issue. labels May 11, 2021
@haszari haszari changed the title Fatal error is_search() Fatal error is_search() with custom page / query May 11, 2021
@haszari haszari added the feature: pixel events & conversion tracking Related to Facebook pixel, events, and conversion tracking. label May 11, 2021
@melek
Copy link

melek commented Jul 27, 2021

@jnz31 also shared this error in a post on the .org support forum with a streamlined description of the fix which may be useful here as well:

inside facebook-commerce-events-tracker.php i changed
public function inject_search_event() { to public function inject_search_event($query) {, and
if ( ! $this->is_pixel_enabled() ) { to if ( ! $this->is_pixel_enabled() || ! $query->is_main_query() ) { to solve the issue.

@jnz31
Copy link
Author

jnz31 commented Sep 22, 2021

yo? can someone implement this easy fix plz?!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature: pixel events & conversion tracking Related to Facebook pixel, events, and conversion tracking. priority: low The issue/PR is low priority—not many people are affected or there’s a workaround, etc. type: bug The issue is a confirmed bug.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants