Php WooCommerce管理订单列表中发送客户发票的操作按钮

Php WooCommerce管理订单列表中发送客户发票的操作按钮,php,ajax,wordpress,woocommerce,email-notifications,Php,Ajax,Wordpress,Woocommerce,Email Notifications,在Wordpress管理仪表板中,当您打开woocommerce订单页面时,您将看到订单列表,列为(订单、日期、状态、账单、发货、总计、操作) 我想为操作列表添加一个新按钮,然后添加此操作以将电子邮件发票发送给订单仍处于保留状态的客户 我已经创建了下面的列和按钮,但是调用操作按钮将每张订单的发票发送给客户时遇到了问题 请给我一些建议 add_filter( 'woocommerce_admin_order_actions', 'add_custom_order_status_actions_b

在Wordpress管理仪表板中,当您打开woocommerce订单页面时,您将看到订单列表,列为(订单、日期、状态、账单、发货、总计、操作

我想为操作列表添加一个新按钮,然后添加此操作以将电子邮件发票发送给订单仍处于保留状态的客户

我已经创建了下面的列和按钮,但是调用操作按钮将每张订单的发票发送给客户时遇到了问题

请给我一些建议

add_filter( 'woocommerce_admin_order_actions', 'add_custom_order_status_actions_button', 100, 2 );
function add_custom_order_status_actions_button( $actions, $order ) {

    if ( $order->has_status( array( 'on-hold' ) ) ) {

        // The key slug defined for your action button
        $action_slug = 'invoice';
        $status = $_GET['status'];
        $order_id = method_exists($the_order, 'get_id') ? $the_order->get_id() : $the_order->id;
        // Set the action button
        $actions[$action_slug] = array(
            'url'       => admin_url('post.php?post=' . $post_id . '&action=edit&message=11'),
            'name'      => __( 'Invoice', 'woocommerce' ),
            'action'    => $action_slug,
        );
    }
    return $actions;
}

add_action( 'admin_head', 'add_custom_order_status_actions_button_css' );
function add_custom_order_status_actions_button_css() {
    $action_slug = "invoice"; // The key slug defined for your action button
    echo '<style>.wc-action-button-'.$action_slug.'::after { font-family: woocommerce !important; content: "\e029" !important; }</style>';
}
add_filter('woocmerce_admin_order_actions','add_custom_order_status_actions_button',100,2);
功能添加\自定义\订单\状态\操作\按钮($actions,$order){
如果($order->has_status(数组('on hold'))){
//为动作按钮定义的键段塞
$action_slug='发票';
$status=$_GET['status'];
$order\u id=method\u exists($the\u order,'get\u id')?$the\u order->get\u id():$the\u order->id;
//设置操作按钮
$actions[$action\u slug]=数组(
'url'=>admin\u url('post.php?post='.$post\u id.&action=edit&message=11'),
“名称”=>uuuuuuu('Invoice','woocommerce'),
“action”=>$action\u slug,
);
}
返回$actions;
}
添加操作('admin_head'、'add_custom_order_status_actions_button_css');
函数添加\自定义\订单\状态\操作\按钮\ css(){
$action\u slug=“invoice”;//为操作按钮定义的键slug
echo'.wc action button-'.$action_slug.'::在{font-family:woocommerce!重要;内容:“\e029”!重要;}”之后;
}
更新了(添加了缺少的

以下内容将在“管理订单列表操作”列中添加一个操作按钮,用于“保留”状态的订单。该按钮通过admin Ajax发送客户发票通知

完整代码:

add_filter( 'woocommerce_admin_order_actions', 'add_admin_order_custom_actions_button', 100, 2 );
function add_admin_order_custom_actions_button( $actions, $order ) {
    if ( $order->has_status( array( 'on-hold' ) ) ) {
        // The key slug defined for your action button
        $action_slug = 'email_invoice';

        // Set the action button
        $actions[$action_slug] = array(
            'url'    => wp_nonce_url(
                admin_url('admin-ajax.php?action=send_invoice_email&order_id=' . $order->get_id() ),
                'send-invoice-email'
            ),
            'name'   => __( 'Send Invoice', 'woocommerce' ),
            'action' => $action_slug,
        );
    }
    return $actions;
}

add_action( 'wp_ajax_send_invoice_email', 'trigger_customer_email_invoice' );
function trigger_customer_email_invoice() {
    if ( current_user_can('edit_shop_orders') && check_admin_referer('send-invoice-email') &&
    isset($_GET['order_id']) && get_post_type( absint( wp_unslash($_GET['order_id']) ) ) === 'shop_order' ) {
        $order_id = absint( wp_unslash($_GET['order_id']) );

        WC()->mailer()->get_emails()['WC_Email_Customer_Invoice']->trigger($order_id); // Send email
        update_post_meta( $order_id, '_invoice_sent', 'OK' ); // For testing purpose (to be removed)
    }
}

add_action( 'admin_head', 'add_custom_order_status_actions_button_css' );
function add_custom_order_status_actions_button_css() {
    $action_slug = "email_invoice"; // The key slug defined for your action button
    echo '<style>.wc-action-button-'.$action_slug.'::after { font-family: woocommerce !important; content: "\e02d" !important; }</style>';
}
add_filter('woocommerce_admin_order_actions','add_admin_order_custom_actions_button',100,2);
函数添加\管理\命令\自定义\操作\按钮($actions,$order){
如果($order->has_status(数组('on hold'))){
//为动作按钮定义的键段塞
$action_slug='email_invoice';
//设置操作按钮
$actions[$action\u slug]=数组(
'url'=>wp\u nonce\u url(
admin\u url('admin-ajax.php?action=send\u invoice\u email&order\u id='。$order->get\u id()),
“发送发票电子邮件”
),
“名称”=>uuuuuuuuuuuu('Send Invoice','woocommerce'),
“action”=>$action\u slug,
);
}
返回$actions;
}
添加操作(“wp\U ajax\U发送发票\U电子邮件”、“触发客户\U电子邮件\U发票”);
函数触发器\客户\电子邮件\发票(){
如果(当前用户可以(编辑车间订单)和检查管理员(发送发票电子邮件)&&
isset($\u GET['order\u id'])和&GET\u post\u类型(absint(wp\u unslash($\u GET['order\u id']))===='shop\u order'){
$order\u id=absint(wp\u unslash($\u GET['order\u id']);
WC()->mailer()->get_emails()['WC_Email_Customer_Invoice']->trigger($order_id);//发送电子邮件
出于测试目的(将被删除)更新发布元数据($order\u id,''u invoice\u sent','OK');//
}
}
添加操作('admin_head'、'add_custom_order_status_actions_button_css');
函数添加\自定义\订单\状态\操作\按钮\ css(){
$action\u slug=“email\u invoice”;//为操作按钮定义的密钥slug
echo'.wc action button-'.$action_slug.'::在{font-family:woocommerce!重要;内容:“\e02d”!重要;}”之后;
}

代码进入活动子主题(或活动主题)的functions.php文件。经过测试,效果良好。

如果您对我下面的答案有任何反馈,我们将不胜感激。它不起作用。按下按钮后,它将转到此页并显示空白页。example.com/wp admin/admin ajax.php?action=send\u invoice\u email&order\u id=25845&\u wpnance=16155d51b2Yep,即使缺少
修复了,我也一样。哦,等等,它实际上可以工作并发送了电子邮件,但它只重定向到空白页。是否要将其重定向回订单页面?是的,那太好了。之后留下空白页不是很有用。它应该“返回”到“订单”页面。@Haroldersen我已经删除了重定向…所以每次按下相关操作图标时,电子邮件都会被发送。