Skip to content

WOOCOMMERCE PRODUCT CAT TREE Shortcode



//PRODUCT CAT TREE
function woo_cards_product_filter_shortcode( $atts ) {
	
	if ( ! function_exists( 'totalthemecore_call_non_static' ) ) {
		return;
	}

	// Load AJAX filter scripts
	totalthemecore_call_non_static( 'Vcex\Ajax', 'enqueue_scripts' );

	// Get target grid ID from shortcode attribute
	$target = isset( $atts['target'] ) ? $atts['target'] : '';
	$ajax = isset( $atts['ajax'] ) ? $atts['ajax'] : false;
	$scroll = isset( $atts['scroll'] ) ? $atts['scroll'] : false;
	$buttonclass = isset( $atts['class'] ) ? ' '.$atts['class'] : false;
	$hideempty = isset( $atts['hideempty'] ) ? $atts['hideempty'] : false;
	$taxonomy = isset( $atts['taxonomy'] ) ? $atts['taxonomy'] : 'product_cat';
	$sort = isset( $atts['sort'] ) ? $atts['sort'] : false;
	$search = isset( $atts['search'] ) ? $atts['search'] : false;
	$reset = isset( $atts['reset'] ) ? $atts['reset'] : false;

	
	//if ( empty( $target ) ) {
	//	return;
	//}
	

	// Get all parent categories in 'product_cat' taxonomy
	$parent_terms = get_terms( [
		'taxonomy'   => $taxonomy,
		'hide_empty' => $hideempty,
		'parent'     => 0,
		'orderby' => 'name',
		'order' => 'ASC',
	] );

	ob_start();

	if ($ajax) {
		//data-vcex-type="category"
		echo '<div class="woo-cards-product-filter" 
			 data-vcex-ajax-filter="1" 
			 data-vcex-ajax-filter-target="#'.esc_attr( str_replace( '#', '', $target ) ).'" 
			 data-vcex-ajax-filter-multiple="0" 
			 data-vcex-ajax-filter-relation="AND">';
	} else {
		echo '<div class="woo-cards-product-filter">';
	}

	if ( !empty( $parent_terms ) && !is_wp_error( $parent_terms ) ) {
		
		foreach ( $parent_terms as $parent ) {

			// Get child terms
			$child_terms = get_terms( [
				'taxonomy'   => $taxonomy,
				'hide_empty' => $hideempty,
				'parent'     => $parent->term_id,
				'orderby' => 'name',
				'order' => 'ASC',
			] );

			if ( empty( $child_terms ) || is_wp_error( $child_terms ) ) {
				
				//continue;
				//WENN PARENT KEIN CHILD HAT MACHE PARENT KLICKBAR UND ZEIGE ES STATT ZU SKIPPEN
				if ($ajax) {
					echo '<a href="#'.$scroll.'" class="theme-txt-link wpex-block no-child'.$buttonclass.'" data-vcex-type="product_cat" data-vcex-value="'.esc_attr( $parent->term_id ).'">'.
						esc_html( $parent->name ).'</a>';
				} else {
					$term_link = get_term_link( $parent->term_id );
					echo '<a href="'.$term_link.'" class="theme-txt-link local-scroll-link wpex-block no-child'.$buttonclass.'">'.esc_html( $parent->name ).'</a>'; //#'.$scroll.'
				}
				
			} else {
	
			echo '<details class="woo-filter-group"><summary><span>'.esc_html( $parent->name ).'</span></summary><ul class="wpex-mt-5 wpex-ml-10 wpex-list-none">';

				foreach ( $child_terms as $child ) {
					echo '<li>';

					if ($ajax) {
						echo '<a href="#'.$scroll.'" class="theme-txt-link wpex-block no-child sub-details'.$buttonclass.'" data-vcex-type="product_cat" data-vcex-value="'.esc_attr( $child->term_id ).'">'.
							esc_html( $child->name ).'</a>';
					} else {
						$term_link = get_term_link( $child->term_id );
						echo '<a href="'.$term_link.'" class="theme-txt-link wpex-block no-child sub-details local-scroll-link'.$buttonclass.'">'.esc_html( $child->name ).'</a>'; 
						//#'.$scroll.'
					}
					echo '</li>';
				}

				echo '</ul></details>';
			}
		}
	} else {
		echo 'Keine Produkt-Kategorien gefunden';
	}

	if ($ajax && $sort) {	
		echo '<select data-vcex-type="order">
			<option value="">Reihenfolge</option>
			<option value="asc">A-Z</option>
			<option value="desc">Z-A</option>
		</select>

		<select data-vcex-type="orderby">
			<option value="">Sortierung</option>
			<option value="name">Name</option>
			<option value="date">Datum</option>
			<option value="price">Preis</option>
		</select>';	
	}
	if ($ajax && $search) {
		echo '<input type="text" data-vcex-type="search" placeholder="Volltextsuche">';
	}
	if ($ajax && $reset) {
		echo '<button class="theme-button" data-vcex-type="reset">Zurücksetzen</button>';		
	}
	
	echo '</div>';

	return ob_get_clean();
}
add_shortcode( 'woo_cards_product_filter', 'woo_cards_product_filter_shortcode' );

An den Anfang scrollen