我目前正在开发一个简单的插件,可以创建自定义订单状态和自定义电子邮件 - 当订单状态从“待定”状态发生变化时,会发送电子邮件。到'处理'因为我的插件中的要点是我可以在付款后发送需要由客户签名的附件。
目前,自定义订单状态有效且电子邮件已发送,但我无法弄清楚如何将附件上传到WooCommerce / WordPress,然后将其附加到电子邮件本身。
我的代码相对简单,目前只包含两个文件,如下所示:
woocommerce法律-documents.php
<?php
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_legal_documents_woocommerce_email( $email_classes ) {
// include our custom email class
require_once( 'includes/class-wc-legal-documents.php' );
// add the email class to the list of email classes that WooCommerce loads
$email_classes['WC_Legal_Documents_Email'] = new WC_Legal_Documents_Email();
return $email_classes;
}
add_filter( 'woocommerce_email_classes', 'add_legal_documents_woocommerce_email' );
/**
* Register new status
**/
function register_legal_documents_status() {
register_post_status( 'wc-legal', array(
'label' => 'Legal',
'public' => true,
'exclude_from_search' => false,
'show_in_admin_all_list' => true,
'show_in_admin_status_list' => true,
'label_count' => _n_noop( 'Legal <span class="count">(%s)</span>', 'Legal <span class="count">(%s)</span>' )
) );
}
add_action( 'init', 'register_legal_documents_status' );
// Add to list of WC Order statuses
function add_legal_to_order_statuses( $order_statuses ) {
$new_order_statuses = array();
// add new order status after processing
foreach ( $order_statuses as $key => $status ) {
$new_order_statuses[ $key ] = $status;
if ( 'wc-processing' === $key ) {
$new_order_statuses['wc-legal'] = 'Legal';
}
}
return $new_order_statuses;
}
add_filter( 'wc_order_statuses', 'add_legal_to_order_statuses' );
/**
* Adds icons for any custom order statuses
**/
add_action( 'wp_print_scripts', 'add_custom_order_status_icon' );
function add_custom_order_status_icon() {
if( ! is_admin() ) {
return;
}
?> <style>
/* Add custom status order icons */
.column-order_status mark.legal:after {
content: "\e004";
font-family: WooCommerce;
color: #ffba00;
font-weight: 400;
font-variant: normal;
text-transform: none;
line-height: 1;
-webkit-font-smoothing: antialiased;
margin: 0;
text-indent: 0;
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
text-align: center;
}
/* Repeat for each different icon; tie to the correct status */
</style> <?php
}
//enqueues our external font awesome stylesheet
function enqueue_our_required_stylesheets(){
wp_enqueue_style('woocommerce-icons', 'includes/style.css');
}
add_action('wp_enqueue_scripts','enqueue_our_required_stylesheets');
包括/类-WC-法律-documents.php
<?php
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
/**
* A custom Expedited Order WooCommerce Email class
*
* @since 0.1
* @extends \WC_Email
*/
class WC_Legal_Documents_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_legal_documents';
// this is the title in WooCommerce Email settings
$this->title = 'Legal Documents';
// this is the description in WooCommerce email settings
$this->description = 'Legal Document email is sent to the customer with an attachment of the required document.';
// these are the default heading and subject lines that can be overridden using the settings
$this->heading = 'Legal Documents';
$this->subject = 'Legal Documents';
// default message to add to the email
//$this->message = '<p>Hello,<br><br> please see attached the legal documentation in which requires your attention.</p>';
// 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-invoice.php';
$this->template_plain = 'emails/plain/customer-invoice.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' ) );
// Add message
add_action( 'woocommerce_email_before_order_table', array( $this, 'add_order_email_instructions' ) );
// Call parent constructor to load any other defaults not explicity defined here
parent::__construct();
// this gets the email attachments
$this->attachment = $this->get_option( 'attachment' );
// this gets the email message
$this->message = $this->get_option( 'message' );
}
public function add_order_email_instructions( $order, $sent_to_admin ) {
if ( ! $sent_to_admin ) {
//echo '<p><strong>Instructions:</strong> Full payment is due immediately upon delivery: <em>cash only, no exceptions</em>.</p>';
echo $this->message;
}
}
/**
* 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 );
$this->recipient = $this->object->billing_email;
$this->object->update_status('wc-legal');
// 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'
),
'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' => ''
),
'message' => array(
'title' => 'Message',
'type' => 'textarea',
'description' => 'Add a custom message to the customer.',
'placeholder' => '',
'default' => ''
),
'attachment' => array(
'title' => 'Attachment',
'type' => 'file',
'description' => 'Select the legal document to be sent as an attachment in the email.',
'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
此外,自定义消息将添加到发送的所有电子邮件中,但我只需要将其添加到此电子邮件中,但我还没有找到这样做的方法 - 任何帮助将不胜感激。