我无法通过向函数发送参数来显示管理消息。有人建议我使用闭包函数,但这似乎不起作用。这是代码
function custom_woocommerce_admin_notice( $message ) {
add_action( 'admin_notices', function() use ( $message ) {
wc_get_logger()->info('Order Message: '.$message ) ;
if ( function_exists( 'get_current_screen' ) ) {
$screen = get_current_screen();
if ( isset( $screen->id ) && $screen->id === 'edit-shop_order' ) {
// Check if the notice has already been dismissed
if ( ! get_option( 'custom_woocommerce_admin_notice_dismissed' ) ) {
?>
<div class="notice notice-success is-dismissible woocommerce-notice">
<p><?php _e( 'Hello: '.$message , 'text-domain' ); ?></p>
</div>
<?php
}
}
}
});
}
add_action( 'admin_notices', 'custom_woocommerce_admin_notice' );
function custom_woocommerce_admin_notice_dismissed() {
update_option( 'custom_woocommerce_admin_notice_dismissed', 1 );
}
add_action( 'wp_ajax_custom_woocommerce_admin_notice_dismissed', 'custom_woocommerce_admin_notice_dismissed' );
function enqueue_custom_woocommerce_admin_notice_script( $hook ) {
// Check if we are on the WooCommerce orders page
if ( function_exists( 'get_current_screen' ) ) {
$screen = get_current_screen();
if ( isset( $screen->id ) && $screen->id === 'edit-shop_order' ) {
wp_enqueue_script( 'custom_woocommerce_admin_notice_script', get_template_directory_uri() . '/js/custom-woocommerce-admin-notice.js', array( 'jquery' ), null, true );
}
}
}
add_action( 'admin_enqueue_scripts', 'enqueue_custom_woocommerce_admin_notice_script' );
该函数被调用使用
custom_woocommerce_admin_notice( $message );
有什么问题吗?没有闭包函数并且不传递 $message 变量并使用字符串它可以工作。
为了完整起见,这里是 js 脚本
jQuery(document).ready(function($) {
$('.notice.is-dismissible').on('click', '.notice-dismiss', function() {
$.ajax({
url: ajaxurl,
type: 'POST',
data: {
action: 'custom_woocommerce_admin_notice_dismissed'
}
});
});
});
您的代码中有多个错误。
要在此自定义通知中动态显示消息,可以使用以下方法。
对于关闭的选项,与其保存在全局选项设置中,不如保存为用户元数据。
PHP 代码:
class Custom_WC_Admin_Notice {
/**
* Message to be displayed in a warning.
*
* @var string
*/
private string $message;
/**
* Initialize class.
*
* @param string $message Message to be displayed in a notice.
*/
public function __construct( string $message ) {
$this->message = $message;
add_action( 'admin_notices', array( $this, 'render' ) );
add_action( 'admin_enqueue_scripts', array( $this, 'admin_enqueue_scripts') );
add_action( 'wp_ajax_custom_wc_admin_orders_notice_dismissed', array( $this, 'notice_dismissed') );
}
/**
* Displays warning on the admin screen.
*
* @return void
*/
public function render() {
if ( ! function_exists('get_current_screen') ) {
return;
}
$screen = get_current_screen();
if ( isset( $screen->id ) && in_array($screen->id, ['edit-shop_order', 'woocommerce_page_wc-orders'], true) ) {
$dismissed = get_user_meta( get_current_user_id(), 'custom_wc_admin_notice_dismissed', true );
if ( $dismissed ) {
return;
}
printf( '<div class="notice notice-success custom-notice is-dismissible"><p>Hello: %s</p></div>', esc_html( $this->message ) );
}
}
/**
* Enqueue Scripts
*
* @return void
*/
public function admin_enqueue_scripts() {
if ( ! function_exists('get_current_screen') ) {
return;
}
$screen = get_current_screen();
if ( isset( $screen->id ) && in_array($screen->id, ['edit-shop_order', 'woocommerce_page_wc-orders'], true) ) {
$dismissed = get_user_meta( get_current_user_id(), 'custom_wc_admin_notice_dismissed', true );
if ( $dismissed ) {
return;
}
// With the main theme use: get_template_directory_uri() . /js/…
// With a child theme use: get_stylesheet_directory_uri() . /js/…
// With a plugin use: plugin_dir_url( __FILE__ ) . js/…
wp_enqueue_script( 'custom-wc-admin-notice', get_stylesheet_directory_uri() . '/js/custom-wc-admin-notice.js', array( 'jquery' ), null, true );
wp_localize_script('custom-wc-admin-notice', 'wc_custom_dan', array(
'ajax_url' => admin_url('admin-ajax.php'),
'user_id' => get_current_user_id(),
'nonce' => wp_create_nonce('wc_custom_dan'),
));
}
}
/**
* Ajax update user metadata when notice is dismissed.
*
* @return void
*/
public function notice_dismissed() {
if ( isset($_POST['nonce']) && wp_verify_nonce($_POST['nonce'], 'wc_custom_dan')
&& isset($_POST['user_id']) && $_POST['user_id'] > 0 ) {
echo update_user_meta( intval($_POST['user_id']), 'custom_wc_admin_notice_dismissed', true );
}
wp_die();
}
}
代码位于子主题的functions.php文件中(或插件中)。
名为“custom-wc-admin-notice.js”的文件中的 JavaScript 代码(位于“js”目录中):
jQuery(function($) {
if ( typeof wc_custom_dan === 'undefined' ) {
return false;
}
$('.notice.is-dismissible.custom-notice').on('click', '.notice-dismiss', function() {
console.log('click');
$.ajax({
url: wc_custom_dan.ajax_url,
type: 'POST',
data: {
'action': 'custom_wc_admin_orders_notice_dismissed',
'user_id': wc_custom_dan.user_id,
'nonce': wc_custom_dan.nonce
},
success: function(response) {
console.log(response);
}
});
});
});
现在您可以使用自定义消息显示通知:
$message = "My custom message";
new Custom_WC_Admin_Notice( $message );
经过测试并可在 WooCommerce 管理订单列表上显示自定义通知(并与 HPOS 兼容)。