繁体   English   中英

当 woocommerce_order_status_changed 钩子触发时,Woocommerce admin_notices 不起作用

[英]Woocommerce admin_notices does not work when woocommerce_order_status_changed hook fires

我是 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();

好问题。 这让我很好奇,让我深入研究了这个WC_Admin_Notices类。 这就是我发现的!

好了,在说WC_Admin_Notices类之前,先来说说你的第一个问题!

“通知没有出现”

因为当woocommerce_order_status_changed钩子触发时,没有与之关联的屏幕,而不仅仅是通知,例如,如果您尝试执行print_r和/或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!');
}

但是如果你用echoprint_r替换die('You hit the right hook!')函数,无论你将钩子的优先级设置多高,它们都不会显示任何内容。


当您使用WC_Admin_Notices类时,它会变WC_Admin_Notices有趣。 如果没有与woocommerce_order_status_changed挂钩关联的屏幕,那么这个类是如何工作的? 嗯,方法如下:

class-wc-admin-notices.php

  1. 它有一个名为$notices的属性,它是一个简单的空数组。
  2. 当您调用add_custom_notice方法时,它会将您提供的值存储到数据库和“选项”表中。 键将是“woocommerce_admin_notice_{您为例如 test 提供的名称}”,值将是您定义的消息/通知。 这就是它所做的一切! 它将您的消息/通知存储到数据库中。
  3. 当您调用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类。

我们可以使用以下方法之一:

  • 使用$_SESSION + admin_notices钩子的组合
  • 使用cookielocal storage + admin_notices钩子的组合
  • 使用database + admin_notices钩子的组合

我认为,如果我们使用$_SESSION + admin_notices钩子方法,它既安全又快速,甚至无需查询数据库以获取简单的文本字符串。 这是我目前能想到的解决方案:

代码进入活动主题的functions.php文件。

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']);
  }
}

笔记:

  • 这仅在有订单且$old_status不等于$new_status
  • $notice变量中,我放置了原始文本,而不是 html,因为我们在admin_notices挂钩的 html 部分/模板中放置了一个p标记。
  • 我对div标签使用了notice-error类,它显示了通知的red color 您可以将其替换为notice-warningnotice-infonotice-success

这个答案已经在 woocommerce 5.7上进行了全面测试并且工作正常。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM