Skip to content

Instantly share code, notes, and snippets.

@wickywills
Last active September 14, 2021 04:25
Show Gist options
  • Save wickywills/d8ec7a9df046794a4c7ae272b1c25f5b to your computer and use it in GitHub Desktop.
Save wickywills/d8ec7a9df046794a4c7ae272b1c25f5b to your computer and use it in GitHub Desktop.
```
/**
* Display categories or products view
*
* If the display type (set in product categories in WP) is set to "standard", add a div with categories so that it can
* be styled. Else, use the set display type value as the class
*/
add_action('woocommerce_before_shop_loop', function(){
$current_term = get_queried_object();
$display_type = get_term_meta( $current_term->term_id );
$display_type = $display_type['display_type'][0];
switch ($display_type) {
case '':
echo '<div class="display-type__categories">';
break;
default:
echo '<div class="display-type__'.$display_type.'">';
break;
}
});
add_action('woocommerce_after_shop_loop', function(){ echo '</div> <!--END display-type__categories -->'; });
```
Or if display type is not being used:
```
add_action('woocommerce_before_shop_loop', function(){
$current_term = get_queried_object();
$display_type = get_term_meta( $current_term->term_id );
$display_type = $display_type['display_type'][0];
$term = get_term_by( 'slug', get_query_var( 'term' ), get_query_var( 'taxonomy' ) ); // get current term
$parent = get_term($term->parent, get_query_var('taxonomy') ); // get parent term
$children = get_term_children($term->term_id, get_query_var('taxonomy')); // get children
if(($parent->term_id!="" && sizeof($children)>0)) {
// has parent and child
echo '<div class="display-type__categories">';
} elseif(($parent->term_id!="") && (sizeof($children)==0)) {
// has parent, no child
echo '<div class="display-type__products">';
} elseif(($parent->term_id=="") && (sizeof($children)>0)) {
// no parent, has child
echo '<div class="display-type__categories">';
}
});
add_action('woocommerce_after_shop_loop', function(){ echo '</div> <!--END display-type__categories -->'; });
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment