0

I am looking for a way to alter the related products functionality to relate one category of products with another. For example:

I have 4 categories: Spoons, Cups, Bowls, Straws. When you select a product in spoons, I want the related products to only display things from the Bowls Categories. And when you select Cups I want only items from Straws to show.

I have tried using tags, but I have tens of thousands of products to go through and that would be a nightmare. I have also tried other plugins including WP Related Items (WRI) by WebshopLogic but it does not offer this feature.

ANY help on how to about tackling this issue is greatly appreciated.

I probably have to write my own plugin to do this but I dont have a lot of experience doing that.

closed as off-topic by birgire, kaiser Apr 4 '16 at 19:17

This question appears to be off-topic. The users who voted to close gave this specific reason:

If this question can be reworded to fit the rules in the help center, please edit the question.

  • Hey, welcome to the site! This site is for answering specific questions, particularly after evidence of attempting it yourself. Ideally it would come up as: Hi, I've done a expected to get b but got c. – Joshua Mar 18 '16 at 5:38
1

I think the following procedure will help you get your job done. When you select a product in spoons category, you should know its 'id' (how to find category ID).

Let's assume for the following that Spoons ID is '12' and Bowls ID is '13'.

You should make a function and pass a parameter. Here the parameter is your current related ID, like '12'. This function would return your related products based on alter category.

function get_related_posts( $relatedID ) {
    switch ( $relatedID ) {
        case "12":
            $id = 13;
            break;
        default:
            $id = 13;
    }
    $query = new WP_Query ( array (
        'post_type' => 'product',
        'tax_query' => array(
            array(
                'taxonomy' => 'your_taxonomy_name',
                'field'    => 'id',
                'terms'    => $id,
            ),
        ),
    ) );
    if ( $query->have_posts () ) {
        while ( $query->have_posts () ) {
            $query->the_post ();
            // "Do your html..... and return it."
        }
        wp_reset_postdata ();
        return "Your Html";
    } else {
        return "Nothing Found";
    }
}

And in your template file you can call this function, e.g. in category page place this code:

echo get_related_posts( 12 );

I think this is the basics. You can find it at WP_Query Codex. Hope it works for you. I haven't tested it for stability.

  • there is also the filter "woocommerce_product_upsell_ids" where you can select which product you want to associate with the actual product – mmm Mar 17 '16 at 19:23

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