I'm trying to not load some products on category page based on its name. Googling I found some examples with taxonomyies, wich doesn't help me at all. So, how can I edit the code below to fit in my needs? Thx!
function check_variations( $q ) {
if ( $outofstock_term = get_term_by( 'name', 'outofstock', 'product_visibility' ) ) {
$tax_query = (array) $q->get('tax_query');
$tax_query[] = array(
'taxonomy' => 'product_visibility',
'field' => 'term_taxonomy_id',
'terms' => array( $outofstock_term->term_taxonomy_id ),
'operator' => 'NOT IN'
);
$q->set( 'tax_query', $tax_query );
}
remove_action( 'pre_get_posts', 'check_variations' );
}
UPDATE
I am really new on Woocommerce. Although It's my first time working on it, I didn't had much difficulties along the way. But I have a problem: Since it's really painfull working with products variations, I made a json file with the avaible variations depending on the category of the product (there are a tons of variations - 91 each product). On product page, I made with js and it's working just fine. Now, I just want to hide some products on the filter page category. Here is my php reading the json:
function check_variations( $q ) {
$page_url = $_SERVER['REQUEST_URI'];
if (strpos($page_url, '?filter_modelo=') !== false) {
$filter = explode('?filter_modelo=', $page_url)[1];
}
if($filter) {
$json_data = file_get_contents(get_template_directory_uri().'/release/json/variations-in-stock.json');
$json_decoded = json_decode($json_data, true);
foreach($json_decoded as $key => $value){
if (strpos(implode(",", $value[0]), $filter) !== false) {
//var_dump($key);
//Here I want to hide products with $key on its name
return;
}
}
}
}
add_action( 'woocommerce_shop_loop', 'check_variations' );
UPDATE 2
I made it by content-product.php file. It worked, but it bugs the pagination... I am trying to add this code to functions.php but I don't know how wich action/filter I have to use to get the post/product name and also prevent the pagination bug.
$page_url = $_SERVER['REQUEST_URI'];
$product_name = $product->get_title();
if (strpos($page_url, 'filter_modelo=') !== false) {
$filter = explode('filter_modelo=', $page_url)[1];
$json_data = file_get_contents(get_template_directory_uri().'/release/json/variations-in-stock.json');
$json_decoded = json_decode($json_data, true);
foreach($json_decoded as $key => $value){
if (in_array($filter, explode(', ', $value[0]['modelos']))) {
if (strpos($product_name, $key) !== false) {
return;
}
}
}
}