我是 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();
uj5u.com熱心網友回復:
好問題。這讓我感到好奇,并讓我深入了解這WC_Admin_Notices門課。這就是我發現的!
好,在說WC_Admin_Notices上課之前,先說你的第一個問題!
“通知沒有出現”
因為當woocommerce_order_status_changed鉤子觸發時,沒有與之關聯的螢屏,它不僅僅是通知,例如,如果您嘗試執行 aprint_r和/或 an,echo它們也不會顯示任何內容,因為沒有與該鉤子關聯的螢屏。您可以通過使用die函式來發現您命中該鉤子的唯一方法。為了測驗這一點,你可以這樣做:
add_action('woocommerce_order_status_changed', 'test', 99, 4);
function test($order_id, $old_status, $new_status, $order_object)
{
die('You hit the right hook!');
}
但是,如果您將die('You hit the right hook!')函式替換為echo或print_r,無論您將鉤子的優先級設定多高,它們都不會顯示任何內容。
使用WC_Admin_Noticesclass時會變得很有趣。如果沒有與woocommerce_order_status_changed鉤子關聯的螢屏,那么這個類是如何作業的?嗯,方法如下:
class-wc-admin-notices.php班級
- 它有一個名為的屬性
$notices,它是一個簡單的空陣列。 - 當您呼叫
add_custom_notice方法時,它會將您提供的值存盤到資料庫和“選項”表中。鍵將是“woocommerce_admin_notice_{您為例如 test 提供的名稱}”,值將是您定義的訊息/通知。這就是它所做的一切!它將您的訊息/通知存盤到資料庫中。 - 當您呼叫
output_custom_notices方法時,它會檢查$notices陣列,并檢查資料庫中存盤在選項表中的任何鍵值對,格式為我剛剛在第 2 條中提到的格式!然后,它使用html-notice-custom.php在以下路徑中呼叫的模板:
yourwebsite.com > wp-content > 插件 > woocommerce > 包含 > 管理 > 視圖 > html-notice-custom.php
html-notice-custom.php模板
該html-notice-custom.php檔案負責輸出自定義通知的 html 標記,它將為它們提供一個名為的類,該類updated將觸發具有淺紫色的 css 規則。
所以類的整體作業流程WC_Admin_Notices是:
- 將您的自定義訊息存盤到陣列或資料庫中
- 在螢屏加載時檢索存盤的鍵值對!
從技術上講,這就是它所做的一切,好吧,用最簡單的術語來說!
由于我們不想要那個自定義的淺紫色模板,我們可以使用WC_Admin_Notices使用和撰寫我們自己的解決方案而不使用WC_Admin_Notices類的作業流。
我們可以使用以下方法之一:
- 使用
$_SESSIONadmin_notices鉤子的組合 - 使用
cookie或local storageadmin_notices鉤子的組合 - 使用
databaseadmin_notices鉤子的組合
I think, if we use $_SESSION admin_notices hook approach, it'd be both safe and fast without even query the database for a simple string of text. Here's the solution I can think of at this moment:
Code goes into the functions.php file of your active theme.
add_action('woocommerce_order_status_changed', 'test', 99, 4);
function test($order_id, $old_status, $new_status, $order_object)
{
if ( $order_object && ($old_status != $new_status) )
{
$notice = 'This notice appears on the order page.';
session_start();
$_SESSION['your_custom_message_name'] = $notice;
}
}
add_action('admin_notices', 'your_them_displaying_custom_admin_notice');
function your_them_displaying_custom_admin_notice()
{
session_start();
if (isset($_SESSION['your_custom_message_name'])) {
?>
<div class='notice notice-error is-dismissible'>
<p><?php echo $_SESSION['your_custom_message_name'] ?></p>
</div>
<?php
unset($_SESSION['your_custom_message_name']);
}
}
Note:
- This works only if there is an order AND the
$old_statusis not equal to the$new_status. - In the
$noticevariable, I put raw text, not html, since we put aptag in the html section/template of theadmin_noticeshook. - I've used
notice-errorclass for thedivtag which shows thered colorfor the notice. You could replace it withnotice-warning, ornotice-infoornotice-success.
This answers has been fully tested on woocommerce 5.7 and works fine.
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/352914.html
標籤:php WordPress的 求购 钩子商务 wordpress-admin
