wp e-commerce group product by category
Try the below code.
/* ----------
-------------
Continue code
-------------
---------- */
<?php
/* Check if this is the products page not the category or single page */
if( is_products_page() && wpsc_is_product() && (!wpsc_is_in_category()) && (!wpsc_is_single_product()) ) {
/* Get all the categories for wp e-commerce products */
$wpec_product_categories = get_terms( 'wpsc_product_category', 'hide_empty=0&parent=0');
foreach($wpec_product_categories as $wpec_categories){
$wpec_term_id = $wpec_categories->term_id;
$wpec_term_name = $wpec_categories->name;
$wpec_term_slug = $wpec_categories->slug;
//Skip the categories term(which comes default in wp e-commerce
if($wpec_term_slug == 'categories') {
continue;
}
//Set the args array
$wpec_args = array(
'post_status' => 'publish',
'post_type' => 'wpsc-product',
'numberposts' => 12,
'showposts' => 12,
"wpsc_product_category" => $wpec_term_slug
);
$wpec_categoryProducts = new WP_Query($wpec_args);
?>
<div class="wpec_productcat_name"><h3><?php echo $wpec_term_name; ?></h3></div>
<?php /** start the category wise - products loop here */?>
<?php while ($wpec_categoryProducts->have_posts()) : $wpec_categoryProducts->the_post();
global $wpsc_custom_meta, $wpsc_variations;
$wpsc_custom_meta = new wpsc_custom_meta( get_the_ID() );
$wpsc_variations = new wpsc_variations( get_the_ID() );
?>
<div class="default_product_display product_view_<?php echo wpsc_the_product_id(); ?> <?php echo wpsc_category_class(); ?> group">
/* ----------
-------------
Continue code - product display
-------------
---------- */
</div><!--close default_product_display-->
<?php endwhile; ?>
<?php /** end the product loop here */?>
<?php
}
else {
?>
<?php /** start the wp e-commerce default product loop here */ ?>
<?php while (wpsc_have_products()) : wpsc_the_product(); ?>
<div class="default_product_display product_view_<?php echo wpsc_the_product_id(); ?> <?php echo wpsc_category_class(); ?> group">
/* ----------
-------------
Continue code - products display(same code as above)
-------------
---------- */
</div><!--close default_product_display-->
<?php endwhile; ?>
<?php /** end the product loop here */?>
<?php
} //End of else block for products page checking
?>
The above code you have to use inside the product template of WP E-Commerce.
Steps:
– Open the wpsc-products_page.php file. – Find the products loop statement in the code.
<?php /** start the product loop here */?>
<?php while (wpsc_have_products()) : wpsc_the_product(); ?>
– Find the products loop end statement.
<?php endwhile; ?>
<?php /** end the product loop here */ ?>
– Copy the whole block in between while and endwhile for the product loop. – Then enclose those copied code inside the below mentioned condition. – Save and check the products page.
Tsybulsky Serg
Updated on November 21, 2022Comments
-
Tsybulsky Serg over 1 year
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 2I 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!
-
Malloc about 11 yearsHi, what if I want to use that code as an API (webservice) to return products of given category ID? Thanx in advance.
-
Subharanjan about 11 years@malloc: Yes you can create a function which accepts the category-id and then query the products, run a loop to format it according to your need and return. Example: $term = get_term( $category_id, 'wpsc_product_category'); $wpec_term_slug = $term->slug; $args = array( 'post_status' => 'publish', 'post_parent' => 0, 'post_type' => 'wpsc-product', 'wpsc_product_category' => $wpec_term_slug ); $products = get_posts($args);