You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The products on my website have like 2 -3 categories, so I need to push that data into the dataLayer. I thought in this code below, but can't get it done, how to achieve that ?
<?php
/**
* Lists all product categories and sub-categories in a tree structure.
*
* @return array
*/
function list_product_categories() {
$categories = get_terms(
array(
'taxonomy' => 'product_cat',
'orderby' => 'name',
'hide_empty' => false,
)
);
$categories = treeify_terms($categories);
return $categories;
}
/**
* Converts a flat array of terms into a hierarchical tree structure.
*
* @param WP_Term[] $terms Terms to sort.
* @param integer $root_id Id of the term which is considered the root of the tree.
*
* @return array Returns an array of term data. Note the term data is an array, rather than
* term object.
*/
function treeify_terms($terms, $root_id = 0) {
$tree = array();
foreach ($terms as $term) {
if ($term->parent === $root_id) {
array_push(
$tree,
array(
'name' => $term->name,
'slug' => $term->slug,
'id' => $term->term_id,
'count' => $term->count,
'children' => treeify_terms($terms, $term->term_id),
)
);
}
}
return $tree;
}
The text was updated successfully, but these errors were encountered:
Hi
The products on my website have like 2 -3 categories, so I need to push that data into the dataLayer. I thought in this code below, but can't get it done, how to achieve that ?
The text was updated successfully, but these errors were encountered: