Index WooCommerce product variation SKUs with the parent product
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// index WooCommerce product_variation SKUs with the parent post | |
function my_searchwp_index_woocommerce_variation_skus( $extra_meta, $post_being_indexed ) { | |
// we only care about WooCommerce Products | |
if ( 'product' !== get_post_type( $post_being_indexed ) ) { | |
return $extra_meta; | |
} | |
// retrieve all the product variations | |
$args = array( | |
'post_type' => 'product_variation', | |
'posts_per_page' => -1, | |
'fields' => 'ids', | |
'post_parent' => $post_being_indexed->ID, | |
); | |
$product_variations = get_posts( $args ); | |
if ( ! empty( $product_variations ) ) { | |
// store all SKUs as a Custom Field with a key of 'my_product_variation_skus' | |
$extra_meta['my_product_variation_skus'] = array(); | |
// loop through all product variations, grab and store the SKU | |
foreach ( $product_variations as $product_variation ) { | |
$extra_meta['my_product_variation_skus'][] = get_post_meta( absint( $product_variation ), '_sku', true ); | |
} | |
} | |
return $extra_meta; | |
} | |
add_filter( 'searchwp_extra_metadata', 'my_searchwp_index_woocommerce_variation_skus', 10, 2 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment