Skip to content
Advertisement

Hide item from array

I am showing the product categories on the WooCommerce thank you page using this piece of code:

add_action( 'woocommerce_thankyou', 'show_product_category', 10, 1 );
function show_product_category ( $order_id ){
    // Get $product object from $order / $order_id
    $order = wc_get_order( $order_id );
    $items = $order->get_items();

    foreach ( $items as $item ) {
        $product = $item->get_product();

        print_r ($product->get_categories());

    }
}

The code above shows all categories, but I need to hide a specific category. Is it possible to hide a specific category by ID in the above code?

Advertisement

Answer

Woocommerce uses main get_categories() function from WordPress core.

You can exclude any categories with passing option arguments to function based on “WP Terms Query

exclude parameter should be array or comma/space-separated string of category IDs to exclude.

$categories = $product->get_categories(array(
    'exclude' => array( 10, 11 ) // change these IDs
));
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement