Support » Plugin: WooCommerce » WC 3.3.1 New issues with product category display

  • Resolved Steve Weigold

    (@sweigold)


    Updated versions of WooCommerce and Storefront. Problem I’m having after update is SOME, not all categories are not displaying as expected. The linked page above is one example. My categories are set to display both subcategories and products, but I have a modification in my child theme because I only want the products displayed for the category being view and not the products of the subcategories. The code for this is below.

    This has worked on all categories up until the 3.3.1 update I believe. Now, it works for many still, but not all. Generally, I find that now in a product category that doesn’t have any products, only subcategories, I get the “no products found” message.

    Things I’ve tried:
    – No caching plugins in use
    – Clearing browser caches, WooCommerce counters etc has no effect
    – On the errant pages, no changes to display settings has any effect
    – If I add a test product to the errant category, the category and subcategories display as expected.
    – If I remove my exclude code below, most pages display with products, subcategories and subcategory products as I would expect, BUT, the errant categories display subcategories, but NOT subcategory products, so are still displaying incorrectly based on settings.

    Help please!
    Steve

    Exclude code:

    
    function exclude_product_cat_children($wp_query) {
    	if ( isset ( $wp_query->query_vars['product_cat'] ) && $wp_query->is_main_query()) {
    		$wp_query->set('tax_query', array(
    				array (
    						'taxonomy' => 'product_cat',
    						'field' => 'slug',
    						'terms' => $wp_query->query_vars['product_cat'],
    						'include_children' => false
    				)
    		)
    		);
    	}
    }
    add_filter('pre_get_posts', 'exclude_product_cat_children');
    
    
    ### WordPress Environment ###
    
    Home URL: http://sbw.weigoldenterprises.com
    Site URL: http://sbw.weigoldenterprises.com
    WC Version: 3.3.1
    Log Directory Writable: ✔
    WP Version: 4.9.4
    WP Multisite: –
    WP Memory Limit: 256 MB
    WP Debug Mode: ✔
    WP Cron: ✔
    Language: en_US
    
    ### Server Environment ###
    
    Server Info: Apache/2.4.10 (Debian)
    PHP Version: 5.6.33-0+deb8u1
    PHP Post Max Size: 8 MB
    PHP Time Limit: 30
    PHP Max Input Vars: 2000
    cURL Version: 7.38.0
    OpenSSL/1.0.1t
    
    SUHOSIN Installed: –
    MySQL Version: ❌ 5.5.49 - We recommend a minimum MySQL version of 5.6. See: WordPress requirements
    Max Upload Size: 8 MB
    Default Timezone is UTC: ✔
    fsockopen/cURL: ✔
    SoapClient: ✔
    DOMDocument: ✔
    GZip: ✔
    Multibyte String: ✔
    Remote Post: ✔
    Remote Get: ✔
    
    ### Database ###
    
    WC Database Version: 3.3.1
    WC Database Prefix: wp_
    MaxMind GeoIP Database: ❌ The MaxMind GeoIP Database does not exist - Geolocation will not function. You can download and install it manually from http://dev.maxmind.com/geoip/legacy/geolite/ to the path: . Scroll down to "Downloads" and download the "Binary / gzip" file next to "GeoLite Country". Please remember to uncompress GeoIP.dat.gz and upload the GeoIP.dat file only.
    Total Database Size: 13.48MB
    Database Data Size: 8.51MB
    Database Index Size: 4.97MB
    wp_woocommerce_sessions: Data: 0.05MB + Index: 0.02MB
    wp_woocommerce_api_keys: Data: 0.02MB + Index: 0.03MB
    wp_woocommerce_attribute_taxonomies: Data: 0.02MB + Index: 0.02MB
    wp_woocommerce_downloadable_product_permissions: Data: 0.02MB + Index: 0.05MB
    wp_woocommerce_order_items: Data: 0.02MB + Index: 0.02MB
    wp_woocommerce_order_itemmeta: Data: 0.02MB + Index: 0.03MB
    wp_woocommerce_tax_rates: Data: 0.02MB + Index: 0.06MB
    wp_woocommerce_tax_rate_locations: Data: 0.02MB + Index: 0.03MB
    wp_woocommerce_shipping_zones: Data: 0.02MB + Index: 0.00MB
    wp_woocommerce_shipping_zone_locations: Data: 0.02MB + Index: 0.03MB
    wp_woocommerce_shipping_zone_methods: Data: 0.02MB + Index: 0.00MB
    wp_woocommerce_payment_tokens: Data: 0.02MB + Index: 0.02MB
    wp_woocommerce_payment_tokenmeta: Data: 0.02MB + Index: 0.03MB
    wp_woocommerce_log: Data: 0.02MB + Index: 0.02MB
    wp_commentmeta: Data: 0.02MB + Index: 0.03MB
    wp_comments: Data: 0.02MB + Index: 0.09MB
    wp_links: Data: 0.02MB + Index: 0.02MB
    wp_mgmlp_folders: Data: 0.00MB + Index: 0.01MB
    wp_options: Data: 3.02MB + Index: 0.11MB
    wp_postmeta: Data: 3.06MB + Index: 3.52MB
    wp_posts: Data: 1.52MB + Index: 0.31MB
    wp_rg_form: Data: 0.02MB + Index: 0.00MB
    wp_rg_form_meta: Data: 0.08MB + Index: 0.00MB
    wp_rg_form_view: Data: 0.02MB + Index: 0.03MB
    wp_rg_incomplete_submissions: Data: 0.02MB + Index: 0.02MB
    wp_rg_lead: Data: 0.02MB + Index: 0.03MB
    wp_rg_lead_detail: Data: 0.02MB + Index: 0.06MB
    wp_rg_lead_detail_long: Data: 0.02MB + Index: 0.00MB
    wp_rg_lead_meta: Data: 0.02MB + Index: 0.05MB
    wp_rg_lead_notes: Data: 0.02MB + Index: 0.03MB
    wp_sentry_groups: Data: 0.02MB + Index: 0.00MB
    wp_termmeta: Data: 0.05MB + Index: 0.03MB
    wp_terms: Data: 0.02MB + Index: 0.03MB
    wp_term_relationships: Data: 0.11MB + Index: 0.08MB
    wp_term_taxonomy: Data: 0.02MB + Index: 0.03MB
    wp_usermeta: Data: 0.02MB + Index: 0.03MB
    wp_users: Data: 0.02MB + Index: 0.05MB
    wp_wc_download_log: Data: 0.02MB + Index: 0.03MB
    wp_wc_webhooks: Data: 0.02MB + Index: 0.02MB
    
    ### Post Type Counts ###
    
    acf-field: 7
    acf-field-group: 3
    attachment: 236
    content_block: 5
    customize_changeset: 8
    mgmlp_media_folder: 71
    nav_menu_item: 46
    page: 13
    post: 3
    product: 762
    product_variation: 252
    revision: 46
    shop_order: 1
    ultimate_slider: 5
    wccpf: 1
    
    ### Security ###
    
    Secure connection (HTTPS): ❌Your store is not using HTTPS. Learn more about HTTPS and SSL Certificates.
    Hide errors from visitors: ❌Error messages should not be shown to visitors.
    
    ### Active Plugins (11) ###
    
    Gravity Forms: by rocketgenius – 2.2.5
    Advanced Custom Fields PRO: by Elliot Condon – 5.6.8
    Content Blocks (Custom Post Widget): by Johan van der Wijk – 3.0.3
    WordPress Media Library Folders: by Max Foundry – 4.1.1
    Slider Ultimate: by Etoile Web Design – 1.0.21 – Not tested with the active version of WooCommerce
    Woo Product Images Slider: by DesirePress – 1.3.0 – Not tested with the active version of WooCommerce
    WooCommerce Authorize.Net AIM Gateway: by SkyVerge – 3.12.2 – 3.13.0 is available – Not tested with the active version of WooCommerce
    WooCommerce PayPal Powered by Braintree Gateway: by WooCommerce – 2.1.1
    WooCommerce - Gravity Forms Product Add-Ons: by Lucas Stark – 3.2.5 – 3.2.9 is available – Not tested with the active version of WooCommerce
    WooCommerce - Store Toolkit: by Visser Labs – 1.7.9
    WooCommerce: by Automattic – 3.3.1
    
    ### Settings ###
    
    API Enabled: ✔
    Force SSL: –
    Currency: USD ($)
    Currency Position: left
    Thousand Separator: ,
    Decimal Separator: .
    Number of Decimals: 2
    Taxonomies: Product Types: external (external)
    grouped (grouped)
    simple (simple)
    variable (variable)
    
    Taxonomies: Product Visibility: exclude-from-catalog (exclude-from-catalog)
    exclude-from-search (exclude-from-search)
    featured (featured)
    outofstock (outofstock)
    rated-1 (rated-1)
    rated-2 (rated-2)
    rated-3 (rated-3)
    rated-4 (rated-4)
    rated-5 (rated-5)
    
    ### WC Pages ###
    
    Shop base: #57 - /?page_id=57
    Cart: #7 - /?page_id=7
    Checkout: #2436 - /?page_id=2436
    My account: ❌ Page ID is set
    but the page does not exist
    
    Terms and conditions: #2438 - /?page_id=2438
    
    ### Theme ###
    
    Name: Storefront Child - SBW Specific
    Version: 1.0.0
    Author URL: http://www.weigoldenterprises.com
    Child Theme: ✔
    Parent Theme Name: Storefront
    Parent Theme Version: 2.2.7
    Parent Theme Author URL: https://woocommerce.com/
    WooCommerce Support: ✔
    
    ### Templates ###
    
    Overrides: –
    
    ### Templates ###
    
    thumbnail: 
    medium: 
    medium_large: 
    large: 
    woocommerce_thumbnail: 
    woocommerce_single: 
    woocommerce_thumbnail_2x: 
    shop_thumbnail: 
    shop_catalog: 
    shop_single: 
    
    ### Authorize.Net AIM ###
    
    Environment: Test
    Debug Mode: Save to Log
    
    ### Braintree (PayPal) ###
    
    Environment: 
    Tokenization Enabled: –
    Debug Mode: Off
    

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

