0 レビュー
1 回答
php-woocommerce_order_status_changedフックが起動すると、Woocommerceadmin_noticesが機能しません
私は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();
「admin_notices」アクションの「priority」パラメーターを20
に設定しようとしましたが、成功しませんでした(ネストされたアクションが最初に呼び出されたものと同じであれば便利だったと思います)。
testTheNotice()
メソッドで「admin_notices」アクションを直接呼び出すと(したがって、「woocommerce_order_status_changed」アクションを呼び出さないで)、機能します(すべての管理ページで、これは私が望むものではありません)。
notice()
がどういうわけか認識されなかったためだと思いましたが、実際は次のようになっています。以下のコードは「この通知は注文ページに表示されます」と表示されます。空白のページ(これは私が望むものではなく、テスト目的のためだけです)。
<?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();
WooCommerce管理者通知用の特別なクラスとメソッドがあることを認識しており、以下のコードをnotice()
書き込むと通知が表示されますが、紫色の境界線が表示されます(これは、「更新」cssクラスが原因です。赤い境界線の代わりに(変更方法を見つけました)(これは、適用方法がわからない「エラー」cssクラスのおかげで可能です)。
$adminNotice = new WC_Admin_Notices();
$adminNotice->add_custom_notice("Test",'<p>This notice appears on the order page.</p>');
$adminNotice->output_custom_notices();
わからない
同様の質問
私たちのウェブサイトで同様の質問で答えを見つけてください。