質問

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.

役に立ちましたか?

解決

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.

ライセンス: CC-BY-SA帰属
所属していません wordpress.stackexchange
scroll top