发送电子邮件的WooCommerce管理员订单中的“自定义操作"按钮 [英] Custom action button in WooCommerce admin orders that send an email

查看:55
本文介绍了发送电子邮件的WooCommerce管理员订单中的“自定义操作"按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在订单页面中添加了一个操作按钮,问题是没有向该函数传递任何数据.

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

所以当我使用按钮时总是显示0.customer_second_payment_reminder_button函数未从URL接收?order_id参数.我放了一个var_dump($ order_id);在函数中并显示string(0)" 0

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

我如何将订单ID传递给函数?

how i can pass the order id to the function?

推荐答案

您的代码中存在一些错误和遗漏的东西:

There are some mistakes and some missing things in your code:

  • 没有带有 wp_ajax_ {action} wp_ajax_nopriv_ {action} 动作钩子的函数参数.
  • 订单ID是通过URL发送的,因此您可以通过 $ _ GET 变量
  • 来捕获订单ID.
  • 您必须保护Ajax功能并在最后嵌入重定向.如果没有,您将得到一个白页.也永远不要忘记最后的 exit; ...
  • There are no function arguments with wp_ajax_{action} or wp_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>';
    }
}

代码进入您的活动子主题(或活动主题)的functions.php文件中.经过测试并可以正常工作.

Code goes in functions.php file of your active child theme (or active theme). Tested and works.

这篇关于发送电子邮件的WooCommerce管理员订单中的“自定义操作"按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