Posted on

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!)

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


/**
 * @snippet       Hide Hidden Products from Cart, Checkout, Order - WooCommerce
 * @how-to        Watch tutorial @ https://businessbloomer.com/?p=19055
 * @sourcecode    https://businessbloomer.com/?p=21936
 * @author        Rodolfo Melogli
 * @testedwith    WooCommerce 3.4.5
 */
 
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 this code?

You can place PHP snippets at the bottom of your child theme functions.php file (before "?>" if you have it). CSS, on the other hand, goes in your child theme style.css file. Make sure you know what you are doing when editing such delicate files - if you need more guidance, please take a look at my free WooCommerce Customization video tutorial.

Does this snippet still work?

Please let me know in the comments if everything worked as expected. I would be happy to revise the snippet if you report otherwise (please provide screenshots). I have tested this code with Storefront theme, the WooCommerce version listed above and a WordPress-friendly hosting on PHP 7.

If you think this code saved you time & money, please join other Business Bloomer supporters and avail of 365 days of WooCommerce benefits. Thank you in advance :)

Need Help with WooCommerce?

Check out these free videos, tutorials and tips!

  • how-to-edit-woocommerce-with-php-snippets
  • woocommerce-hooks-add_action-list-visual
  • woocommerce-customize-single-product-page-PHP
  • woocommerce-customize-shop-page-PHP
  • woocommerce-advanced-customization
  • how-to-edit-woocommerce-cart
  • woocommerce-customize-checkout-page-PHP
  • woocommerce-email-customization
  • woocommerce-conditional-logic

Rodolfo Melogli

Author, WooCommerce expert, WordCamp speaker and Internet marketer, Rodolfo Melogli has worked as a WooCommerce freelancer since 2011. He helps entrepreneurs and developers overcome their WooCommerce nightmares :) Rodolfo is the organiser of WordCamp Dublin, the Dublin WooCommerce Meetup, the Dublin Ecommerce Meetup and the Dublin WordPress Meetup. He enjoys interacting with people, travelling and chasing tennis & soccer balls. Of course, he loves pizza too.

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

  1. 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!

  2. 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!

  3. 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

  4. 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!

  5. 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!

    2. 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?

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

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

    5. 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? Support? Leave your Comment Now!
If you're writing code, please wrap it between: [php] code_here [/php]

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.