WordPress.org

Ready to get started?Download WordPress

Forums

WooCommerce - excelling eCommerce
[resolved] Make all products 'related' / all products listed under a single item (3 posts)

  1. trmash
    Member
    Posted 1 year ago #

    Hi,

    A client sells hampers, to which customers can then choose to add extras. What I'd like to do is to show the hamper as a single product, which then lists all the items in another category (e.g. 'Extras') underneath the main product. So essentially, the customer can shop from one page.

    If I create a Page within WordPress, I can make a custom product page using WooCommerce shortcodes:

    [product id="18"]
    [product_category category="extras" per_page="20" columns="4" orderby="date" order="desc"]

    I know about WooCommerce's ability to override default templates by creating your own within your theme, but I'm not sure of the correct calls. How can I replicate the above shortcode functionality on my single product page when the customer is looking at a hamper?

    Any gentle shoves in the right direction are greatly appreciated.

    http://wordpress.org/extend/plugins/woocommerce/

  2. trmash
    Member
    Posted 1 year ago #

    Found my solution! If anyone else is wondering, then WooCommerce's do_shortcode() function is your friend.

    Using the function, I could call the product_category shortcode on single-product.php.

    So for my product with an ID of 18:

    <?php if (is_product(18)) { echo do_shortcode( '[product_category category="extras" per_page="20" columns="4" orderby="date" order="desc"]' ); } ?>

  3. trmash
    Member
    Posted 1 year ago #

    I got a bit ahead of myself, turns out the above didn't really work. However, this seems to be doing the trick.

    $terms = wp_get_post_terms( $post->ID, 'product_cat' );
    foreach ( $terms as $term ) $categories[] = $term->slug;
    if (in_array( 'hampers', $categories)) {
    echo 'hampers'; echo do_shortcode( '[product_category category="extras" per_page="20" columns="4" orderby="date" order="desc"]' );
    };

    Probably comes as no surprise to anyone reading this thread that I'm no expert with PHP, so corrections are appreciated.

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic