Shortocode - Lista Categorie e Brands
function woocommerce_subcats_from_brand( $product_parent_cat_NAME, $title_widget ) {
global $wpdb, $post;
//$IDbyNAME = get_term_by('slug', $parent_cat_NAME, 'product_cat');
//$product_cat_ID = $IDbyNAME->term_id;
$IDproductByNAME = get_term_by('slug', $product_parent_cat_NAME, 'product_cat');
$product_parent_cat_ID = $IDproductByNAME->term_id;
$args = array(
'hierarchical' => 1,
'show_option_none' => '',
'hide_empty' => 0,
'parent' => '',
'taxonomy' => 'product_brand'
);
$brands = get_categories($args);
echo '<div class="widget woocommerce widget_product_categories"><h3>'. $title_widget .'</h3>';
echo '<ul class="product-categories parent" >';
foreach ($brands as $brand) {
$link = get_term_link( $brand->slug, $brand->taxonomy );
echo '<li class="cat-item cat-parent parent"><a href="'. $link .'">'.$brand->name.'</a></li>';
$sql = "SELECT DISTINCT wt.* FROM wp_posts p
INNER JOIN wp_term_relationships r ON r.object_id=p.ID
INNER JOIN wp_term_taxonomy t ON t.term_taxonomy_id = r.term_taxonomy_id
INNER JOIN wp_terms wt on wt.term_id = t.term_id
WHERE p.ID IN (
SELECT post.ID FROM `wp_posts` as post
INNER JOIN wp_term_relationships rs ON rs.object_id = post.ID
WHERE `post_type` = 'product'
AND `post_status` = 'publish'
AND rs.term_taxonomy_id = ". $brand->term_id ."
) AND t.taxonomy='product_cat'
AND t.parent = " . $product_parent_cat_ID;
$prod_categories = $wpdb->get_results( $sql );
// VISUALIZZO IL SOTTO MENU DEL BRAND
$view_brand = false;
if( isset($_GET['product_brand']) AND ($_GET['product_brand'] == $brand->slug) ) {
$view_brand = true;
}
if( is_singular('product') ) {
$current_brands = get_the_terms( $post->ID, 'product_brand', true);
foreach ($current_brands as $current_brand) {
if($current_brand->slug == $brand->slug) $view_brand = true;
}
}
if( $prod_categories AND $view_brand ) {
echo '<ul class="children">';
foreach ($prod_categories as $prod_category) {
echo '<li class="cat-item"><a href="'. get_site_url() .'?product_brand='. $brand->slug .'&product_cat='. $prod_category->slug .'">'. $prod_category->name .'</a></li>';
}
echo '</ul>';
echo '</div>';
}
}
echo '</ul>';
}
// Add Shortcode
function lista_brands( $atts ) {
// Attributes
$attr = shortcode_atts(
array(
'parent_products' => '',
'title_widget' => ''
), $atts );
// Code
woocommerce_subcats_from_brand($atts['parent_products'], $atts['title_widget']);
}
add_shortcode( 'ipac-lista-brands', 'lista_brands' );