Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- // index WooCommerce product_variation Attributes with the parent post
- function my_searchwp_wcpv_index_woocommerce_variation_attributes( $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_attributes'] = array();
- // loop through all product variations, grab and store the attributes
- foreach ( $product_variations as $product_variation_id ) {
- $product_variation = new WC_Product_Variation( $product_variation_id );
- $extra_meta['my_product_variation_attributes'][] = SWP()->clean_term_string( $product_variation->get_formatted_variation_attributes() );
- }
- }
- return $extra_meta;
- }
- add_filter( 'searchwp_extra_metadata', 'my_searchwp_wcpv_index_woocommerce_variation_attributes', 10, 2 );
- function my_searchwp_wcpv_custom_field_keys( $keys ) {
- $keys[] = 'my_product_variation_attributes';
- return $keys;
- }
- add_filter( 'searchwp_custom_field_keys', 'my_searchwp_wcpv_custom_field_keys', 10, 1 );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement