• Resolved popelix

    (@popelix)


    Hi there and thanks for this awesome plugin!

    I am having issues since Relevanssi search seems to return attributes attached to a product in draft mode. Trying to visit results gives 404 error. You may check the website link.
    The search results have the following format:

    https://www.domain.com/?post_type=product&p=XXXX&attribute_pa_name=value, where product with ID=XXXX exists and is in Draft mode.

    Please note that I have excluded attributes from being indexed in plugin settings.

    Thanks in advance!

    The page I need help with: [log in to see the link]

Viewing 12 replies - 1 through 12 (of 12 total)
  • Plugin Author Mikko Saari

    (@msaari)

    Try unindexing the product variations. Variations are not supposed to be in the search results (says WooCommerce), and including them may cause weird situations. Does the problem go away?

    Thread Starter popelix

    (@popelix)

    Thanks for the prompt reply!

    You are right, if I exclude Variations from indexing, the problem no longer exists. We really think that indexing them is a cool feature for our customers though, since they are instantly informed about the options they have when purchasing a product with e.g. volume options or sizes.

    Is there any custom code we could use to fix this?

    Plugin Author Mikko Saari

    (@msaari)

    The problem is about the post status. Relevanssi works correctly and doesn’t show the main product that has a draft status, but the variations have publish status, which makes Relevanssi think they’re fine to show.

    Add this to your theme functions.php or a code snippet:

    add_filter( 'relevanssi_post_ok', function( $ok, $post_id ) {
    	$post_type = relevanssi_get_post_type( $post_id );
    	if ( 'product_variation' === $post_type ) {
    		$parent = get_post_parent( $post_id );
    		return apply_filters( 'relevanssi_post_ok', $ok, $parent->ID );
    	}
    	return $ok;
    }, 10, 2 );
    Thread Starter popelix

    (@popelix)

    I am afraid that the search results are the same, after using your code in functions.php. I also rebuilt the index through plugin settings.

    Plugin Author Mikko Saari

    (@msaari)

    They’re the same to you, because you’re allowed to see the draft product. Try logging out.

    Thread Starter popelix

    (@popelix)

    I already tried this in a logged-out environment. I just realized that when using your code snippet, the variations only show up in the search bar, as a drop-down list and when I press Enter, they are not present in the search results page https://www.domain.com/?s=SEARCHTERM&post_types=product%2Cproduct_variation&product_cat=0

    Plugin Author Mikko Saari

    (@msaari)

    That’s unusual, considering you’re using Relevanssi Live Ajax Search. I checked this on my test site, and there both the live search and the basic search immediately respect the snippet as expected. That same filter is applied to the live search, so it should do the trick there as well.

    I suppose some kind of debugging is required to see why the filter isn’t working with the live search. Maybe add some error logging to it to see if it’s running and what it does when you do a live search?

    Thread Starter popelix

    (@popelix)

    I enabled Error logging and I performed the same search. I got the following entries in the log file:
    The first one is 9 hits in live ajax search and the second one is zero results in search results page.

    id……query……………….hits

    2…….SEARCHTERM…..9
    3…….SEARCHTERM…..0

    • This reply was modified 1 year, 1 month ago by popelix.
    • This reply was modified 1 year, 1 month ago by popelix.
    Plugin Author Mikko Saari

    (@msaari)

    Add some error logging to the filter function to see if it runs in the live search. Like this:

    add_filter( 'relevanssi_post_ok', function( $ok, $post_id ) {
    	$post_type = relevanssi_get_post_type( $post_id );
    	if ( 'product_variation' === $post_type ) {
    		$parent = get_post_parent( $post_id );
    $value = apply_filters( 'relevanssi_post_ok', $ok, $parent->ID );
    error_log( "Variation id: $post_id - Parent id: $parent->ID - Parent visibility: " . print_r( $value, true ) );
    		return $value;
    	}
    	return $ok;
    }, 10, 2 );

    Then run a live search. What does that output to the error log (the server PHP error log, not the Relevanssi search log)?

    Thread Starter popelix

    (@popelix)

    Here you are:

    [15-Feb-2024 07:50:17 UTC] Variation id: 28651 - Parent id: 28650 - Parent visibility: 1
    [15-Feb-2024 07:50:17 UTC] Variation id: 28653 - Parent id: 28650 - Parent visibility: 1
    [15-Feb-2024 07:50:17 UTC] Variation id: 28655 - Parent id: 28650 - Parent visibility: 1
    [15-Feb-2024 07:50:17 UTC] Variation id: 28656 - Parent id: 28650 - Parent visibility: 1
    [15-Feb-2024 07:50:17 UTC] Variation id: 28657 - Parent id: 28650 - Parent visibility: 1
    [15-Feb-2024 07:50:17 UTC] Variation id: 28659 - Parent id: 28650 - Parent visibility: 1
    [15-Feb-2024 07:50:17 UTC] Variation id: 28652 - Parent id: 28650 - Parent visibility: 1
    [15-Feb-2024 07:50:17 UTC] Variation id: 28654 - Parent id: 28650 - Parent visibility: 1
    [15-Feb-2024 07:50:17 UTC] Variation id: 28658 - Parent id: 28650 - Parent visibility: 1
    ...
    [15-Feb-2024 07:50:19 UTC] Variation id: 28651 - Parent id: 28650 - Parent visibility: 1
    [15-Feb-2024 07:50:19 UTC] Variation id: 28653 - Parent id: 28650 - Parent visibility: 1
    [15-Feb-2024 07:50:19 UTC] Variation id: 28655 - Parent id: 28650 - Parent visibility: 1
    [15-Feb-2024 07:50:19 UTC] Variation id: 28656 - Parent id: 28650 - Parent visibility: 1
    [15-Feb-2024 07:50:19 UTC] Variation id: 28657 - Parent id: 28650 - Parent visibility: 1
    [15-Feb-2024 07:50:19 UTC] Variation id: 28659 - Parent id: 28650 - Parent visibility: 1
    [15-Feb-2024 07:50:19 UTC] Variation id: 28652 - Parent id: 28650 - Parent visibility: 1
    [15-Feb-2024 07:50:19 UTC] Variation id: 28654 - Parent id: 28650 - Parent visibility: 1
    [15-Feb-2024 07:50:19 UTC] Variation id: 28658 - Parent id: 28650 - Parent visibility: 1
    ...
    [15-Feb-2024 07:50:21 UTC] Variation id: 28651 - Parent id: 28650 - Parent visibility:
    [15-Feb-2024 07:50:21 UTC] Variation id: 28653 - Parent id: 28650 - Parent visibility:
    [15-Feb-2024 07:50:21 UTC] Variation id: 28655 - Parent id: 28650 - Parent visibility:
    [15-Feb-2024 07:50:21 UTC] Variation id: 28656 - Parent id: 28650 - Parent visibility:
    [15-Feb-2024 07:50:21 UTC] Variation id: 28657 - Parent id: 28650 - Parent visibility:
    [15-Feb-2024 07:50:21 UTC] Variation id: 28659 - Parent id: 28650 - Parent visibility:
    [15-Feb-2024 07:50:21 UTC] Variation id: 28652 - Parent id: 28650 - Parent visibility:
    [15-Feb-2024 07:50:21 UTC] Variation id: 28654 - Parent id: 28650 - Parent visibility:
    [15-Feb-2024 07:50:21 UTC] Variation id: 28658 - Parent id: 28650 - Parent visibility:

    The product with ID=28650 is in draft mode.

    Plugin Author Mikko Saari

    (@msaari)

    I don’t understand why the draft post would appear as visible. Here’s a stricter version that should never show children of drafts:

    add_filter( 'relevanssi_post_ok', function( $ok, $post_id ) {
      $post_type = relevanssi_get_post_type( $post_id );
      if ( 'product_variation' === $post_type ) {
        $parent = get_post_parent( $post_id );
        if ( 'draft' === $parent->post_status ) {
          $ok = false;
        }
      }
      return $ok;
    }, 10, 2 );
    

    Does this work any better?

    Thread Starter popelix

    (@popelix)

    Yes, this one works like a charm. No hits both in live search and results page.

    Thanks for the great support!

Viewing 12 replies - 1 through 12 (of 12 total)
  • The topic ‘Search results include attributes linked to Draft products’ is closed to new replies.