Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

Sorry for my bad english.
I have WP 3.3.1 & wp-e-commerce.3.8.7.6.2. On the products page (which use wpsc-products_page.php template) I have list of products. I want to group this products by category. For example:

**Cat1
Product 1
Product 2

** Cat2
Product 1
Product 2

** Cat3
Product 1
Product 2

I try to use this method, but it`s does not work

add_filter('posts_join', create_function('$a', 'global $wpdb; return $a . " INNER JOIN $wpdb->term_relationships ON ($wpdb->posts.ID = $wpdb->term_relationships.object_id) INNER JOIN $wpdb->term_taxonomy ON ($wpdb->term_relationships.term_taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id) ";'));
add_filter('posts_where', create_function('$a', 'global $wpdb; return $a . " AND $wpdb->term_taxonomy.taxonomy = \'wpsc_product_category\'";'));
add_filter('posts_orderby', create_function('$a','global $wpdb; return "$wpdb->term_taxonomy.term_id DESC";'));
query_posts('');

Thank you all in advance for your reply!

share|improve this question

2 Answers 2

Here you can find a solution : http://subharanjan.in/category-wise-products-display-in-wp-e-commerce/

I am not sure if this will solve your problem.

Thanks!

share|improve this answer
    
An answer should be more than just a link to an external site. Please add a solution. –  toscho Dec 19 '12 at 23:58

You are very close. I had the same problem a while ago, and blogged about it. Your join is missing one more join table, wp_terms, which has the term you want to sort on.

Here is the code I used:

class OrderWpscProducts {

    // flag for when a shop products post query is being created
    private $isProductQuery = false;

    public function __construct() {
        // hooks for altering order of product pages
        add_filter('parse_query', array($this, 'filterProductQueryMark'), 20);
        add_filter('posts_join', array($this, 'filterProductQueryJoins'), 20);
        add_filter('posts_orderby', array($this, 'filterProductQueryOrderBy'), 20);
    }

    /**
    * detect a posts query that should return a list of shop products, 
    * and mark it for later filters
    * @param WP_Query $query
    * @return WP_Query
    */
    public function filterProductQueryMark($query) {
        $this->isProductQuery = false;

        if (!is_admin() && (wpsc_is_in_category() || !empty($query->query_vars['wpsc_wine_style']))) {
            $this->isProductQuery = true;
        }
    }

    /**
    * if the posts query is for a list of shop products, 
    * then add some tables to the join so that we can sort by category name
    * @param string $joins
    * @return string
    */
    public function filterProductQueryJoins($joins) {
        global $wpdb;

        if ($this->isProductQuery) {
            $joins .= "
 INNER JOIN $wpdb->term_relationships wpsc_cat_rel ON wp_posts.ID = wpsc_cat_rel.object_id
 INNER JOIN $wpdb->term_taxonomy wpsc_cat_tax ON wpsc_cat_tax.term_taxonomy_id = wpsc_cat_rel.term_taxonomy_id
   and wpsc_cat_tax.taxonomy = 'wpsc_product_category'
 INNER JOIN $wpdb->terms wpsc_cat ON wpsc_cat.term_id = wpsc_cat_tax.term_id
";
        }

        return $joins;
    }

    /**
    * if the posts query is for a list of shop products, then sort by category name and product title
    * @param string $orderby
    * @return string
    */
    public function filterProductQueryOrderBy($orderby) {
        if ($this->isProductQuery) {
            $orderby = 'wpsc_cat.name ASC, wp_posts.post_title ASC';
        }

        return $orderby;
    }
}

// create instance, will be kept alive by hooks
new OrderWpscProducts();
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.