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!
woocommerce
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        Watch tutorial @ https://businessbloomer.com/?p=19055
 * @sourcecode    https://businessbloomer.com/?p=21936
 * @author        Rodolfo Melogli
 * @testedwith    WooCommerce 2.6.14
 */
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', 10, 2 );
function bbloomer_hide_hidden_product_from_cart( $visible, $cart_item, $cart_item_key ) {
$product_id = apply_filters( 'woocommerce_cart_item_product_id', $cart_item['product_id'], $cart_item, $cart_item_key );
if ( get_post_meta( $product_id, '_visibility', true ) == 'hidden' ) {
        $visible = false;
    }
    return $visible;
}
function bbloomer_hide_hidden_product_from_order( $visible, $order_item ) {
if ( get_post_meta( $order_item['product_id'], '_visibility', true ) == '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 (and in case of CSS, this goes at the bottom of the style.css file) – if you need more guidance, please take a look at my free WooCommerce Customization video tutorial.

Did this snippet 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.

And if you think this code saved you time & money, please join the other Business Bloomer supporters and avail of 365 days of WooCommerce benefits. Thank you in advance! Rodolfo,

Mostakim Hossen

I am Mostakim Hossain, a Web designer and developer, 23 years of age. I have been working as a freelance (upwork) for almost 2 years now and I will be glad in helping you create your next awesome project.

I have created lots of public and private websites(some shown below) and always looking for more work to learn from. I am using PHP as my primary programming language. You can see my skill set beside this text.

I am a person who is willing to learn and adapts quickly. I am keen to design and security. I believe in the core principles of Open Source and support it. I have a minimalist approach to design and usability. Simplicity is beauty they say.
Masters of Accountancy : Asian University of Bangladesh
I’m here to answer any questions you might have about our products and services. Feel free to contact me using any of the following methods:

Email : support@smarterdevs.com

Leave a Reply

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

Subscribe
SUBSCRIBE NOW

Coupon : happynewyear

60% OFF

Stay updated with all latest updates,upcoming plugins & much more.
WordPress Themes
Search