• Resolved David

    (@topsquirrel)


    In the link, you’ll see there are just two subcategories displayed but in the sidebar you’ll see a list of the 13 subcategories that exist.

    I have a feeling that the problem might be image size related. This is a development site where I’m rewriting an old Joomla site and many category images I’m inheriting are small eg. 125px width; some are 1:1, some are 4:3 etc; so its a mixed bag of images!

    I’ve set Product Image settings in the Customizer as:-
    Main image width: 300
    Thumbnail width: 150
    Thumbnail cropping: 1:1

    Thanks

    David

    The page I need help with: [log in to see the link]

Viewing 8 replies - 1 through 8 (of 8 total)
  • Thread Starter David

    (@topsquirrel)

    Just to add…

    The shop display is based on the display categories shortcode:-
    [product_categories number="0" parent="0" hide_empty="0"]

    Plugin Support Fernando a11n

    (@fhaps)

    Automattic Happiness Engineer

    The first thing you’ll want to verify is that all the categories have products assigned to them. Any empty categories (or subcategories) will be hidden from the display.

    If you’ve verified that, then I’d suggest you try completing a conflict test. To rule out the possibility of a theme or plugin being the cause of this, you’ll want to temporarily switch your theme to the default Storefront theme, and disable all of your plugins except for WooCommerce.

    If the categories are shown under that setup, then you’ll know that the issue was being caused by one of the features you just disabled.

    Hopefully that helps!

    Thread Starter David

    (@topsquirrel)

    Thanks for your help Fernando, much appreciated.

    I had already tried deactivating the plugins which didn’t work with Poseidon theme.

    On your advice, I tried installing Storefront and deactivating the plugins again – unfortunately that still didn’t work either.

    I’ve also tried uploading a larger category image to one of the categories that isn’t displaying but this didn’t fix it.

    Do you know where the loop is to retrieve subcategories? I’m thinking I could try debugging there perhaps but it looks quite complex.

    Thread Starter David

    (@topsquirrel)

    p.s. I’ve now decided to overwrite the archive page handling

    Plugin Support Fernando a11n

    (@fhaps)

    Automattic Happiness Engineer

    @topsquirrel The categories that are missing, do they have products assigned to them? Not assigned to subcategories under that category, but directly assigned to this category/subcategory.

    I’d test creating a new product manually and adding it to a category/subcategory that is currently missing, to see if that makes any difference.

    Thread Starter David

    (@topsquirrel)

    Hi Fernando,

    Thanks for coming back again.

    One of the two sub-categories that was displaying (out of 13) had products assigned, the other had products assigned to its sub-categories. It is a puzzler what causes the problem; I wondered about database collation as the data has originally come from Joomla 1.5 in a roundabout route.

    Thank you for your help. I was completely stuck! I have now created a workaround which displays all subcategories (and the header category image). I have done this through a Plugin – see below.

    <?php
    /**
     * Plugin Name: Squirrelhouse - handle WooCommerce (sub)categories in archive pages
     * Description: Display products and categories / subcategories as two separate lists in product archive pages
     * Version: 1.0
     * Author: Squirrelhouse
     * Author URI: https://media.squirrelhouse.biz
     */
     
    /**
     * Display all sub categories based on 3 column display
     * Based on https://code.tutsplus.com/tutorials/display-woocommerce-categories-subcategories-and-products-in-separate-lists--cms-25479
     */
    function squirrel_product_subcategories( $args = array() ) {
        $parentid = get_queried_object_id();
        
        $counter = 1; //counter tracks the number of the post we're on 
             
        $args = array(
            'parent' => $parentid
        );
     
        $terms = get_terms( 'product_cat', $args );
     
        if ( $terms ) {
             
            echo '<hr><h2 class="squirrel-cat-section">Sub-Categories</h1>';
            echo '<ul class="products columns-3">';
         
                foreach ( $terms as $term ) {
                    
                    // add last or first classes          
                    ?>
                    <?php if ($counter % 3 == 0) { echo '<li class="product-category product squirrel-cat last">';} 
                      elseif ($counter % 3 == 1) { echo '<li class="product-category product squirrel-cat first">';} 
                      else                       { echo '<li class="product-category product squirrel-cat">';} 
                      ?> 
                    <?php
                             
                    // echo '<li class="product-category product squirrel-cat ">';                 
                         
                        echo '<a href="' .  esc_url( get_term_link( $term ) ) . '" class="' . $term->slug . '" squirrel-cat-a>';
                        
                        woocommerce_subcategory_thumbnail( $term );
                        
                        echo '<h2 class="woocommerce-loop-category__title squirrel-cat-h2">';
                        echo $term->name;
                        echo '</h2>';
                        echo '</a>';
                                                                         
                echo '</li>';
                
                $counter++; // This increases the value of $counter by 1 for every loop iteration 
                                                                         
            }
         
            echo '</ul>';
            echo '<hr><h2 class="squirrel-cat-section">Products</h1>';
    
     
        }
    }
    
    add_action( 'woocommerce_before_shop_loop', 'squirrel_product_subcategories', 50 );
    
    /**
     * Display category image on category archive
     * Based on https://stackoverflow.com/questions/12717112/how-to-display-woocommerce-category-image
     */
    function woocommerce_category_image() {
        if ( is_product_category() ){
    	    global $wp_query;
    	    $cat = $wp_query->get_queried_object();
    	    $thumbnail_id = get_woocommerce_term_meta( $cat->term_id, 'thumbnail_id', true );
    	    $image = wp_get_attachment_url( $thumbnail_id );
    	    if ( $image ) {
    		    echo '<img src="' . $image . '" alt="' . $cat->name . '" />';
    		}
    	}
    }
    
    add_action( 'woocommerce_archive_description', 'woocommerce_category_image', 2 );
    
    ?>
    Plugin Support Fernando a11n

    (@fhaps)

    Automattic Happiness Engineer

    @topsquirrel I’m glad to hear you were able to find a workaround!

    I do wonder if this was caused by the way your products were originally created. I didn’t realize you had used an import tool to create this. When we have situations like this, a good first step is verifying whether manually creating a new category with a new product reproduces the error.

    Thankfully you are past this issue now!

    Thread Starter David

    (@topsquirrel)

    Thanks for your thoughts Fernando.

    Yes, I used cart2cart to migrate the data from Joomla 1.5 to WordPress 5.0 which appears to have worked well otherwise.

    As my workaround is a plugin it was easy to deactivate and then test with a category created manually. The home page shop display is using this shortcode [product_categories number=”0″ parent=”0″ hide_empty=”0″ columns=”3″]. As hide_empty is false, I was expecting to see the new category but it didn’t display the category until I added a product to it.

    I will leave the plugin activated now as that provides a solution and the site goes live tomorrow! ??

    Many thanks again for your help!

    p.s. If anyone seeing this post wants to try the plugin, you need to change Woocommerce Product Display settings in the Customizer to have:-
    Shop Page Display = Products
    Category Display = Products

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘Some subcategories are missing on category page’ is closed to new replies.