WordPress.org

Ready to get started?Download WordPress

Forums

WooCommerce - excelling eCommerce
Different page layouts for category vs subcategory? (7 posts)

  1. amberturner
    Member
    Posted 1 year ago #

    Hello!

    I would like to use one page layout to show all of the subcategories to a category, then a different layout for the subcategory that will show all the products.

    Or said another way:

    I have a main category of "wedding."
    I have a subcategory of wedding called "modern."

    I would like the main category "wedding" (or any other main category) to have one page layout, then when selecting "modern" (or any other subcategory) it will have a slightly different layout.

    My test link (note that things are changing RAPIDLY on this site): http://test.januarycreative.com/invitation

    I would like the "wedding invitation" main category to have a layout like it is now, and the "modern" subcategory have a description on the right side of that large image.

    Background: I am using modified Woocommerce theme files that I have in my custom WordPress theme. These files are in mytheme/woocomerce/

    Currently, the archive-product page is dictating how the category page will look and the subcategory page will look.

    Is there any way to be able to have two different page layouts? Ideally, if I can tell Woocommerce that "if this is a main category page, do this, else, do this."

    Can anyone help me do this?

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

  2. imevilduckie
    Member
    Posted 1 year ago #

    After much trial and error, I was able to figure this out. When you're browsing a category, the main template file being called is taxonomy-product_cat.php which pulls archive-product.php. Basically, you add conditions to taxonomy-product_cat.php to redirect to a different template based on which level of category you're on. The below example goes three levels deep, but you can customize based on your needs.

    // We need to get the top-level category so we know which template to load.
    $get_cat = $wp_query->query['product_cat'];
    
    // Split
    $all_the_cats = explode('/', $get_cat);
    
    // How many categories are there?
    $cat_count = count($all_the_cats);
    
    //
    // All the cats say meow!
    //
    
    // Define the parent
    $parent_cat = $all_the_cats[0];
    
    // In-house classes
    if ( $parent_cat == 'classes' ) woocommerce_get_template( 'archive-product.php' );
    
    // Online courses (videos) -- "top-level" categories
    elseif ( $parent_cat == 'online-courses' && $cat_count == 2 ) woocommerce_get_template( 'archive-online-courses-top.php' );
    
    // Online courses (videos) -- sub-categories
    elseif ( $parent_cat == 'online-courses' && $cat_count > 2 ) woocommerce_get_template( 'archive-online-courses-sub.php' );
  3. amberturner
    Member
    Posted 1 year ago #

    Thanks so much for trying this out for me! Although, I appear to be having a couple of issues.

    I removed this from my taxonomy-product_cat.php:

    woocommerce_get_template( 'archive-product.php' );

    And added this (per your code):

    // We need to get the top-level category so we know which template to load.
    $get_cat = $wp_query->query['product_cat'];
    
    // Split
    $all_the_cats = explode('/', $get_cat);
    
    // How many categories are there?
    $cat_count = count($all_the_cats);
    
    //
    // All the cats say meow!
    //
    
    // Define the parent
    $parent_cat = $all_the_cats[0];
    
    // Main Wedding Invitations
    if ( $parent_cat == 'wedding-collections' ) woocommerce_get_template( 'archive-product.php' );
    
    // Collection Layout
    //elseif ( $parent_cat == 'wedding-collections' && $cat_count == 2 ) woocommerce_get_template( 'subcat-archive-product.php' );

    You can see all the categories I am working with here:

    Link to picture of categories

    I am wanting the very top product category "Wedding Collections" to have archive-product.php, then the subcategories of "Wedding Collections" (the only one right now being "Modern Collection") to have subcat-archive-product.php.

    I believe I missed something. I tried your code every way I could and couldn't get it to work pass the "Collection Layout" part above. It still takes the layout of archive-product.php even though I click on "Modern Collection." When clicking on "Modern Collection" right now it should only read: TEST SUBCAT ARCHIVE PRODUCT with only the header and footer showing. What did I blow up?

    Test site: http://test.januarycreative.com/invitation/ (Click on Wedding Invitations to see Wedding Collections category, click "Modern Collection" to see Modern Collection products)

    (Also, I have checked, subcat-archive-product.php and archive-product.php are in the same location with taxonomy-product_cat.php, so there is no worry about it not finding subcat-archive-product because it can find archive-product.php)

    Thanks again for your help!!!

  4. bunkerprojectsudio
    Member
    Posted 1 year ago #

    I need to get to the solution step by step for different template of products for specific category

    I need your help :)

    thank you very much

  5. k.devantoni
    Member
    Posted 1 year ago #

    Really Fantastic conversation !
    Now I just wanna know something to display different layout for product (single product page) under the these categories,....
    hmm k let me define,

    if category A display AA template for subcategories also the single product layout in AA , similarly
    if category B display BB template for subcategories also the single product layout in BB
    where I get all layout correctly except the "Single-Product.php"

    Hi, imevilduckie can you please suggest me the technique ? thx in advance ;-)

  6. k.devantoni
    Member
    Posted 1 year ago #

    Okie got it fixed
    http://wordpress.stackexchange.com/questions/3507/custom-field-values-in-permalink/3517#3517

    <?php
    global $post;
    $terms = wp_get_post_terms( $post->ID, 'product_cat' );
    foreach ( $terms as $term ) $categories[] = $term->slug;
    
    if ( in_array( 'audio', $categories ) ) {
      echo 'In audio';
      woocommerce_get_template_part( 'content', 'single-product' );
    } elseif ( in_array( 'elektro', $categories ) ) {
      echo 'In elektro';
      woocommerce_get_template_part( 'content', 'single-product' );
    } else {
      echo 'some blabla';
    } ?>
  7. daledude
    Member
    Posted 12 months ago #

    k.devantoni - I'm not able to get the code you worked to share content between the categories and the single product pages. How were you able to get it to work? Did you just use the code you posted, or did you combine it with imevilduckie's code?

    Basically, I have a footer that I only want to appear on a parent category, ALL of its subcategories, and the single products within those categories. All of them will share this same footer. I can use imevilduckie's code to make it appear in my footer.php on all of the main category and subcategories pages, but it won't show on the single product page.

    Thanks for any assistance!

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic