Created
August 23, 2020 13:19
-
-
Save finalwebsites/242efd5e0ea35d39f3c75545eb95a214 to your computer and use it in GitHub Desktop.
Hierarchical categories for the plugin WooCommerce Google Product feed
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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