Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
<?php
/**
* Index WooCommerce Product Variations
*/
// Add product variations to the list of post types to index
function my_searchwp_indexed_post_types( $post_types ) {
if ( ! in_array('product_variation', $post_types ) ) {
$post_types = array_merge( $post_types, array( 'product_variation' ) );
}
return $post_types;
}
add_filter( 'searchwp_indexed_post_types', 'my_searchwp_indexed_post_types' );
// Update the product variations in the index when the parent product gets updated
function my_swp_purge_product_variations( $post_id, $post, $update ){
if ( 'product' === $post->post_type ) {
$args = array(
'post_type' => 'product_variation',
'post_parent' => $post_id,
);
$variations = get_children( $args, ARRAY_A );
foreach( $variations as $id => $variation ) {
SWP()->purge_post( $id, true );
}
SWP()->trigger_index();
}
}
add_action( 'save_post', 'my_swp_purge_product_variations', 10, 3 );
// OPTIONAL: Add title and custom fields support for product variations
function my_searchwp_woo_custom_post_type_args( $args, $post_type ) {
if ( $post_type === 'product_variation' ) {
$args['supports'] = array( 'title', 'custom-fields' );
}
return $args;
}
// OPTIONAL (and opt-in) to enable this functionality uncomment the next line
// add_filter( 'register_post_type_args', 'my_searchwp_woo_custom_post_type_args', 999, 2 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.