Skip to content

Instantly share code, notes, and snippets.

@pirey
Last active January 17, 2017 09:50
Show Gist options
  • Save pirey/aa92cccea6bb306bd006db01ad1cb345 to your computer and use it in GitHub Desktop.
Save pirey/aa92cccea6bb306bd006db01ad1cb345 to your computer and use it in GitHub Desktop.
Get all categories with additional fields in woocommerce
<?php
function wc_get_categories() {
$all_categories = get_categories(array(
'taxonomy' => 'product_cat'
));
foreach ($all_categories as &$cat) {
$thumbnail_id = get_woocommerce_term_meta( $cat->term_id, 'thumbnail_id', true );
$cat->thumbnail_image = wp_get_attachment_url( $thumbnail_id );
$cat->link = get_term_link( $cat->slug, 'product_cat' );
}
return $all_categories;
// return this instead if you want to return only root categories
// return array_map($all_categories, function($cat) {
// return $cat->parent == 0;
// });
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment