Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

GetShopped (wp-ecommerce): how to get categories in select box? WordPress

  • SOLVED

We decided to change our interface. In the past we were listing the product categories for our WP -eCommerce shop, but now we want them in a drop down select box.

[[LINK href="http://www.wpquestions.com/question/show/id/1923"]]This is a follow up my previous question. [[/LINK]]

Sébastien suggested code to exclude one group. Right now I have this:

/*-----------------------------------------------------------------------------------*/
/* exclude a category or a group */
/*-----------------------------------------------------------------------------------*/
function my_exclude_categories($sql_segments) {
//$sql_segments[] = 'id != "13" '; //to exclude the cat 13
echo "HI";
$sql_segments[] = " `group_id`='2' "; // to exclude the group 1
return $sql_segments;
}
add_filter(wpsc_display_category_loop_category_sql_segments,'my_exclude_categories');


Right now I'm getting one category that appears (this is the group 2, called "brands").
See the attached screenshot.

Now I want this in a drop down select box.

We have 5 groups:

brands

furnishings

outerwear

knits

footwear

I need to get these 5 groups into 5 different drop down select boxes.

The material that shows up in the screenshot should only appear in one drop down box.

How do I get a category in a drop down box in WP-eCommerce?


[[UPDATE}}

So, for instance, if I want to get all the categories that are part of the group called "brands" I could use this query:

SELECT wp_wpsc_product_categories.id, wp_wpsc_product_categories.name
FROM wp_wpsc_categorisation_groups, wp_wpsc_product_categories
WHERE wp_wpsc_categorisation_groups.id =2
AND wp_wpsc_categorisation_groups.id = wp_wpsc_product_categories.group_id
AND wp_wpsc_product_categories.group_id =2


The group called "brands" has an id of 2.

So I could potentially do several custom queries like this, hard-coding the values for groups 2, 5, 6, 9, 11 and 12. That would be one approach, assuming WordPress does not throw up obstacles working with WP-eCommerce tables.

But I was assuming there was a function I could use for this? Do I really need to do custom queries for every drop down select box?

Answers (2)

2011-04-01

Denzel Chia answers:

Hi Lawrence,

Never used WP-eCommence before, but here is the codex page of WordPress detailing on how to use get_categories to get array of categories based on perimeters such as "parent"
http://codex.wordpress.org/Function_Reference/get_categories

There is also an example on select box, farther down the page.

Hope this helps.
Thanks.
Denzel


Lawrence Krubner comments:

Right but WP-eCommerce categories are stored in a separate database table, compared to regular WordPress categories, right? Or do I have that wrong?


Denzel Chia comments:

Hi

Yes you are right. It has its own tables. I didn't noticed that earlier.

Maybe you can write your database query into a function, have the result saved into options table using update_option() and hook your database query function into save_post and edit_post hook so that it runs only on edit or publish post.

On the front end, write a function to get_option() and use it to populate the drop down select box?

Thanks.
Denzel


Lawrence Krubner comments:

For instance, trying your suggestion just for the hell of it, I post this:

<?php wp_dropdown_categories(array('hide_empty' => 0, 'parent' => 2, 'name' => '', 'orderby' => 'name', 'selected' => '', 'hierarchical' => true)); ?>


The group id for brands is "2" so I assume parent=2 should get me the sub-groups. But of course, it doesn't, because these are WP-eCommerce groups.


Lawrence Krubner comments:

This is the query for getting all of group 2:

SELECT wp_wpsc_product_categories.id, wp_wpsc_product_categories.name
FROM wp_wpsc_categorisation_groups, wp_wpsc_product_categories
WHERE wp_wpsc_categorisation_groups.id =2
AND wp_wpsc_categorisation_groups.id = wp_wpsc_product_categories.group_id
AND wp_wpsc_product_categories.group_id =2


Your suggesting a custom query?


Lawrence Krubner comments:

About this:
<blockquote>
and hook your database query function into save_post and edit_post hook so that it runs only on edit or publish post.</blockquote>

Why? For the sake of speed? Why not run this as an ad-hoc query?


Denzel Chia comments:

Yes, for reducing database query.


Denzel Chia comments:

Yes, use your query for group 2 and write that into a function with id as variable so that you can reuse it as many time as you like?


Denzel Chia comments:

Example;


function category_query($id){
global $wpdb;

$query = $wpdb->get_results("SELECT wp_wpsc_product_categories.id, wp_wpsc_product_categories.name FROM wp_wpsc_categorisation_groups, wp_wpsc_product_categories WHERE wp_wpsc_categorisation_groups.id =$id AND wp_wpsc_categorisation_groups.id = wp_wpsc_product_categories.group_id AND wp_wpsc_product_categories.group_id =$id");

return $query;
}


Hope there is no syntax error.

Thanks.


Denzel Chia comments:

Hi,

By the way, totally unrelated to this question.
Codes that I posted gets hidden when viewing in Firefox.
Only Google Chrome can "see" the far right end of the code.

Thanks.

2011-04-01

Vidyut Kale answers:

<?php wp_dropdown_categories( ); ?>

Perhaps I am not understanding your question correctly?


Lawrence Krubner comments:

Using WP-eCommerce, how do I get all the subgroups of a group, into a drop down box. For instance, look at the screenshot. You can see all the sub-groups of the group "brands". How do I get them into a drop down box?