WooCommerce: Hide Hidden Products From Cart, Order, Emails

While working on a workaround for a client, I had to hide a hidden product from the cart, checkout, order received and emails. Hidden products already don’t show on the shop and category pages, but they do show on the cart if they’re added to cart “programmatically”.

A tough task, but as usual here I come with the solution πŸ™‚

First, make your WooCommerce product hidden!
The wanted result: hide Hidden Products from the Cart (and also Checkout, Order Received and Emails!)

PHP Snippet: Hide Hidden Products from WooCommerce Cart, Checkout, Order, Emails

/**
 * @snippet       Hide Hidden Products from Cart, Checkout, Order - WooCommerce
 * @how-to        businessbloomer.com/woocommerce-customization
 * @author        Rodolfo Melogli, Business Bloomer
 * @testedwith    WooCommerce 4.1
 * @community     https://businessbloomer.com/club/
 */
  
add_filter( 'woocommerce_cart_item_visible', 'bbloomer_hide_hidden_product_from_cart' , 10, 3 );
add_filter( 'woocommerce_widget_cart_item_visible', 'bbloomer_hide_hidden_product_from_cart', 10, 3 );
add_filter( 'woocommerce_checkout_cart_item_visible', 'bbloomer_hide_hidden_product_from_cart', 10, 3 );
add_filter( 'woocommerce_order_item_visible', 'bbloomer_hide_hidden_product_from_order_woo333', 10, 2 );
   
function bbloomer_hide_hidden_product_from_cart( $visible, $cart_item, $cart_item_key ) {
    $product = apply_filters( 'woocommerce_cart_item_product', $cart_item['data'], $cart_item, $cart_item_key );
    if ( $product->get_catalog_visibility() == 'hidden' ) {
        $visible = false;
    }
    return $visible;
}
   
function bbloomer_hide_hidden_product_from_order_woo333( $visible, $order_item ) {
    $product = $order_item->get_product();
    if ( $product->get_catalog_visibility() == 'hidden' ) {
        $visible = false;
    }
    return $visible;
}

Where to add custom code?

You should place custom PHP in functions.php and custom CSS in style.css of your child theme: where to place WooCommerce customization?

This code still works, unless you report otherwise. To exclude conflicts, temporarily switch to the Storefront theme, disable all plugins except WooCommerce, and test the snippet again: WooCommerce troubleshooting 101

Related content

Rodolfo Melogli

Business Bloomer Founder

Author, WooCommerce expert and WordCamp speaker, Rodolfo has worked as an independent WooCommerce freelancer since 2011. His goal is to help entrepreneurs and developers overcome their WooCommerce nightmares. Rodolfo loves travelling, chasing tennis & soccer balls and, of course, wood fired oven pizza. Follow @rmelogli

