Saturday, January 28, 2012

Sorting Product Category Widget - WP E-Commerce Plugin Wordpress

Wordpress WP E-Commerce Plugin, There is a widget called Product Category Widget which lists all the product categories. During the site development you may have a lot of categories and nested categories then it will be difficult to sort the list. So here I'm helping you with a bit of code which will sort the products in the widget.

Method 1:

Edit the file :  /wp-content/plugins/wp-e-commerce/wpsc-widgets/category_widget.php
Add the below highlighted code to your php file.

$instance_categories = get_terms( 'wpsc_product_category', 'hide_empty=0&parent=0&orderby=name&order=ASC');
           
 function subval_sort($a,$subkey) {
            foreach($a as $k=>$v) {
                $b[$k] = strtolower($v[$subkey]);
            }
            asort($b);
            foreach($b as $key=>$val) {
                $c[] = $a[$key];
            }
            return $c;
        }
           
            $newArray = array();
            $tempI = $i;
            $i=0;
            foreach($instance_categories as $key=>$value)
           
            {
                $newArray[$i]['term_id'] = $value->term_id;
                $newArray[$i]['name'] = $value->name;
                $newArray[$i]['slug'] = $value->slug;
                $newArray[$i]['term_group'] = $value->term_group;
                $newArray[$i]['term_taxonomy_id'] = $value->term_taxonomy_id;
                $newArray[$i]['taxonomy'] = $value->taxonomy;
                $newArray[$i]['description'] = $value->description;
                $newArray[$i]['parent'] = $value->parent;
                $newArray[$i]['count'] = $value->count;
                $newArray[$i]['sort_order'] = $value->sort_order;
                $i++;
               
            }
            $i = $tempI;
            $newArray = subval_sort($newArray,'name');
           
            $xyz = 0;
            $instance_categories = array();
            foreach($newArray as $key=>$value)
            {
                $instance_categories[$xyz] = (object) $value;   
                $xyz++;
            }
            unset($xyz);
            unset($newArray);


           
            if(!empty($instance_categories)){
                foreach($instance_categories as $categories){


                    $instance['categories'][$categories->term_id] = 'on';
                }
            }

Method 2:  
You can use the below query also to fetch categories and can use it recursively for sub-categories.
 


                                  
    $results =$wpdb->get_results("SELECT name, slug
    FROM wp_term_taxonomy AS wtt
    JOIN wp_terms AS wt ON wt.term_id = wtt.term_id
    WHERE wtt.taxonomy = 'wpsc_product_category'
    AND wtt.parent =$category_id");

    foreach($results as $key=>$value)
    {
    ?>

  • name; ?>

  • }
    ?>                                     

1 comment:

Anonymous said...

hi this is ram. this "Sorting Product Category Widget - WP E-Commerce Plugin Wordpress" not working for me..i trye your coding.;my site is new.fieldtoplate.com/store

Popular Posts