我是 WordPress 开发新手,目前遇到了死胡同。
我希望在订单状态更改后在 WooCommerce 订单中显示管理员通知。
使用以下代码,不会出现该通知:
<?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();
Run Code Online (Sandbox Code Playgroud)
我尝试将“admin_notices”操作的“优先级”参数设置为20,但没有成功(我认为如果嵌套操作与第一次调用的操作相同,这会很有用)。
但是,当我直接在方法中调用“admin_notices”操作testTheNotice()(因此不调用“woocommerce_order_status_changed”操作)时,它可以工作(在每个管理页面上,这不是我想要的)。
我以为这是因为notice()不知何故无法识别,但实际上是:下面的代码显示“此通知出现在订单页面上”。在空白页上(这不是我想要的,仅用于测试目的)。
<?php
class TestNotice {
public function testTheNotice() {
add_action('woocommerce_order_status_changed', [$this, …Run Code Online (Sandbox Code Playgroud)