Skip to content

Instantly share code, notes, and snippets.

@opicron
Created January 26, 2022 10:08
Show Gist options
  • Save opicron/753431f617d657bd6a465d857dac11fd to your computer and use it in GitHub Desktop.
Save opicron/753431f617d657bd6a465d857dac11fd to your computer and use it in GitHub Desktop.
SearchWP v4 - workaround
// Add WooCommerce Product (and Variation) SKUs to SearchWP.
// @link https://searchwp.com/documentation/knowledge-base/search-woocommerce-skus-and-variation-skus/
add_filter( 'searchwp\entry\data', function( $data, \SearchWP\Entry $entry ) {
// If this is not a Product, there's nothing to do.
if ( 'product' !== get_post_type( $entry->get_id() ) ) {
return $data;
}
$my_extra_meta_key = 'searchwp_skus';
// Retrieve all Variations.
$grouped_products = wc_get_products(
array(
'type' => 'grouped',
'status' => 'publish',
'limit' => -1,
)
);
$children = array();
if (is_array($grouped_products))
{
foreach ($grouped_products as $product)
{
#$sku = $product->get_sku();
#$data['meta'][ $my_extra_meta_key ][] = $sku;
#$product = wc_get_product( $productID );
$children = array_merge($children, $product->get_children());
#echo var_dump($children);
//get _children
}
}
#mayb not add grouped child skus
// Retrieve this Product SKU.
if (! in_array($entry->get_id(), $children))
{
$data['meta'][ $my_extra_meta_key ] = [
get_post_meta( $entry->get_id(), '_sku', true )
];
}
/*
$product_variations = get_posts( [
'post_type' => 'product_variation',
'posts_per_page' => -1,
'fields' => 'ids',
'post_parent' => $entry->get_id(),
] );*/
/*
if ( empty( $product_variations ) ) {
return $data;
}
// Append all Product Variation SKUs.
foreach ( $product_variations as $product_variation ) {
$sku = get_post_meta( $product_variation, '_sku', true );
$data['meta'][ $my_extra_meta_key ][] = $sku;
}
*/
return $data;
}, 20, 2 );
// Add our Extra Meta entry to SearchWP's UI.
// @link https://searchwp.com/documentation/knowledge-base/search-woocommerce-skus-and-variation-skus/
add_filter( 'searchwp\source\attribute\options', function( $keys, $args ) {
if ( $args['attribute'] !== 'meta' ) {
return $keys;
}
// This key is the same as the one used in the searchwp\entry\data hook above, they must be the same.
$my_extra_meta_key = 'searchwp_skus';
$option = new \SearchWP\Option( $my_extra_meta_key, 'SearchWP WooCommerce SKUs' );
// If there's already a match, remove it because we want ours there.
$keys = array_filter( $keys, function( $option ) use ( $my_extra_meta_key ) {
return $my_extra_meta_key !== $option->get_value();
} );
// Add "SearchWP WooCommerce SKUs" Option
$keys[] = $option;
return $keys;
}, 20, 2 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment