自定义状态触发自定义电子邮件

时间:2019-08-24 06:36:57

标签: php wordpress woocommerce

我创建了一个名为“ Shipped”的自定义触发器,该触发器在我的选项列表中似乎非常有效。

function add_new_order_woocommerce_email( $email_classes ) {

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

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

    return $email_classes;

}
add_filter( 'woocommerce_email_classes', 'add_new_order_woocommerce_email' );

function register_shipped_order_status() {
    register_post_status( 'wc-shipped', array(
        'label'                     => 'Shipped',
        'public'                    => true,
        'exclude_from_search'       => false,
        'show_in_admin_all_list'    => true,
        'show_in_admin_status_list' => true,
        'label_count'               => _n_noop( 'Shipped <span class="count">(%s)</span>', 'Shipped <span class="count">(%s)</span>' )
    ) );
}
add_action( 'init', 'register_shipped_order_status' );

function add_new_to_order_statuses( $order_statuses ) {
    $new_order_statuses = array();

    foreach ( $order_statuses as $key => $status ) {
        $new_order_statuses[ $key ] = $status;
        if ( 'wc-processing' === $key ) {
            $new_order_statuses['wc-shipped'] = 'Shipped';
        }
    }
    return $new_order_statuses;
}
add_filter( 'wc_order_statuses', 'add_new_to_order_statuses' );

在状态更改为上面提供的自定义状态时,尝试获取此电子邮件来触发时遇到了问题。我在互联网上搜索了多个示例以及可能解决我的问题的方法,但找不到任何有帮助的东西。

代码中的电子邮件模板已创建,并且位于正确的目录位置。

<?php

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

/**
 * A custom Shipped Order WooCommerce Email class
 *
 * @since 0.1
 * @extends \WC_Email
 */
class WC_Shipped_Order_Email extends WC_Email {

    public function __construct() {

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

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

        // defines the email for customers only
        $this->customer_email = true;

        // this is the description in WooCommerce email settings
        $this->description = 'Shipped order notification emails are sent when a customers order has been shipped.';

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

        // 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-shipped-order.php';
        $this->template_plain = 'emails/plain/customer-shipped-order.php';

        // Trigger on new paid orders
        add_action( 'woocommerce_order_status_pending_to_shipped', array( $this, 'trigger' ) );
        add_action( 'woocommerce_order_status_processing_to_shipped',  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 );

        // 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 email subject.
     *
     * @since  3.1.0
     * @return string
     */
    public function get_default_subject() {
        return __( 'Your order has been shipped', 'woocommerce' );
    }

    /**
     * Get email heading.
     *
     * @since  3.1.0
     * @return string
     */
    public function get_default_heading() {
        return __( 'Thanks for shopping with us', 'woocommerce' );
    }

    /**
     * Get content html.
     *
     * @return string
     */
    public function get_content_html() {
        return wc_get_template_html(
            $this->template_html, array(
                'order'         => $this->object,
                'email_heading' => $this->get_heading(),
                'sent_to_admin' => false,
                'plain_text'    => false,
                'email'         => $this,
            )
        );
    }

    /**
     * Get content plain.
     *
     * @return string
     */
    public function get_content_plain() {
        return wc_get_template_html(
            $this->template_plain, array(
                'order'         => $this->object,
                'email_heading' => $this->get_heading(),
                'sent_to_admin' => false,
                'plain_text'    => true,
                'email'         => $this,
            )
        );
    }

    /**
     * Initialise settings form fields.
     */
    public function init_form_fields() {
        $this->form_fields = array(
            'enabled'    => array(
                'title'   => __( 'Enable/Disable', 'woocommerce' ),
                'type'    => 'checkbox',
                'label'   => __( 'Enable this email notification', 'woocommerce' ),
                'default' => 'yes',
            ),
            'subject'    => array(
                'title'       => __( 'Subject', 'woocommerce' ),
                'type'        => 'text',
                'desc_tip'    => true,
                /* translators: %s: list of placeholders */
                'description' => sprintf( __( 'Available placeholders: %s', 'woocommerce' ), '<code>{site_title}, {order_date}, {order_number}</code>' ),
                'placeholder' => $this->get_default_subject(),
                'default'     => '',
            ),
            'heading'    => array(
                'title'       => __( 'Email heading', 'woocommerce' ),
                'type'        => 'text',
                'desc_tip'    => true,
                /* translators: %s: list of placeholders */
                'description' => sprintf( __( 'Available placeholders: %s', 'woocommerce' ), '<code>{site_title}, {order_date}, {order_number}</code>' ),
                'placeholder' => $this->get_default_heading(),
                'default'     => '',
            ),
            'email_type' => array(
                'title'       => __( 'Email type', 'woocommerce' ),
                'type'        => 'select',
                'description' => __( 'Choose which format of email to send.', 'woocommerce' ),
                'default'     => 'html',
                'class'       => 'email_type wc-enhanced-select',
                'options'     => $this->get_email_type_options(),
                'desc_tip'    => true,
            ),
        );
    }


} // end \WC_Shipped_Order_Email class

function fabricdeluxe_shipped_woocommerce_email_actions( $actions ){
    $actions[] = 'woocommerce_order_status_shipped';
    return $actions;
}
add_filter( 'woocommerce_email_actions', 'fabricdeluxe_shipped_woocommerce_email_actions' );

很多这样的编码是通过其他来源获得的,并且可以根据我的应用进行定制,但是很长一段时间以来,我一直坚持这个问题,所以我还没有决定使用这个出色的网站来获得关于我所学内容的专家意见。可能做错了,或者在哪里可以找到解决方案。 (我更喜欢学习自己,但是我的大脑只能花很多时间才能变得难以解决哈哈!)

非常感谢您的帮助!

0 个答案:

没有答案