How to display products of the same category on a single-product page?

I have problems to display a list of products of the same category in the mp_product.php template…

Basicaly what i want to do is:

<?php mp_list_products(true, true, 0, 3, “THE CURRENT CATEGORY”, ‘DESC’:wink:; ?>

Can i use a variable for the category string in mp_list_products?

Here is an example: The product category is ‘modell-lienz’. I want to list all the other variations of colors of ‘modell-lienz’ after the product-content.

http://www.thomas-christoph.com/brugger/store/produkte/lienz-panna/

Thank you in advance…

  • Alexander
    • DEV MAN’s Mascot

    Hi @Thomas,

    The current category can be accessed with a WordPress function.

    http://codex.wordpress.org/Function_Reference/get_the_category

    If you’re within the loop, it will use the current post ID by default, so this should work:

    $cat = get_the_category();

    Or you could pass an ID to the function if you’re outside the loop:

    $cat = get_the_category($id);

    You can use this category in mp_list_products as needed now. Let me know how it goes!

    Best regards,

  • Thomas
    • WPMU DEV Initiate

    Ok, so here is what i have so far, but that doesnt work:

    <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    <?php mp_product_image( true, 'single' );
    the_content();
    mp_product_price(true);
    mp_buy_button(true, 'single');

    $cat = get_the_category();
    mp_list_products(true, true, 0, 6, '' ,'' , $cat , '', 'DESC'); ?>

    <?php endwhile; ?>
    <?php endif; ?>

    It lists all products, not considering the category…

  • Alexander
    • DEV MAN’s Mascot

    Hi @Thomas,

    Your second example looks good, but I think I was missing something before. Try modifying it like this so we pass through the name of the category instead of it’s ID

    mp_list_products(true, true, 0, 6, '' ,'' , $cat[0]->cat_name; , '', 'DESC'); ?>

    Best regards,

  • Thomas
    • WPMU DEV Initiate

    Still the same result… it looks like the array in $cat stays empty. I’m pretty sure the problem is that i’m not able to request / get the product-category…

    $cat = get_the_category(); gives me no result (an empty array)

    I am using Marketpress as a Multilanguage Site (German and Italian) and the product-categories have sub-categories.

  • Thomas
    • WPMU DEV Initiate

    Yes i tried both variants, but with no sucess… Could it be a taxonomy problem? Because the slugs such as ‘products’ and ‘category’ have been translated…

    At the moment i’m using a workaraound, which is fine for now, but in future i’ll have to solve this problem.

    Any other ideas? Thank you in advance…

  • Thomas
    • WPMU DEV Initiate

    Still the same result. the variable $terms stays empty. I don’t understand why i always get an empty array, it is like there is nothing to query…

    I tried some other things while testing, which were weird, for example i’m not able to display a Product by ID with the WP function get_post();

    Is there a difference in querying posts and products?

    Also it was strange that i couldn’t get it to work with the WPML Plug-Ins (Translations) disabled… $cat = get_the_category() stayed empty as well

    On the other hand [mp_list_products category=”a-hardcoded-category”] works perfektly fine.

    I also thought of writing a “if – elseif” loop but here i have the same problem: i can’t query the categories (or tags, i also tried that…:wink:

  • DavidM
    • DEV MAN’s Mascot

    Hi Thomas,

    The following is MarketPress’ own category listing function from template-functions.php:

    function mp_list_categories($echo = true, $args = '') {
    $args['taxonomy'] = 'product_category';
    $args['echo'] = false;

    $list = '<ul id="mp_category_list">' . wp_list_categories($args) . '</ul>';

    if ($echo)
    echo $list;
    else
    return $list;
    }

    Perhaps you could use the same method to just get the categories, then just use the first item in the array?

    EDIT: Whoops, that function gets a formatted list. I think you’ll want to use get_categories instead.

  • Alexander
    • DEV MAN’s Mascot

    @Thomas,

    What about $post? Does it contain anything? It might be that we’re not getting the post ID.

    instead of $post->ID you may need to use get_the_ID()

    Or before using post, you may need to declare that you want the global post variable by using this first: global $post

    Best regards,