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 π
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;
}
Hello,
This code breaks my site.
I don’t have error code.
thanks for your help !
kevin
Works for me, must be something else
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
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?
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
Hi Brooke, which PHP error di you get?
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
I haven’t tested that case scenario, so let us know
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
Weird. Try adding this check:
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?
Hi Wim, just tested on latest Woo, works perfectly, emails included. Must be something else
Can i remove the hidden product from the admin order edit page?
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!
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?
Hi Andy, this hides products from emails, but does not disable emails though
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
https://businessbloomer.com/woocommerce-exclude-hidden-products-from-mini-cart-counter/
Not working 3/1/2020. Hidden product is hidden as is entire cart content.
Please advise. Latest wordpress release
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
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
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
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.
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!
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!
Hey Chad: https://businessbloomer.com/woocommerce-exclude-hidden-products-from-mini-cart-counter/
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
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!
Hi,
In th latest version of woocommerce, there seems to be an issue with the following code:
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.
You’re absolutely right Vinoth π Snippet has now been updated!
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”
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
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
Ethan, thanks so much for your comment! And yes, you’re right π Snippet has now been updated – test it out!
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
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!
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?
Excellent Rory π Do you use a particular invoice plugin, or are you referring to the default WooCommerce invoice?
I have been using booster.io for its pdf feature and the pdf invoice is what includes the hidden items.
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