Mayccoll
5/17/2017 - 7:29 PM

Create Widget in function.php

Create Widget in function.php

class widget_modelos_categorias_custom extends WP_Widget {

    public function __construct() {
        $widget_details = array(
            'classname' => 'widget_modelos_categorias_custom',
            'description' => 'My plugin description'
        );
        parent::__construct( 'widget_modelos_categorias_custom', 'Woocommerce Categorias Custom', $widget_details );
    }

    public function form( $instance ) {
        // Backend Form
    }

    public function update( $new_instance, $old_instance ) {
        return $new_instance;
    }

    public function widget( $args, $instance ) {
        // Frontend display HTML
        if ( is_product() ) {
            woocommerce_add_categories();
        }
    }

}


function woocommerce_add_categories () {
  $taxonomy     = 'product_cat';
  $orderby      = 'name';
  $show_count   = 0;      // 1 for yes, 0 for no
  $pad_counts   = 0;      // 1 for yes, 0 for no
  $hierarchical = 1;      // 1 for yes, 0 for no
  $title        = '';
  $empty        = 0;

  $args = array(
    'taxonomy'     => $taxonomy,
    'orderby'      => $orderby,
    'show_count'   => $show_count,
    'pad_counts'   => $pad_counts,
    'hierarchical' => $hierarchical,
    'title_li'     => $title,
    'hide_empty'   => $empty
  );
  $all_categories = get_categories( $args );

  echo "<div class='modelo-categories'>";
  foreach ($all_categories as $cat) {
    if($cat->category_parent == 0) {
      echo "<ul>";
      $category_id = $cat->term_id;
      echo '<li><a href="'. get_term_link($cat->slug, 'product_cat') .'">'. $cat->name .'</a></li>';

              $args2 = array(
                'taxonomy'     => $taxonomy,
                'child_of'     => 0,
                'parent'       => $category_id,
                'orderby'      => $orderby,
                'show_count'   => $show_count,
                'pad_counts'   => $pad_counts,
                'hierarchical' => $hierarchical,
                'title_li'     => $title,
                'hide_empty'   => $empty
              );
              $sub_cats = get_categories( $args2 );
              if($sub_cats) {
                echo "<ul>";
                foreach($sub_cats as $sub_category) {
                  echo '<li><a href="'. get_term_link($sub_category->slug, 'product_cat') .'">'. $sub_category->name  .'</a></li>';
                }
                echo "</ul>";
              }

      echo "</ul>";
    }
  }
  echo "</div>";
}