问题
I added a action button in the order page, the problem is no passing any data to the function.
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>';
}
so always show a 0 when i use the button. customer_second_payment_reminder_button function don't receive the ?order_id parameter from the url. i put a var_dump($order_id); in the function and show string(0) "" 0
how i can pass the order id to the function?
回答1:
There are some mistakes and some missing things in your code:
- There are no function arguments with
wp_ajax_{action}
orwp_ajax_nopriv_{action}
action hooks. - The order ID is sent via the URL, so you can catch it through
$_GET
variable - You have to secure your Ajax function and to embed a redirection at the end. If not you get a white page. Also never forget
exit;
at the end...
So your functional code will be:
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>';
}
}
Code goes in functions.php file of your active child theme (or active theme). Tested and works.
来源:https://stackoverflow.com/questions/56031411/custom-action-button-in-woocommerce-admin-orders-that-send-an-email