2

I have been trying to hide all products from a specific category (from shop page and single page) using this StackOverFlow answer code and following instructions on this other forum thread

On "Exclude specific product categories on Woocommerce single product pages" answer code I have defined one of my product categories as follow (here the term Id 43):

$category_ids = array( 43 );

I just need that all products of this category (ID 43) to be added to the cart and buy.

One of the products of the category ID 43 "Plan": https://mamasmateas.atac.cl/product/plan-personalizado-sin-seguimiento/

The other code I have tested is:

add_filter( 'get_terms', 'ts_get_subcategory_terms', 10, 3 );

function ts_get_subcategory_terms( $terms, $taxonomies, $args ) {

$new_terms = array();

// if it is a product category and on the shop page
if ( in_array( 'product_cat', $taxonomies ) && ! is_admin() && is_shop() ) {

foreach ( $terms as $key => $term ) {

if ( ! in_array( $term->slug, array( 'plan' ) ) ) { //pass the slug name here
$new_terms[] = $term;
}
}
$terms = $new_terms;
}

return $terms;
}

Any help would be much appreciated.

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
  • Hi! the code worked perfectly. Was a problem with a plugin I have activated. I have two problem with the solution: 1) the category filter still show the hidden category. 2) I have been using the add to cart preset button available in WP Bakery for woocommerce and with this code the button is not showing. Could there be a extra code to enable it? or other way to get a "add to cart" button for the products of this specific category?. Many thanks in advance – Álvaro Cercós May 08 '20 at 00:53
  • Hi guys, Im still having problems with this issue. First, i can´t use the add to cart button of WP Bakery for woocommerce, it seems that with the hidden category it is disable this option. Second, I haven´t been able to also hide the category in the shop option. Is it possible to enable this add to cart option with the code above? And it is possible after that to hide the specific category? Many thanks in advance and hope to get the last help on this issue. – Álvaro Cercós May 12 '20 at 02:27

1 Answers1

0
add_action( 'woocommerce_shop_loop', 'woocommerce_shop_loop' );

function woocommerce_shop_loop() {
    global $product;

    $category_ids = array( 20 );
    var_dump(has_term( $category_ids, 'product_cat', $product->get_id());
    if ( has_term( $category_ids, 'product_cat', $product->get_id() ) ) {
        unset($GLOBALS['product']);

    }
}
mujuonly
  • 11,370
  • 5
  • 45
  • 75
  • Hi and thanks for your quick reply. I deleted all the code I have used and added this one but didn´t work. I change the code to $category_ids = array( 43 ); and it also didn´t work. Is there anything I could be doing wrong? – Álvaro Cercós May 06 '20 at 20:45