Skip to content

Instantly share code, notes, and snippets.

@victormattosvm
Last active November 13, 2021 01:17
Show Gist options
  • Save victormattosvm/208db212be5124fbb974dbfddbd09ea6 to your computer and use it in GitHub Desktop.
Save victormattosvm/208db212be5124fbb974dbfddbd09ea6 to your computer and use it in GitHub Desktop.
Get all product terms and attributes (API rest)
<?php
/**
* Get all product terms and attributes
*
*/
function get_all_product_terms() {
global $wpdb;
$taxonomies = $wpdb->get_results( "SELECT attribute_id, attribute_name, attribute_label FROM wp_woocommerce_attribute_taxonomies" );
$data = array();
foreach ($taxonomies as $tax){
$terms = get_terms( "pa_" . $tax->attribute_name );
foreach ($terms as $term){
$data[] = array(
'term_id' => $term->term_id,
'term_name' => $term->name,
'term_slug' => $term->slug,
'attribute_id' => $tax->attribute_id,
'attribute_name' => $tax->attribute_label,
'attribute_slug' => $tax->attribute_name
);
}
}
return $data;
}
add_action('rest_api_init', function() {
register_rest_route('wp/v1', 'product_terms', [
'methods' => 'GET',
'callback' => 'get_all_product_terms',
]);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment