WooCommerce 订单操作 Metabox 下拉列表中缺少自定义操作

Custom action missing from WooCommerce Order actions Metabox dropdown

this page 的帮助下,我在 WooCommerce 电子邮件中添加了一封额外的电子邮件。我希望它是我可以发送给客户的手动电子邮件,但它没有显示在要发送的电子邮件下拉列表中的顺序中。我猜我错过了什么,但我不知道是什么。这是我使用的插件的代码:

<?php
/**
 * Plugin Name: WooCommerce Custom Order Email
 * Plugin URI: http://www.skyverge.com/blog/how-to-add-a-custom-woocommerce-email/
 * Description: Demo plugin for adding a custom WooCommerce email that sends admins an email when an order is received with expedited shipping
 * Author: SkyVerge
 * Author URI: http://www.skyverge.com
 * Version: 0.1
 *
 * License: GNU General Public License v3.0
 * License URI: http://www.gnu.org/licenses/gpl-3.0.html
 *
 */

if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly

/**
 *  Add a custom email to the list of emails WooCommerce should load
 *
 * @since 0.1
 * @param array $email_classes available email classes
 * @return array filtered available email classes
 */
function add_expedited_order_woocommerce_email( $email_classes ) {

    // include our custom email class
    require_once( 'includes/class-wc-expedited-order-email.php' );

    // add the email class to the list of email classes that WooCommerce loads
    $email_classes['WC_Expedited_Order_Email'] = new WC_Expedited_Order_Email();

    return $email_classes;

}
add_filter( 'woocommerce_email_classes', 'add_expedited_order_woocommerce_email' );
<?php

if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly

/**
 * A custom Expedited Order WooCommerce Email class
 *
 * @since 0.1
 * @extends \WC_Email
 */
class WC_Expedited_Order_Email extends WC_Email {


    /**
     * Set email defaults
     *
     * @since 0.1
     */
    public function __construct() {

        // set ID, this simply needs to be a unique name
        $this->id = 'wc_expedited_order';

        // this is the title in WooCommerce Email settings
        $this->title = 'Factuur herinnering';

        // this is the description in WooCommerce email settings
        $this->description = 'Deze mail kan handmatig worden vestuurd ter herinnering. Factuur zit in de bijlage.';

        // these are the default heading and subject lines that can be overridden using the settings
        $this->heading = 'Herinnering factuur';
        $this->subject = 'Herinnering factuur';

        // these define the locations of the templates that this email should use, we'll just use the new order template since this email is similar
        $this->template_html  = 'emails/customer-reminder.php';
        $this->template_plain = 'emails/plain/customer-reminder.php';

        // Trigger on new paid orders
        //add_action( 'woocommerce_order_status_pending_to_processing_notification', array( $this, 'trigger' ) );
        //add_action( 'woocommerce_order_status_failed_to_processing_notification',  array( $this, 'trigger' ) );

        // Call parent constructor to load any other defaults not explicity defined here
        parent::__construct();

        // this sets the recipient to the settings defined below in init_form_fields()
        $this->recipient = $this->get_option( 'recipient' );

        // if none was entered, just use the WP admin email as a fallback
        if ( ! $this->recipient )
            $this->recipient = get_option( 'admin_email' );
    }


    /**
     * Determine if the email should actually be sent and setup email merge variables
     *
     * @since 0.1
     * @param int $order_id
     */
    public function trigger( $order_id ) {

        // bail if no order ID is present
        if ( ! $order_id )
            return;

        // setup order object
        $this->object = new WC_Order( $order_id );

        // bail if shipping method is not expedited
        /* if ( ! in_array( $this->object->get_shipping_method(), array( 'Three Day Shipping', 'Next Day Shipping' ) ) )
            return; */

        // replace variables in the subject/headings
        $this->find[] = '{order_date}';
        $this->replace[] = date_i18n( woocommerce_date_format(), strtotime( $this->object->order_date ) );

        $this->find[] = '{order_number}';
        $this->replace[] = $this->object->get_order_number();

        if ( ! $this->is_enabled() || ! $this->get_recipient() )
            return;

        // woohoo, send the email!
        $this->send( $this->get_recipient(), $this->get_subject(), $this->get_content(), $this->get_headers(), $this->get_attachments() );
    }


    /**
     * get_content_html function.
     *
     * @since 0.1
     * @return string
     */
    public function get_content_html() {
        ob_start();
        woocommerce_get_template( $this->template_html, array(
            'order'         => $this->object,
            'email_heading' => $this->get_heading()
        ) );
        return ob_get_clean();
    }


    /**
     * get_content_plain function.
     *
     * @since 0.1
     * @return string
     */
    public function get_content_plain() {
        ob_start();
        woocommerce_get_template( $this->template_plain, array(
            'order'         => $this->object,
            'email_heading' => $this->get_heading()
        ) );
        return ob_get_clean();
    }


    /**
     * Initialize Settings Form Fields
     *
     * @since 2.0
     */
    public function init_form_fields() {

        $this->form_fields = array(
            'enabled'    => array(
                'title'   => 'Enable/Disable',
                'type'    => 'checkbox',
                'label'   => 'Enable this email notification',
                'default' => 'yes'
            ),
            'recipient'  => array(
                'title'       => 'Recipient(s)',
                'type'        => 'text',
                'description' => sprintf( 'Enter recipients (comma separated) for this email. Defaults to <code>%s</code>.', esc_attr( get_option( 'admin_email' ) ) ),
                'placeholder' => '',
                'default'     => ''
            ),
            'subject'    => array(
                'title'       => 'Subject',
                'type'        => 'text',
                'description' => sprintf( 'This controls the email subject line. Leave blank to use the default subject: <code>%s</code>.', $this->subject ),
                'placeholder' => '',
                'default'     => ''
            ),
            'heading'    => array(
                'title'       => 'Email Heading',
                'type'        => 'text',
                'description' => sprintf( __( 'This controls the main heading contained within the email notification. Leave blank to use the default heading: <code>%s</code>.' ), $this->heading ),
                'placeholder' => '',
                'default'     => ''
            ),
            'email_type' => array(
                'title'       => 'Email type',
                'type'        => 'select',
                'description' => 'Choose which format of email to send.',
                'default'     => 'html',
                'class'       => 'email_type',
                'options'     => array(
                    'plain'     => __( 'Plain text', 'woocommerce' ),
                    'html'      => __( 'HTML', 'woocommerce' ),
                    'multipart' => __( 'Multipart', 'woocommerce' ),
                )
            )
        );
    }


} // end \WC_Expedited_Order_Email class

我已经注释掉触发器并添加代码使其成为手动客户邮件。

    function is_customer_email() {
        return true;
    }

    function is_manual() {
        return true;
    }

在 WooCommerce 设置中,我看到了邮件,它看起来像是可以发送给客户的手动邮件。邮件模板看起来不错,但我没有在真正发送邮件的顺序中看到下拉列表中的邮件名称。 我在这里错过了什么?

您错过了向 "ORDER actions" Metabox 下拉列表添加 "Send Expedited email" 操作,如下所示:

// Adding 'Send Expedited email' to action Metabox dropdown in admin order pages
add_filter( 'woocommerce_order_actions', 'filter_wc_add_send_expedited_email_action', 20, 1 );
function filter_wc_add_send_expedited_email_action( $actions ) {
    $actions['send_expedited_email'] = __( 'Send Expedited email', 'woocommerce' );

    return $actions;
}

// Trigger the email notification on 'Send Expedited email' action (composite hook)
add_action( 'woocommerce_order_action_send_expedited_email', 'trigger_action_send_expedited_email', 20, 1 );
function trigger_action_send_expedited_email( $order ) {
    // Get all WC_emails objects instances
    $wc_emails = WC()->mailer()->get_emails();

    // HERE We define our custom email class name
    $class_name = 'WC_Expedited_Order_Email';

    // Send custom email
    if( class_exists($class_name) ) {
        $wc_emails[$class_name]->trigger( $order->get_id() );
    }
}

代码进入您的活动子主题(或活动主题)的 functions.php 文件。已测试并有效。

I didn't really tried with your custom email code, as the custom related templates are missing from your answer code… You should add your complete code to a public Gist on Github and then add the link to your answer (useful for the community).

我补充了:

function sv_wc_process_order_meta_box_action( $order ) {

    // add the order note
    $message = sprintf( __( 'Herinnering verstuurd door: %s.', 'label20-textdomain' ), wp_get_current_user()->display_name );
    $order->add_order_note( $message );

}
add_action( 'woocommerce_order_action_send_expedited_email', 'sv_wc_process_order_meta_box_action' );

记下执行该操作的用户的用户名。