1

I need to show a specific notice when there is an article with the product category (product_cat) "media" in the order. I currently just know how to change it for all orders. All my other attempts where not working:

function customize_thankyou_page_notice( $message ) {
  $new_message = 'Your custom message goes here.';
  return $new_message;
}
add_filter( 'woocommerce_thankyou_order_received_text', 'customize_thankyou_page_notice' );
LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
public9nf
  • 1,311
  • 3
  • 18
  • 48
  • The callback function gets a second parameter passed, containing the order object. So receive that parameter, and then loop over the items contained in the order (if you don't know how to do that, research it), and see if you find one with that category. If so, return your new message, otherwise the existing one. – CBroe May 24 '23 at 09:56
  • Some feedback on the answer below will be appreciated, please. Thank you. – LoicTheAztec Jun 07 '23 at 17:34

1 Answers1

0

To show a specific notice on Thankyou page when an order item is from a specific product category, you can use the following code that will check on each order item for your specific product category (displaying a custom notice):

add_filter( 'woocommerce_thankyou_order_received_text', 'custom_thankyou_order_received_text', 20, 2 );
function custom_thankyou_order_received_text( $thankyou_text, $order ){
    // Loop through order items
    foreach ( $order->get_items() as $item ) {
        // Get the Product ID from WC_Order_Item_Product order item
        $product_id = $item->get_product_id();

        // We check for 'media' product category on each order item
        if ( has_term( 'media', 'product_cat', $product_id ) ) {
            $thankyou_text = __( 'Your custom message goes here.', 'woocommerce' );
            break; // We can stop the loop now.
        }
    }
    return $thankyou_text;
}

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

Related:

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399