我创建了一个 Woocommerce 插件并要求它做两件事:
根据购物车中的产品变体,向特定电子邮件地址发送通知消息。
电子邮件必须仅包含相关产品,不得包含其他属性的产品。
例如:
产品 A 具有名为 Chef 的属性,其中 Chef-one 和 Chef-two 作为变量术语。用户可以从厨师一或厨师二中选择产品A。
如果用户从 Chef-one 选择产品 A,则必须将通知电子邮件发送至[电子邮件受保护] /cdn-cgi/l/email-protection包含所订购产品的名称(因为它会显示在常规 Woocommerce 通知电子邮件中)。
如果用户从厨师一中选择产品 A,从厨师二中选择产品 B,则必须向厨师一发送一封仅包含产品 A 的通知电子邮件,并且必须向厨师二发送一封仅包含产品 B 的通知电子邮件。
我已经使用上找到的教程创建了该插件https://www.skyverge.com/blog/how-to-add-a-custom-woocommerce-email/ https://www.skyverge.com/blog/how-to-add-a-custom-woocommerce-email/并对其进行了调整以适应上述目的。
我还修改了代码找到了以下解决方案:根据产品属性添加自定义 woocommerce 电子邮件 https://stackoverflow.com/questions/40633541/adding-a-custom-woocommerce-email-based-on-the-product-attribute
Woocommerce - 需要根据邮政编码将电子邮件发送到特定地址 https://stackoverflow.com/questions/26429482/woocommerce-need-to-send-email-to-specific-address-based-on-zip-code
这是我的插件类文件中的代码:
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
/**
* A custom WAKIKI Order WooCommerce Email class
*
* @since 0.1
* @extends \WC_Email
*/
class WC_Wakiki_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_wakiki_order';
// this is the title in WooCommerce Email settings
$this->title = 'WAKIKI Order';
// this is the description in WooCommerce email settings
$this->description = 'WAKIKI Order Notification emails are sent when a customer places an order on the website';
// these are the default heading and subject lines that can be overridden using the settings
$this->heading = 'WAKIKI Delivery Order';
$this->subject = 'WAKIKI Delivery Order';
// 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_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;
$order = new WC_Order( $order_id );
// Find the product_id
$items = $order->get_items();
foreach ( $items as $item ) {
$product_id = $item['product_id'];
}
// From the product_id get the product attribute
$product = new WC_Product( $product_id ); // create an object of WC_Product class
$patt = $product->get_attributes(); // call get_attributes method
// Condition valid to send the email (if the attributes is chef)
if ( array_key_exists('pa_chef', $patt) )
// Determine which email address to send to, based on Product Attribute Term)
add_filter( 'new_order' , 'add_recipient', 20, 2 );
function add_recipient( $email, $order ) {
$additional_email = "[email protected] /cdn-cgi/l/email-protection";
$terms = get_terms("pa_chef");
if( $order->$term->name == "pa_chef-one" ){
$email = explode( ',', $email );
array_push( $email, $additional_email );
}
return $email;
}
{
// Send the email
// Setup order object
$this->object = new WC_Order( $order_id );
$this->recipient = $this->object->billing_email;
// 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;
// Send the email!
$this->send( $this->get_recipient(), $this->get_subject(), $this->get_content(), $this->get_headers(), $this->get_attachments() );
}
else
{
return; //do nothing if there is not chef attribute
}
}
/**
* 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' ),
)
)
);
}
这是我所能得到的最接近的结果,但它不起作用。我怀疑问题出在“根据产品属性术语确定要发送到哪个电子邮件地址”这一行。该插件一直在加载,直到我添加该部分。
该函数应该位于单独的插件文件中吗?
我还需要帮助让电子邮件仅包含与其发送到的供应商相关的信息。
任何使该插件正常工作的帮助将不胜感激。