我已经创建了一个自定义订单状态和一个自定义电子邮件,但是当订单设置为自定义状态时,似乎无法触发电子邮件。谁能指出我正确的方向?
这是用于创建自定义订单状态并添加电子邮件的代码:
<?php
/**
*Plugin Name: WooCommerce Custom Order Status
*Description: Creates a custom order status in woocommerce and creates a custom email notification.
*/
// Exit if accessed directly
if ( !defined( 'ABSPATH' ) ) exit;
// BEGIN ENQUEUE PARENT ACTION
// AUTO GENERATED - Do not modify or remove comment markers above or below:
// END ENQUEUE PARENT ACTION
// Register New Order Statuses
function wpex_wc_register_post_statuses() {
register_post_status( 'wc-customer-approval', array(
'label' => _x( 'Awaiting Customer Approval', 'WooCommerce Order status', 'text_domain' ),
'public' => true,
'exclude_from_search' => false,
'show_in_admin_all_list' => true,
'show_in_admin_status_list' => true,
'label_count' => _n_noop( 'Approved (%s)', 'Approved (%s)', 'text_domain' )
) );
}
add_filter( 'init', 'wpex_wc_register_post_statuses' );
// Add New Order Statuses to WooCommerce
function wpex_wc_add_order_statuses( $order_statuses ) {
$order_statuses['wc-customer-approval'] = _x( 'Awaiting Customer Approval', 'WooCommerce Order status', 'text_domain' );
return $order_statuses;
}
add_filter( 'wc_order_statuses', 'wpex_wc_add_order_statuses' );
/**
* 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_signature_woocommerce_email( $email_classes ) {
// include our custom email class
require( 'includes/class-wc-signature-email.php' );
// add the email class to the list of email classes that WooCommerce loads
$email_classes['WC_Signature_Email'] = new WC_Signature_Email();
return $email_classes;
}
add_filter( 'woocommerce_email_classes', 'add_signature_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_Signature_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_signature_email';
// this is the title in WooCommerce Email settings
$this->title = 'Signature Email';
// this is the description in WooCommerce email settings
$this->description = 'Signature Email Notification';
// these are the default heading and subject lines that can be overridden using the settings
$this->heading = 'Customer Approval Needed';
$this->subject = 'Customer Approval Needed';
// 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/admin-new-order.php';
$this->template_plain = 'emails/plain/admin-new-order.php';
// Trigger on new paid orders
add_action( 'woocommerce_order_status_pending_to_customer-approval_notification', array( $this, 'trigger' ) );
add_action( 'woocommerce_order_status_on-hold_to_customer-approval_notification', array( $this, 'trigger' ) );
add_action( 'woocommerce_order_status_failed_to_customer-approval_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 );
// 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
*/
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
*/
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 0.1
*/
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',
'html' => 'HTML', 'woocommerce',
'multipart' => 'Multipart', 'woocommerce',
)
)
);
}
} // end \WC_Signature_Email class
到目前为止,我已经设法找到了一些错误,因此我已将代码更新为最新版本。
谢谢, 格里高
答案 0 :(得分:0)
如果在类中使用钩子,则必须使用array( $this, $function )
来传递函数名称,因此:
add_action( 'woocommerce_order_status_changed', array( $this, 'my_awesome_publication_notification' ) );
请参见Codex