Skip to content

Instantly share code, notes, and snippets.

@santanup789
Last active January 5, 2022 10:57
Show Gist options
  • Save santanup789/fd93dfa587d415741bb5ebec0adcd49d to your computer and use it in GitHub Desktop.
Save santanup789/fd93dfa587d415741bb5ebec0adcd49d to your computer and use it in GitHub Desktop.
Add woocommerce product categories after shop loop item title using woocommerce hook
<?php
//add Product category and number of flavour attribute after shop loop title
add_filter( 'woocommerce_after_shop_loop_item_title', 'so58344618_variation_count' );
function so58344618_variation_count() {
global $product;
$categories = get_the_terms( $post->ID, 'product_cat' );
$catList = '';
foreach($categories as $category) {
if(!empty($catList)) {
$catList .= ', ';
}
//$catID = get_cat_ID( $cat->cat_name );
//$catLink = get_category_link( $catID );
$catList .= '<span>'.$category->name.'</span>';
//echo '<span>'.$category->name .'</span>';
}
echo '<div class="cats">'.$catList.'</div>';
if ( $product->is_type( 'variable' ) ) {
//$variations = $product->get_available_variations();
//$variations = $product->get_attribute( 'pa_flavours' );
$term = wc_get_product_terms( $product->id, 'pa_flavours', array( 'fields' => 'names' ) );
echo "<div class='avail_flavours'>". count( $term ) . ' Flavours</div>';
}
}
//END---add Product category and number of flavour attribute after shop loop title
//remove add to cart from shop loop
remove_action('woocommerce_after_shop_loop_item', 'woocommerce_template_loop_add_to_cart');
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment