• Resolved memwebproject

    (@memwebproject)


    hi,

    i want a menu visible only in a woocommerce category and in all its subcategories.

    if i use is_product_category( ‘optika-microscopes’ ) menu is visible only in the top level category optika-microscopes

    i have a lot of subcategories.

    thanks for your help

    regards

Viewing 12 replies - 1 through 12 (of 12 total)
  • Plugin Author shazdeh

    (@shazdeh)

    Hi,

    You could try something like this:

    
    is_product_category( 'optika-microscopes' ) || cat_is_ancestor_of( 69, get_queried_object()->term_id )
    

    In the snippet 69 is the ID of the optika-microscopes category, since the cat_is_ancestor_of function works with IDs and not slugs. So now the menu will be displayed on both parent category archive and its children’s.

    • This reply was modified 7 years, 9 months ago by shazdeh.
    • This reply was modified 7 years, 9 months ago by shazdeh.
    Thread Starter memwebproject

    (@memwebproject)

    hi,

    thanks for your replay.

    it’s working in all subcategories, but menu disappears in the single page product.
    a little help more?

    best regards

    Plugin Author shazdeh

    (@shazdeh)

    For that you need the is_product conditional tag. See: https://docs.woocommerce.com/document/conditional-tags/#section-7

    Thread Starter memwebproject

    (@memwebproject)

    hi,

    i tryed with is_product_category( 'optika-microscopes' ) || cat_is_ancestor_of( 653, get_queried_object()->term_id ) || (is_product() && cat_is_ancestor_of( 653, get_queried_object()->term_id ))

    but it’s not working in product page.
    is it the right way?

    best regards

    Thread Starter memwebproject

    (@memwebproject)

    hi,

    for example using this code is_product_category( 'optika-microscopes' ) || cat_is_ancestor_of( 653, get_queried_object()->term_id ) || (is_product() && has_term( array('brightfield','stereomicroscopes','Inverted Microscopes','camera digital solutions','Refractometers & Polarimeter'), 'product_cat' ) )

    i have to add all my subcategories ( i have a lot of subcategories) and it’s working for the 5 subcategories.

    i’m looking for a solution for all subcategories of ‘optika-microscopes’

    i hope you can help me

    best regards

    Plugin Author shazdeh

    (@shazdeh)

    You could try something like this:

    
    has_term( get_terms( array( 'child_of' => 69, 'taxonomy' => 'product_cat', 'fields' => 'names' ) ), 'product_cat' )
    

    get_terms returns a list of all product_cat terms that are child of 69, then that is compared against the product_cat terms assigned to the current post with the has_term function.

    Thread Starter memwebproject

    (@memwebproject)

    thaks a lot for your fantastic support!!!

    best regards

    @shazdeh
    @memwebproject
    Hey guys,
    has_term( get_terms( array( 'child_of' => 69, 'taxonomy' => 'product_cat', 'fields' => 'names' ) ), 'product_cat' )
    when I use this code for each subcategory, they just won’t show up when I click on a parent category. Where am I wrong?

    Figured that out myself already.
    is_product() && has_term( 'jackets', 'product_cat' ) || is_product_category( '30' ) || cat_is_ancestor_of( 30, get_queried_object()->term_id ),
    where ‘jackets’ has to be replaced with one’s category slug, and ’30’—with a category ID.

    Thread Starter memwebproject

    (@memwebproject)

    my custom code is not working in product page, why? i upgraded woocommerce, in category and subcategories menu appears

    my code is:
    is_product_category( ‘optika-microscopes’ ) || cat_is_ancestor_of( 653, get_queried_object()->term_id ) || (is_product() && has_term( get_terms( array( ‘child_of’ => 653, ‘taxonomy’ => ‘product_cat’, ‘fields’ => ‘name’ ) ), ‘product_cat’ ))

    can you help me?

    @memwebproject
    Check my reply which is right above yours.
    Try to use this code:
    is_product() && has_term( 'optika-microscopes', 'product_cat' ) || is_product_category( '653' ) || cat_is_ancestor_of( 653, get_queried_object()->term_id )

    Thread Starter memwebproject

    (@memwebproject)

    hi,
    thanks @bzapototskyy but not worked for me.

    i have this code : is_product_category( ‘optika-microscopes’ ) || cat_is_ancestor_of( 653, get_queried_object()->term_id ) || (is_product() && has_term( get_terms( array( ‘child_of’ => 653, ‘taxonomy’ => ‘product_cat’, ‘fields’ => ‘names’ ) ), ‘product_cat’ ))

    but in product pages stopped to work

    @shazdeh can you help me please?

Viewing 12 replies - 1 through 12 (of 12 total)
  • The topic ‘is_product_category and all subcategories’ is closed to new replies.