WP E-Commerce displays all the products from all the categories in the default products page which uses the template file: wpsc-products_page.php for displaying. There are situations when we have product categories, under each category we have different products. Now we have to display all the products grouped by product-categories. Like:
1 2 3 4 5 6 7 8 9 10 11 12 13 | Product-Category#1 – Product #1 – Product #2 – Product #3 Product-Category#2 – Product #4 – Product #5 Product-Category#3 – Product #6 – Product #7 – Product #8 |
Solution:
We can achieve this using short-codes provided by wp e-commerce, but then we have to write the shortcode that many number of times as we have number of categories.
1 2 3 4 5 | [wpsc_products category_id='4'] [wpsc_products category_url_name='test-category'] [wpsc_products category_url_name='test-category' number_per_page='16' sort_order='price' order='desc'] |
Find a complete list of wp e-commerce category shortcodes with additional arguments
There are some other problems like: we have to create another product page,write the short codes there and keep the default products page intact, else chances are there that the single product page, category page etc. may break or show page not found error.
So another option is to change the default products-page template file( wpsc-products_page.php ) to display products group by categories. This can be done by running multiple loops in the the products page template file. I have used this once and works fine for me. Use this if it suits your requirement. 🙂
Steps:
— Open the wpsc-products_page.php file.
— Find the products loop statement in the code.
1 2 3 4 | <?php /** start the product loop here */?> <?php while (wpsc_have_products()) : wpsc_the_product(); ?> |
— Find the products loop end statement.
1 2 3 4 | <?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.
Example Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 | <?php /* =========================================================================== */ /* ADDED/MODIFIED CODE -- CUSTOM LOOP TO DISPLAY THE 12 PRODUCTS CATEGORY WISE */ /* =========================================================================== */ /* ------ CHECK IF THIS IS THE DEFAULT PRODUCTS PAGE ------- */ if( is_products_page() && wpsc_is_product() && (!wpsc_is_in_category()) && (!wpsc_is_single_product()) ) { /* --------- GET ALL THE PRODUCT CATEGORIES ---------- */ $wpec_product_categories = get_terms( 'wpsc_product_category', 'hide_empty=0&parent=0'); /* ---------- RUN A LOOP FOR EACH PRODUCT CATEGORIES ----------- */ 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; /* --------- JUST PRODUCT CATEGORY WHICH IS DEFAULT IN WPSC ---------- */ if($wpec_term_slug == 'categories' || $wpec_term_slug == 'product-category') { continue; } /* --------- SET THE PRODUCT QUERY ARGUMENTS 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 /* =============================================================================== */ /* ADDED/MODIFIED CODE -- ADD THE WPSC PRODUCT DISPLAY CODE BELOW WITH SOME CHANGE */ /* =============================================================================== */ ?> <div class='wpsc_default_product_list'> <?php /* ====================================================================== */ /* ADDED/MODIFIED CODE -- CHANGE THE PRODUCT LOOP BELOW TO OUR CUSTOM ONE */ /* ====================================================================== */ 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() ); /* ======================================================================= */ /* ADDED/MODIFIED CODE -- CHANGED THE PRODUCT LOOP ABOVE TO OUR CUSTOM ONE */ /* ======================================================================= */ <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 */?> </div> <?php /* ============================================================================ */ /* ADDED/MODIFIED CODE -- END OF PRODUCT LOOP, END OF FOREACH LOOP - CATEGORIES */ /* ============================================================================ */ } /* END OF FOREACH LOOP OF PRODUCT CATEGORIES */ } /* END OF IF BLOCK WHICH CHECKS IF THIS IS PRODUCTS PAGE ONLY */ else { /* =========================================================================== */ /* ADDED/MODIFIED CODE -- START THE ELSE BLOCK WHICH CONTAINS THE DEFAULT CODE */ /* =========================================================================== */ ?> <div class='wpsc_default_product_list'> <?php /** start the 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 - product display(same as previous) ------------- ---------- */ </div><!--close default_product_display--> <?php endwhile; ?> <?php /** end the product loop here */?> </div><!-- #wpsc_default_product_list --> <?php /* ======================================================================== */ /* ADDED/MODIFIED CODE -- END OF ELSE BLOCK WHICH CONTAINS THE DEFAULT CODE */ /* ======================================================================== */ } /* END OF ELSE BLOCK FOR CHECK THE PRODUCTS PAGE */ ?> |
Download the modified wpsc-products_page.php !!