Advertisement
Guest User

Untitled

a guest
Mar 27th, 2017
44
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.41 KB | None | 0 0
  1. <?php
  2.  
  3. // index WooCommerce product_variation Attributes with the parent post
  4. function my_searchwp_wcpv_index_woocommerce_variation_attributes( $extra_meta, $post_being_indexed ) {
  5.  
  6. // we only care about WooCommerce Products
  7. if ( 'product' !== get_post_type( $post_being_indexed ) ) {
  8. return $extra_meta;
  9. }
  10.  
  11. // retrieve all the product variations
  12. $args = array(
  13. 'post_type' => 'product_variation',
  14. 'posts_per_page' => -1,
  15. 'fields' => 'ids',
  16. 'post_parent' => $post_being_indexed->ID,
  17. );
  18. $product_variations = get_posts( $args );
  19.  
  20. if ( ! empty( $product_variations ) ) {
  21.  
  22. // store all SKUs as a Custom Field with a key of 'my_product_variation_skus'
  23. $extra_meta['my_product_variation_attributes'] = array();
  24.  
  25. // loop through all product variations, grab and store the attributes
  26. foreach ( $product_variations as $product_variation_id ) {
  27. $product_variation = new WC_Product_Variation( $product_variation_id );
  28. $extra_meta['my_product_variation_attributes'][] = SWP()->clean_term_string( $product_variation->get_formatted_variation_attributes() );
  29. }
  30. }
  31.  
  32. return $extra_meta;
  33. }
  34.  
  35. add_filter( 'searchwp_extra_metadata', 'my_searchwp_wcpv_index_woocommerce_variation_attributes', 10, 2 );
  36.  
  37. function my_searchwp_wcpv_custom_field_keys( $keys ) {
  38. $keys[] = 'my_product_variation_attributes';
  39.  
  40. return $keys;
  41. }
  42.  
  43. add_filter( 'searchwp_custom_field_keys', 'my_searchwp_wcpv_custom_field_keys', 10, 1 );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement