I'm new to WordPress development and I'm currently encountering a dead-end.
I want an admin notice to be displayed in a WooCommerce order after the order's status has been changed.
With the following code, the notice doesn't appear:
<?php
class TestNotice {
public function testTheNotice() {
add_action('woocommerce_order_status_changed', [$this, 'test'], 10, 4);
}
public function test(int $id, string $statusFrom, string $statusTo, WC_Order $order)
{
add_action('admin_notices', [$this, 'notice']);
}
public function notice()
{
?>
<div class="notice notice-error is-dismissible">
<p>This notice appears on the order page.</p>
</div>
<?php
}
}
$testNotice = new TestNotice();
$testNotice->testTheNotice();
I have tried setting the "priority" parameter of the "admin_notices" action to 20
, without success (I think it would have been useful if the nested action was the same as the one first called).
However, when I call "admin_notices" action directly in testTheNotice()
method (and thus not calling "woocommerce_order_status_changed" action), it works (on every admin page, which is not what I want).
I thought it was because the notice()
was somehow not recognised, but it actually is: the code below displays "This notice appears on the order page." on a blank page (which is not what I want and only for test purpose).
<?php
class TestNotice {
public function testTheNotice() {
add_action('woocommerce_order_status_changed', [$this, 'test'], 10, 4);
}
public function test(int $id, string $statusFrom, string $statusTo, WC_Order $order)
{
call_user_func([$this, 'notice']); die();
}
public function notice()
{
?>
<div class="notice notice-error is-dismissible">
<p>This notice appears on the order page.</p>
</div>
<?php
}
}
$testNotice = new TestNotice();
$testNotice->testTheNotice();
I'm aware there's a special class and method for WooCommerce admin notices, and writing below code in notice()
displays a notice, but with a purple border (because of the "update" css class, which I haven't found how to change) instead of a red border (which would be possible thanks to the "error" css class, which I don't know how to apply).
$adminNotice = new WC_Admin_Notices();
$adminNotice->add_custom_notice("Test",'<p>This notice appears on the order page.</p>');
$adminNotice->output_custom_notices();