Skip to content

Instantly share code, notes, and snippets.

@finalwebsites
Created August 23, 2020 13:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save finalwebsites/242efd5e0ea35d39f3c75545eb95a214 to your computer and use it in GitHub Desktop.
Save finalwebsites/242efd5e0ea35d39f3c75545eb95a214 to your computer and use it in GitHub Desktop.
Hierarchical categories for the plugin WooCommerce Google Product feed
<?php
add_filter( 'woocommerce_gpf_elements_google', function ( $elements, $product_id, $variation_id ) {
$elements['product_type'] = array_map( function ( $item ) {
if ( empty( $item->parent ) ) {
return $item->name;
}
$parent_list = array_reverse( get_ancestors( $item->term_id, 'product_cat', 'taxonomy' ) );
$parent_list = array_map( function ( $term_id ) {
$term = get_term( $term_id, 'product_cat' );
if ( isset( $term->name ) ) {
return $term->name;
}
return '';
}, $parent_list );
return implode( ' > ', $parent_list ) . ' > ' . $item->name;
}, get_the_terms( $product_id, 'product_cat' ) );
return $elements;
}, 10, 3 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment