In Woocommerce, Due to an inventory sku issue with clothing size, we have two different products for ultimately the same product. Let's call it a blue shirt, but it is 2 different products in our store due to an ordering decision. I've created a grouped product to handle the display of this.
However I can't seem to hide the linked products from my shop. When I use the WC_Product_query
class and the get_products function, it doesn't seem to return all 46 products that are being queried. I'm wondering what exactly, I'm doing wrong and/or if there is a better solution.
Placed the following in my functions.php child theme:
add_action( 'woocommerce_product_query', 'only_grouped_products_query' );
function only_grouped_products_query( $q ) {
$query = new WC_Product_Query(array($q));
$products = $query->get_products();
$linked_array = array();
$i = 0;
foreach($products as $product){
$i++;
/*
echo $product->get_name() .' - ' . $product->product_type .'<br>';
$is_grouped = $product->product_type;
if ( $is_grouped == 'grouped' )
{
// echo $product->get_name() .' - ' . $product->product_type;
$get_child_id = $product->get_children();
if (is_array($get_child_id)){
foreach($get_child_id as $child_id){
$linked_array[] = $child_id;
}
} else {
$linked_array[] = $get_child_id;
}
}
*/
}
echo $i;
// $q->set( 'post__not_in', $linked_array );
}
The above code snippet does work, but it doesn't pull in all 46 results that the query is showing when I dump the original $query variable. $i only returns 10 results.