WooCommerce 管理员订单中发送电子邮件的自定义操作按钮
Custom action button in WooCommerce admin orders that send an email
我在订单页面添加了一个操作按钮,问题是没有向函数传递任何数据。
add_filter( 'woocommerce_admin_order_actions', 'add_customer_second_payment_reminder_button', 100, 2 );
function add_customer_second_payment_reminder_button( $actions, $order ) {
if ( $order->has_status(array( 'partially-paid' ) )) {
$actions['email_reminder'] = array(
'url' => wp_nonce_url( admin_url('admin-ajax.php?action=customer_second_payment_reminder_button&order_id=' . $order->get_id() )),
'name' => __( 'Email Second Payment Reminder' , 'woocommerce-deposits' ),
'action' => 'email_reminder',
);
}
return $actions;
}
add_action( 'wp_ajax_customer_second_payment_reminder_button', 'customer_second_payment_reminder_button' );
function customer_second_payment_reminder_button( $order_id ) {
do_action( 'woocommerce_deposits_second_payment_reminder_email' , $order_id );
}
add_action( 'admin_head', 'add_customer_second_payment_reminder_button_css' );
function add_customer_second_payment_reminder_button_css() {
echo '<style>.wc-action-button-'.'email_reminder'.'::after { font-family: woocommerce !important; content: "\e030" !important; }</style>';
}
所以当我使用按钮时总是显示 0。
customer_second_payment_reminder_button 函数不接收来自 url 的 ?order_id 参数。
我放了一个 var_dump($order_id);在函数中显示 string(0) "" 0
如何将订单 ID 传递给函数?
您的代码中存在一些错误和遗漏的内容:
wp_ajax_{action}
或 wp_ajax_nopriv_{action}
操作挂钩没有函数参数。
- 订单 ID 通过 URL 发送,因此您可以通过
$_GET
变量获取它
- 您必须保护您的 Ajax 函数并在末尾嵌入重定向。如果不是,你会得到一个白页。也永远不要忘记最后的
exit;
...
因此您的功能代码将是:
add_filter( 'woocommerce_admin_order_actions', 'add_customer_second_payment_reminder_button', 100, 2 );
function add_customer_second_payment_reminder_button( $actions, $order ) {
if ( $order->has_status( array('partially-paid') ) ) {
$actions['email_reminder'] = array(
'url' => wp_nonce_url(
admin_url('admin-ajax.php?action=customer_second_payment_reminder&order_id=' . $order->get_id() ),
'customer-second-payment-reminder'
),
'name' => __( 'Email Second Payment Reminder', 'woocommerce-deposits' ),
'action' => 'email_reminder',
);
}
return $actions;
}
add_action( 'wp_ajax_customer_second_payment_reminder', 'get_customer_second_payment_reminder' );
function get_customer_second_payment_reminder() {
if ( current_user_can('edit_shop_orders') && check_admin_referer('customer-second-payment-reminder') &&
isset($_GET['order_id']) && get_post_type( absint( wp_unslash($_GET['order_id']) ) ) === 'shop_order' ) {
$order_id = absint( wp_unslash($_GET['order_id']) );
$order = wc_get_order($order_id);
if( is_a($order, 'WC_Order') ) {
do_action( 'woocommerce_deposits_second_payment_reminder_email', $order_id, $order );
update_post_meta( $order_id, '_reminder_button', 'OK' ); // For testing purpose (to be removed)
}
}
wp_safe_redirect( wp_get_referer() ? wp_get_referer() : admin_url( 'edit.php?post_type=shop_order' ) );
exit;
}
add_action( 'admin_head', 'customer_second_payment_reminder_button_css' );
function customer_second_payment_reminder_button_css() {
global $pagenow;
if( $pagenow === 'edit.php' && isset($_GET['post_type']) && $_GET['post_type'] === 'shop_order' ) {
echo '<style>.wc-action-button-'.'email_reminder'.'::after { font-family: woocommerce !important; content: "\e02d" !important; }</style>';
}
}
代码进入您的活动子主题(或活动主题)的 functions.php 文件。 已测试并有效。
我在订单页面添加了一个操作按钮,问题是没有向函数传递任何数据。
add_filter( 'woocommerce_admin_order_actions', 'add_customer_second_payment_reminder_button', 100, 2 );
function add_customer_second_payment_reminder_button( $actions, $order ) {
if ( $order->has_status(array( 'partially-paid' ) )) {
$actions['email_reminder'] = array(
'url' => wp_nonce_url( admin_url('admin-ajax.php?action=customer_second_payment_reminder_button&order_id=' . $order->get_id() )),
'name' => __( 'Email Second Payment Reminder' , 'woocommerce-deposits' ),
'action' => 'email_reminder',
);
}
return $actions;
}
add_action( 'wp_ajax_customer_second_payment_reminder_button', 'customer_second_payment_reminder_button' );
function customer_second_payment_reminder_button( $order_id ) {
do_action( 'woocommerce_deposits_second_payment_reminder_email' , $order_id );
}
add_action( 'admin_head', 'add_customer_second_payment_reminder_button_css' );
function add_customer_second_payment_reminder_button_css() {
echo '<style>.wc-action-button-'.'email_reminder'.'::after { font-family: woocommerce !important; content: "\e030" !important; }</style>';
}
所以当我使用按钮时总是显示 0。 customer_second_payment_reminder_button 函数不接收来自 url 的 ?order_id 参数。 我放了一个 var_dump($order_id);在函数中显示 string(0) "" 0
如何将订单 ID 传递给函数?
您的代码中存在一些错误和遗漏的内容:
wp_ajax_{action}
或wp_ajax_nopriv_{action}
操作挂钩没有函数参数。- 订单 ID 通过 URL 发送,因此您可以通过
$_GET
变量获取它 - 您必须保护您的 Ajax 函数并在末尾嵌入重定向。如果不是,你会得到一个白页。也永远不要忘记最后的
exit;
...
因此您的功能代码将是:
add_filter( 'woocommerce_admin_order_actions', 'add_customer_second_payment_reminder_button', 100, 2 );
function add_customer_second_payment_reminder_button( $actions, $order ) {
if ( $order->has_status( array('partially-paid') ) ) {
$actions['email_reminder'] = array(
'url' => wp_nonce_url(
admin_url('admin-ajax.php?action=customer_second_payment_reminder&order_id=' . $order->get_id() ),
'customer-second-payment-reminder'
),
'name' => __( 'Email Second Payment Reminder', 'woocommerce-deposits' ),
'action' => 'email_reminder',
);
}
return $actions;
}
add_action( 'wp_ajax_customer_second_payment_reminder', 'get_customer_second_payment_reminder' );
function get_customer_second_payment_reminder() {
if ( current_user_can('edit_shop_orders') && check_admin_referer('customer-second-payment-reminder') &&
isset($_GET['order_id']) && get_post_type( absint( wp_unslash($_GET['order_id']) ) ) === 'shop_order' ) {
$order_id = absint( wp_unslash($_GET['order_id']) );
$order = wc_get_order($order_id);
if( is_a($order, 'WC_Order') ) {
do_action( 'woocommerce_deposits_second_payment_reminder_email', $order_id, $order );
update_post_meta( $order_id, '_reminder_button', 'OK' ); // For testing purpose (to be removed)
}
}
wp_safe_redirect( wp_get_referer() ? wp_get_referer() : admin_url( 'edit.php?post_type=shop_order' ) );
exit;
}
add_action( 'admin_head', 'customer_second_payment_reminder_button_css' );
function customer_second_payment_reminder_button_css() {
global $pagenow;
if( $pagenow === 'edit.php' && isset($_GET['post_type']) && $_GET['post_type'] === 'shop_order' ) {
echo '<style>.wc-action-button-'.'email_reminder'.'::after { font-family: woocommerce !important; content: "\e02d" !important; }</style>';
}
}
代码进入您的活动子主题(或活动主题)的 functions.php 文件。 已测试并有效。