Viewing 13 replies - 16 through 28 (of 28 total)
  • Here is my working code. Had to add a count check in the conditional

    function modify_category_products($query)
        {
            if($query->get('product_cat') && !is_admin() && $query->queried_object->count > 0)
            {
                $tax_query = $query->get('tax_query');
    
                $cat = get_term_by('slug', $query->get('product_cat'), 'product_cat');
                $subcats = get_terms( 'product_cat', array(
                    'parent' => $cat->term_id
                ) );
    
                $child_cats = array();
                foreach($subcats as $cat)
                {
                    array_push($child_cats, $cat->term_id);
                }
    
                $new_tax_query = array(
                    array(
                        'taxonomy' => 'product_cat',
                        'field' => 'id',
                        'terms' => $child_cats,
                        'operator'=> 'NOT IN'
                    )
                );
    
                if(!$tax_query)
                    $tax_query = array();
    
                array_push($tax_query, $new_tax_query);
    
                $query->set('tax_query', $new_tax_query);
            }
    }
    add_filter( 'pre_get_posts', 'modify_category_products' );

    @mikejolley This is necessary because my Utility category is set to Both. But when that happens it displays:
    – the subcategory(ies)
    – the direct child products
    – and the subcategories’ products (which we do not want). it should only be displaying the subcategories and direct child products.

    Plugin Author Mike Jolley

    (@mikejolley)

    I’m not talking about the global setting. There is a setting per category you can edit. This has been in place for years.

    Bypassing WC and editing core queries will have side effects. In this case, a category with no children won’t be visible. Your query changes the product counts.

    Use the setting – I’m pretty confident it does what you’re after. No code required.

    So, here’s the link to my test site, with the query adjustment in place:
    http://sbw.weigoldenterprises.com/test/?product_cat=indoor-lighted

    You can see the desired behavior in “Indoor Lighted” where we show products in that category and subcategories, but not the products in the subcategories.

    “Outdoor Lighted” is the category that ONLY has subcategories and of course, it’s broken now.

    WooCommerce doesn’t have the capability of doing this type of configuration natively. Further, this approach worked properly up until a recent update, so something changed that broke this approach. I need to come up with a solution. If modifying the query isn’t it, I’d appreciate some help on what is.

    @mikejolley, the setting does not. I have changed the test site again and edited the setting for the category itself. Indoor Lighted in my test site now only shows subcategories. The product that is in Indoor Lighted is now no longer visible.

    http://sbw.weigoldenterprises.com/test/?product_cat=indoor-lighted

    Here’s the link to the product in the Indoor Lighted category which is no longer visible:

    http://sbw.weigoldenterprises.com/test/?product=ils-test-product-1

    • This reply was modified 1 year, 11 months ago by Steve Weigold.

    I think there is a disconnect in desired behavior.

    I AM going into a specific category’s settings to change the display.
    If I change it to Both (for my Utility category for example):
    – that category’s subcategories display as they should
    – that category’s direct child products display as they should
    – any subcategory’s products are also getting displayed (which is undesired for our site)
    – This has been the case before 3.3.1. Regardless if a category is set to display Products or Both it displays all products regardless of it being a direct descendent of the category or in a subcategory.
    – This default behavior of WC is not desired for our website, therefore I wrote the code that I did – to only display direct descendent products of the current category and remove those in subcategories.
    – With the 3.3.1 update, the pre_get_posts was affected but I fixed by adding a count check to the conditional as mentioned.

    @sweigold It sounds like you might be trying to implement the same effect? If so, have you tried adding my working snippet to your site?

    @pinksharpii I am trying to do exactly that. I overlooked your working code until a few minutes ago and so just now added it to mine. Thank you so much! You saved me from having to figure out that myself 🙂

    @mikejolley, @pinksharpii’s explanation in his last post is spot on. Maybe this needs to be a feature request?

    I’m going to mark this issue as resolved. Thanks everyone.

    Plugin Author Mike Jolley

    (@mikejolley)

    Ok I can see why it happens now and why the count based snippet would fix.

    To work in shortcodes and via templates, 3.3 prepends the products loop with subcategories. By default, there will always be products if there are categories.

    This snippet removes those products so have_posts returns false. There is therefore no loop, and no categories get prepended. The ‘no products exist’ notice is rendered instead.

    Making a note, thanks.

    I am having the same issue, I was using the same exlude code that Sweigold posted. It used to work fine, but after the new woocommerce updates(like 2 weeks ago) it stopped working. Any solutions?

    Thanks in advance

    Sadly we also encounter this problem, especially since the latest woocommerce 3.3.3.
    Where also the script from @pinksharpii is not working and is still resulting in no products found.

    We have several shops were we would like to see this behaviour as described in this topic which we included since longtime with a similar script as in the post starter topic from @sweigold

    I hope anyone has a clue how to solve this since we have several shops that use this functionality to not display products from subcategories in the main category but only these products that are connected to the current group. And i think it would be a great feature request since the inital code can be found on many sites as a solution to this question, it looks like many people use this behaviour.

    I am having the exact same issue on a few sites.

    Has anyone had any luck in re-writting the snippet?

    @wordmonkey25 I have been looking around for some time now, but i couldn’t find a solution 🙁

Viewing 13 replies - 16 through 28 (of 28 total)
  • The topic ‘WC 3.3.1 New issues with product category display’ is closed to new replies.