How to hide some products (items) from WooCommerce orders

34 Views Asked by At

I would like to hide one product from my order notification (which comes via email)

I took this code as a basis, it works. This code hides certain products in the checkout page using product IDs:

function filter_woocommerce_cart_item_visible( $true, $cart_item, $cart_item_key ) {    
// The targeted product ids
$targeted_ids = array( 5500, 5499, 5498 );
// Computes the intersection of arrays

if ( array_intersect( $targeted_ids, array( $cart_item['product_id'], $cart_item['variation_id'] ) ) ) {

    $true = false;
}
return $true; 
}
add_filter( 'woocommerce_cart_item_visible', 'filter_woocommerce_cart_item_visible', 10, 3 ); 

I tried changing the code to hide this product in order notifications (via order item), but this modified code is throwing an error. Here is the code:

function hide_product_from_order( $true, $order_item, $order_item_key ) {    
    // The targeted product ids
    $targeted_ids = array( 5500, 5499, 5498 );

    // Computes the intersection of arrays

    if ( array_intersect( $targeted_ids, array( $order_item['product_id'],$order_item['variation_id'] ) ) ) {
        $true = false;
    }
    return $true; 
}

add_filter( 'woocommerce_order_item_visible', 'hide_product_from_order', 10, 3 ); 
1

There are 1 best solutions below

0
LoicTheAztec On

They are some mistakes in your code, try the following revised code:

add_filter( 'woocommerce_order_item_visible', 'hide_specific_order_items', 10, 2 ); 
function hide_specific_order_items( $visible, $item ) {    
    // The targeted product ids to hide
    $targeted_ids = array( 5500, 5499, 5498 );

    // Hide matching order item(s)
    if ( array_intersect( $targeted_ids, array( $item->get_product_id(), $item->get_variation_id() ) ) ) {
        $visible = false;
    }
    return $visible; 
}

Code goes in functions.php file of your active child theme (or active theme). It should work now.