Skip to content

Commit

Permalink
Tweak: Stores taxonomies as a transient for a whole day to cache for …
Browse files Browse the repository at this point in the history
…performance.
  • Loading branch information
seb86 committed Feb 26, 2024
1 parent d163d2c commit 686dbec
Showing 1 changed file with 22 additions and 16 deletions.
38 changes: 22 additions & 16 deletions includes/rest-api/v2/class-cocart-rest-products-v2-controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -1552,24 +1552,30 @@ protected function get_price_from_tax_display_mode( $tax_display_mode ) {
* @return array Array of taxonomy terms.
*/
public function get_all_product_taxonomies( $taxonomy = 'cat', $page_num = '', $offset = '' ) {
$terms = array();
$terms = get_transient( 'cocart_products_taxonomies_' . $taxonomy );

if ( empty( $terms ) ) {
$terms = array();

$all_terms = get_terms( array(
'taxonomy' => 'product_' . $taxonomy,
'hide_empty' => false,
'orderby' => 'name',
'order' => 'ASC',
'number' => $page_num,
'offset' => $offset,
) );

$all_terms = get_terms( array(
'taxonomy' => 'product_' . $taxonomy,
'hide_empty' => false,
'orderby' => 'name',
'order' => 'ASC',
'number' => $page_num,
'offset' => $offset,
) );
foreach ( $all_terms as $term ) {
$terms[] = array(
'id' => $term->term_id,
'name' => $term->name,
'slug' => $term->slug,
'rest_url' => $this->product_rest_url( $term->term_id, $taxonomy ),
);
}

foreach ( $all_terms as $term ) {
$terms[] = array(
'id' => $term->term_id,
'name' => $term->name,
'slug' => $term->slug,
'rest_url' => $this->product_rest_url( $term->term_id, $taxonomy ),
);
set_transient( 'cocart_products_taxonomies_' . $taxonomy, $terms, DAY_IN_SECONDS );
}

return $terms;
Expand Down

0 comments on commit 686dbec

Please sign in to comment.