Thread Starter
Milan
(@milanxy)
I’m sorry. You were faster and I didn’t have time to correct the text. Searching for the Name and SKU of the variant does not work. I have _sku set and I haven’t changed it.
- I don’t have a “product ID”.
- It finds everything in “admin search”.
- WooCommerce products – selected post, page, product, product_variation, product_tag
A vast majority of WooCommerce themes sets the post_type parameter to product. That makes sense, because product_variation is an internal post type and is not meant for public access in WooCommerce. In general, this method is the preferred way to access variations by SKU.
Thread Starter
Milan
(@milanxy)
The PHP code method will not cause a problem with the settings in the plugin (_sku)? Or should I turn it off?
You still need the setting for the main product SKU. The function just adds the variation SKUs for the main product so that you can find the product by searching for a variation SKU. Both are necessary.
Thread Starter
Milan
(@milanxy)
Searching by SKU now also finds the variant code, but not the name. It’s not so terrible for me. I mainly needed the SKU. THX
It’s strange that before it worked without additional PHP code.
You can modify the function so that it also adds the variation name, that’s not a big deal. Change the function to:
add_filter( 'relevanssi_content_to_index', 'rlv_index_variation_skus', 10, 2 );
function rlv_index_variation_skus( $content, $post ) {
if ( 'product' === $post->post_type ) {
$args = array(
'post_parent' => $post->ID,
'post_type' => 'product_variation',
'posts_per_page' => -1
);
$variations = get_posts( $args );
if ( ! empty( $variations ) ) {
foreach ( $variations as $variation ) {
$sku = get_post_meta( $variation->ID, '_sku', true );
$content .= " $sku {$variation->post_title}";
}
}
}
return $content;
}
I think what changed is a post type restriction in the search. If there’s no post type restriction in the search, Relevanssi will happily return the variations. Relevanssi doesn’t have any problems with them. Your theme does.