40 thoughts on “WooCommerce: Hide Hidden Products From Cart, Order, Emails

  1. Hello,

    This code breaks my site.

    I don’t have error code.

    thanks for your help !
    kevin

    1. Works for me, must be something else

  2. Hello Rodolfo!

    I’ve been using your snippet for quite some time now, thank you very much for that. πŸ™‚

    It seems that the latest Woocommerce update [5.5.1 ] broke this snippet, since the ‘hidden’ products are now visible in both cart & checkout.

    Do you know what causes this sudden change?

    Kind Regards,

    Jeroen

    1. I don’t think there were major changes in WooCommerce in regard to this. If you revert Woo to a previous version it works? Sure it’s not another plugin or your active theme?

  3. Hello,

    The code breaks my site. It does hide the products in my cart but it breaks the product page. Only one product becomes visible and the layout breaks.

    Any ideas how to fix the issue?

    Thanks

    1. Hi Brooke, which PHP error di you get?

  4. Hello

    great tip.

    whether the product is a downloadable product. can the client still have the linked files to download in the “my downloads” area?

    thank you

    1. I haven’t tested that case scenario, so let us know

  5. We are randomly seeing the following error regarding this snippet:

    PHP Fatal error: Uncaught Error: Call to a member function get_catalog_visibility() on bool in /home/hpadmin/public_html/wp-content/themes/aurum-child/functions.php:1474

    We are currently running WP 5.4.2 and WooCommerce 4.2.0

    Stack trace:
    #0 /home/hpadmin/public_html/wp-includes/class-wp-hook.php(287): bbloomer_hide_hidden_product_from_order_woo333(true, Object(WC_Order_Item_Product))
    #1 /home/hpadmin/public_html/wp-includes/plugin.php(206): WP_Hook->apply_filters(true, Array)
    #2 /home/hpadmin/public_html/wp-content/themes/aurum/woocommerce/order/order-details-item.php(23): apply_filters(‘woocommerce_ord…’, true, Object(WC_Order_Item_Product))
    #3 /home/hpadmin/public_html/wp-content/plugins/woocommerce/includes/wc-core-functions.php(251): include(‘/home/hpadmin/p…’)
    #4 /home/hpadmin/public_html/wp-content/themes/aurum/woocommerce/order/order-details.php(63): wc_get_template(‘order/order-det…’, Array)
    #5 /home/hpadmin/public_html/wp-content/plugins/woocommerce/includes/wc-core-functions.php(251): include(‘/home/hpadmin/p…’)
    #6 /home/hpadmin/public_html/wp-content/plugins/wo in /home/hpadmin/public_html/wp-content/themes/aurum-child/functions.php on line 1474

    Any insights or thoughts on what would cause this?

    V/r,
    Charles

    1. Weird. Try adding this check:

      if ( $product && $product->get_catalog_visibility() ....
      
  6. June 2nd, 2020 – Code is still working. However, the hidden product is still visable in the email that is send to the client (woocommerce/templates/emails/customer-processing-order.php). How can I fix this?

    1. Hi Wim, just tested on latest Woo, works perfectly, emails included. Must be something else

  7. Can i remove the hidden product from the admin order edit page?

    1. Varun, thanks so much for your comment! Yes, this is definitely possible, but I’m afraid it’s custom work. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding!

  8. I’ve added your snippet. Seems to work except for emails… They’re still being sent out after I programmatically do the transaction.
    How can I disable emails from going to customer?

    1. Hi Andy, this hides products from emails, but does not disable emails though

  9. This code still works. But I would like to also remove so it’s not counted in the items in cart at the top. Maby its just my theme that shows the number of items. Is there any solution for that. The product is a membership that should be seen that they get. It’s for a lifetime discount.

    Cheers

  10. Not working 3/1/2020. Hidden product is hidden as is entire cart content.

    Please advise. Latest wordpress release

    1. Hi, thanks for your comment! I just tested this again with Storefront theme and it works perfectly. Maybe your theme (or another plugin) is messing/conflicting with my snippet?

      To troubleshoot, disable all plugins but WooCommerce and also switch temporarily to “Twentyseventeen” theme (load the snippet there in functions.php) – does it work? If yes, you have a problem with your current theme or one of the plugins.

      Hope this helps!

      R

  11. Hi, thanks a lot for this code!
    However, the mini cart on top right seems not to update correctly in my case.
    Should it work for the mini cart as well?
    I dont talk about lowering the item count, just hiding the items in mini cart.
    Meena

    1. Hello Meena, correct, this snippet does not work with the mini cart. You need to add another hook trigger for hook = “woocommerce_widget_cart_item_visible” for that. Hope this helps

  12. Nice code, but i dont want to hide any product since am using [product_page id ="8080"] to display specific product on the front end, however i want to allow users to purchase this product but i dont want it listed in the orders section in my account orders page, i want only to hide some specific ordered Products from showing on the my orders page.

    1. hi Chris, thanks so much for your comment! Yes, this is definitely possible, but I’m afraid it’s custom work. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding!

  13. This works great! Thank you so much.

    I need one thing to make this complete. Currently, hidden products show up in the menu cart number indicator.

    Would you please help me hide hidden products from this number indicator? I need this number indicator for my store.

    Thank you!

  14. Just an FYI – Be aware of this code!
    We added it to our site and it caused all new orders to duplicate 4-5 times and customers got charged for every duplicated order. A nightmare!
    We love this site and all the snippets are great and never had an issue until now.
    It could just be a conflict on our end, but just be ware.

    Cheers

    1. Thanks Miguel πŸ™‚ This snippet does not duplicate orders, so yes this is definitely another sort of conflict. Always test snippets on a development/clone website and never on a live environment. Thank you!

  15. Hi,

    In th latest version of woocommerce, there seems to be an issue with the following code:

    function bbloomer_hide_hidden_product_from_order( $visible, $order_item ) {
    if ( $order_item->get_catalog_visibility() == 'hidden' ) {
            $visible = false;
        }
        return $visible;
    }
    

    The order doesn’t go through to the success/thank you page, email notification is not sent to the customer/admin. Does the code hide the HIDDEN products on order email as well?

    Unable to verify as the code is not triggering the successful order email.

    1. You’re absolutely right Vinoth πŸ™‚ Snippet has now been updated!

  16. Hi Rodolfo

    I thank you for this HOOK, which has been very useful for me.

    Now, how could I hide just the prices of the order?

    Saludos!

    “Yeorsh”

    1. Hola Yeorsh, thanks so much for your comment! Yes, this is possible – but unfortunately this is custom work and I cannot provide a complementary solution here via the blog comments. Thanks a lot for your understanding! ~R

  17. This is not working on WooCommerce 3.0.7, any ideas on what needs to be updated for it to work?

    I’ve written some custom jQuery to remove or hide the items in Cart and on Order Received screens on the front-end, but of course this doesn’t stop the hidden product from showing up on customer emails and elsewhere as needed.

    Thanks,
    Ethan

    1. Ethan, thanks so much for your comment! And yes, you’re right πŸ™‚ Snippet has now been updated – test it out!

  18. I have added this code to my child theme functions.php file but hidden products still show in cart and emails. Is this code still valid for current wordpress and woo commerce versions. Thank you

    1. Hey Rory, thanks for your comment! Yes, it should definitely work πŸ™‚ Can you send me a screenshot of your hidden product “edit page” and also cart and email? Thank you!

      1. Thanks for the quick reply. I did figure out a mistake I had made which blocked this from working. Now resolved. Thank you so much for making this tutorial and supporting your page.

        I do have another question. I noticed when I generate invoices the hidden products show up again only on the invoice. Should this code be blocking that as well? Do you know of additional code to also hide from invoice?

        1. Excellent Rory πŸ™‚ Do you use a particular invoice plugin, or are you referring to the default WooCommerce invoice?

          1. I have been using booster.io for its pdf feature and the pdf invoice is what includes the hidden items.

            1. Hey Rory, thanks so much for your comment! Yes, this is possible – but unfortunately this is custom work and I cannot provide a complementary solution here on the blog. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding! ~R

Questions? Feedback? Customization? Leave your comment now!
_____

If you are writing code, please wrap it like so: [php]code_here[/php]. Failure to complying with this, as well as going off topic or not using the English language will result in comment disapproval. You should expect a reply in about 2 weeks - this is a popular blog but I need to get paid work done first. Please consider joining the Business Bloomer Club to get quick WooCommerce support. Thank you!

Your email address will not be published. Required fields are marked *