【问题标题】:Action button in WooCommerce admin orders list that send the customer invoiceWooCommerce 管理订单列表中发送客户发票的操作按钮
【发布时间】:2021-05-19 16:49:17
【问题描述】:

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

【问题讨论】:

  • 请对我的回答提供一些反馈。

标签: php ajax wordpress woocommerce email-notifications


【解决方案1】:

已更新 (添加了缺少的'

以下将在管理订单列表操作列中添加一个操作按钮,用于“暂停”状态的订单。该按钮通过管理员 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>';
}

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

【讨论】:

  • 它不起作用。按下按钮后,它会转到此页面并显示一个空白页面。 example.com/wp-admin/admin-ajax.php?action=send_invoice_email&order_id=25845&_wpnonce=16155d51b2
  • 是的,即使缺少' 已修复,对我来说也是如此。
  • 哦,等等,它确实有效并发送了电子邮件,但它只被重定向到一个空白页面。无论如何要将其重定向回订单页面?
  • 是的,那很好。之后留下空白页不是很有用。它应该“返回”到“订单”页面。
  • @HaroldAldersen 我已经删除了重定向...所以电子邮件只是发送,每次按下相关的操作图标。
猜你喜欢
  • 2019-09-25
  • 1970-01-01
  • 2021-07-16
  • 2018-01-12
  • 2016-10-26
  • 2021-06-04
  • 2018-04-27
  • 2021-09-01
  • 1970-01-01
相关资源
最近更新 更多