Skip Navigation

[Resolved] Function to check current product category 's parent

This support ticket is created 4 years, 10 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 -
- 13:00 – 18:00 13:00 – 18:00 13:00 – 18:00 14:00 – 18:00 13:00 – 18:00 -

Supporter timezone: America/Jamaica (GMT-05:00)

Tagged: 

This topic contains 9 replies, has 2 voices.

Last updated by Shane 4 years, 9 months ago.

Assisted by: Shane.

Author
Posts
#1539347

I'm developing a Woocommerce shop and need to display views conditionally, depending on the parent term of current product category archive page.

The shop has two main categories, Woman and Men.

For example, I need to show a view only if you are on woman's category archive page (this is ok using [wpv-taxonomy-archive]' eq 'Woman') OR if the current archive page parent is Woman. The same with man parent category. Also need to be hable to use the same function to check for future parent categories.

I know I have to use something like this [wpv-conditional if="( '[wpv-taxonomy-archive]' eq 'Woman' ) OR ( archive_term_parent_category() eq 'Woman' )"][/wpv-conditional] based on this post https://toolset.com/forums/topic/how-to-set-condition-on-filter-controls/ but need your help with the function to get current archive parent category, please.

This is the production site hidden link

#1539629

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Jorge,

Thank you for getting in touch.

Currently the way you will get the category of the current archive page would be to use the shortcode [wpv-archive-title].
https://toolset.com/documentation/user-guides/views/views-shortcodes/#vf-295335
This is how you will get the title of the current archive.

Thanks,
Shane

#1540421

I need the PARENT category of the current archive page please..

#1541163

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Jorge,

Unfortunately you're not able to do this with our shortcodes.

However I've written a custom shortcode that should be able to get this done for you.

// Add Shortcode
function wp_parent_archive_title( $atts ) {

	// Attributes
	$atts = shortcode_atts(
		array(
			'taxonomy' => '',
			'term' => '',
		),
		$atts
	);

	$term = get_term_by( 'slug', get_query_var( ''.$atts['term'].'' ), get_query_var( ''.$atts['taxonomy'].'' ) );
	$parent = get_term_by( 'id', $term->parent, get_query_var( ''.$atts['taxonomy'].'' ) );
	if($parent):
	    return $parent->name;
	endif;

}
add_shortcode( 'wp_parent_archive_title', 'wp_parent_archive_title' );

You can use the shortcode like this [wp_parent_archive_title taxonomy="taxonomy-slug" term="term-slug"] where you will replace the taxonomy slug with the slug of your taxonomy and the term-slug with the slug of your term.

So in your case you can do this [wp_parent_archive_title taxonomy="taxonomy-slug" term="[wpv-taxonomy-slug]"]

Please let me know if this helps.
Thanks,
Shane

#1541165

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

You can add the custom shortcode to Toolset->Settings->Custom code and then activate it.

#1542549

Sorry I think I'm not been clear.

I need to get the ''parent archive title'' of the ''current archive page'' INSIDE a condition, just like this:

[wpv-conditional if="( wp_parent_archive_title() eq 'Woman' )"]SHOW THIS VIEW[/wpv-conditional]

This condition will be inside a layout for "Woocommerce archive pages" made with Toolset Layouts plugin.

Thanks in advance

#1542551

Maybe the shortcode is ok but I don't understand how to get it work inside the conditional shortcode.

#1542595

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Jorge,

Change the code i provided to this .

// Add Shortcode
function wp_parent_archive_title( $atts ) {
 
    // Attributes
    $atts = shortcode_atts(
        array(
            'taxonomy' => '',
            'term' => get_queried_object()->term_slug,
        ),
        $atts
    );
 
    $term = get_term_by( 'slug', get_query_var( ''.$atts['term'].'' ), get_query_var( ''.$atts['taxonomy'].'' ) );
    $parent = get_term_by( 'id', $term->parent, get_query_var( ''.$atts['taxonomy'].'' ) );
    if($parent):
        return $parent->name;
    endif;
 
}
add_shortcode( 'wp_parent_archive_title', 'wp_parent_archive_title' );

Then go to Toolset -> Settings -> frontend and add "wp_parent_archive_title" to the toolset 3rd party shortcode arguments section.

Finally you should be able to do this.


[wpv-conditional if="( ' [wp_parent_archive_title taxonomy='taxonomy-slug']' eq 'Woman' )"] [/wpv-conditional]

Finally just use replace taxonomy-slug with the slug of the taxonomy created in Types.

Thanks,
Shane

#1543789

Hello, thanks for your answer. I have added the code and shortcode.

I'm using the condition inside a the layout for Woocommerce Category Pages, but is not working.

[wpv-conditional if="( ' [wp_parent_archive_title taxonomy='product_cat']' eq 'Woman' )"][wpv-view name="tienda-mujer"][/wpv-conditional]

"Finally just use replace taxonomy-slug with the slug of the taxonomy created in Types." The taxonomy is created by default by Woocommerce plugin I didn't create it with Types, just in case this is part of the problem.

#1544533

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Jorge,

Would you mind allowing me to have admin access to the website to check on the setup in some more details ?

Please let me know the page that you are testing this out on.

Thanks,
Shane