简体   繁体   English

根据产品变体术语将收件人添加到 Woocommerce 电子邮件通知

[英]Adding recipients to Woocommerce email notifications based on product variation term

I have created a Woocommerce plugin and require it to do two things:我创建了一个 Woocommerce 插件并要求它做两件事:

  1. Send a notification message to a specific email address, based on which product variation is in the cart.根据购物车中的产品变体,向特定电子邮件地址发送通知消息。

  2. The email must contain only the relevant product, and not products that contain other attributes.电子邮件必须仅包含相关产品,而不能包含包含其他属性的产品。

For example:例如:

Product A has an Attribute named Chef, with chef-one and chef-two as variable Terms.产品 A 有一个名为 Chef 的属性,其中主厨一和主厨二作为变量项。 The user may select Product A from chef-one or chef-two.用户可以从厨师一或厨师二中选择产品A。

If the user selects Product A from chef-one, a notification email must be sent to chefone@email.com containing the name of the product ordered (as it would show up in a regular Woocommerce notification email).如果用户从chef-one 中选择产品A,则必须向chefone@email.com 发送一封通知电子邮件,其中包含所订购产品的名称(因为它会显示在常规的Woocommerce 通知电子邮件中)。

If the user selects Product A from chef-one and Product B from chef-two, a notification email must be sent to chef-one containing only Product A, and a notification email must be sent to chef-two containing only Product B.如果用户选择了大厨一的产品 A 和大厨二的产品 B,则必须向大厨一发送仅包含产品 A 的通知电子邮件,并且必须向大厨二发送仅包含产品 B 的通知电子邮件。

I have created the plugin using the tutorial found on https://www.skyverge.com/blog/how-to-add-a-custom-woocommerce-email/ and have adapted it to suit the above purpose.我使用https://www.skyverge.com/blog/how-to-add-a-custom-woocommerce-email/上的教程创建了插件,并对其进行了调整以适应上述目的。

I have also adapted code found the following solutions: Adding a custom woocommerce email based on the product attribute Woocommerce - Need to send email to specific address based on zip code我还修改了代码,找到了以下解决方案: 添加基于产品属性Woocommerce 的自定义 woocommerce 电子邮件- 需要根据邮政编码将电子邮件发送到特定地址

Here is the code from my plugin's class file:这是我的插件类文件中的代码:

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 = "info@email.com";
                $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' ),
            )
        )
    );
}

This is as close as I am able to get it, but it is not working.这是我所能得到的,但它不起作用。 I suspect the problem lies somewhere around the line that says "Determine which email address to send to, based on Product Attribute Term".我怀疑问题出在“根据产品属性术语确定要发送到哪个电子邮件地址”这一行的某个地方。 The plugin was loading until I added that section.插件正在加载,直到我添加了该部分。

Is the function supposed to be in a separate plugin file?该功能是否应该在单独的插件文件中?

I also need help in getting the email to contain only the information relevant to the vendor it is being sent to.我还需要帮助让电子邮件只包含与它被发送到的供应商相关的信息。

Any assistance in getting this plugin to work would be greatly appreciated.非常感谢让这个插件工作的任何帮助。

The filter new_order doesn't exist in WooCommerce (or in your code) WooCommerce (或您的代码)中不存在过滤器new_order

The correct filter hook (located in WC_Email class core code, line 269) is this one:正确的过滤器钩子(位于WC_Email类核心代码,第 269 行)是这样的:

$recipient  = apply_filters( 'woocommerce_email_recipient_' . $this->id, $this->recipient, $this->object );

In this hook, $this->id is ' new_order ' for you.在这个钩子中, $this->id对你来说是' new_order '。

There is big errors in your code:你的代码有很大的错误:

  • The term name should be something like "one" or "chef-one" , but absolutely not "pa_chef-one" , as "pa_chef" is the taxonomy slug for your attribute "Chef".术语名称应该类似于"one""chef-one"但绝对不是"pa_chef-one" ,因为"pa_chef"是属性 "Chef" 的分类标头。
  • The multiple email recipients are not in an array, but in a coma separated string.多个电子邮件收件人不在数组中,而是在逗号分隔的字符串中。

So the correct code should be something like:所以正确的代码应该是这样的:

add_filter( 'woocommerce_email_recipient_new_order', 'add_recipient', 10, 2 );
function add_recipient( $recipient, $order )
{
    if ( ! is_a( $order, 'WC_Order' ) ) return $recipient;

    // Additional email recipient
    $additional_email = "info@email.com";

    // The term name "pa_chef-one" is very strange … It should be "one" or "chef-one" (may be)
    $term_slug = "one";

    $has_term = false;

    // Iterating through each order item
    foreach ($order->get_items() as $item_id => $item_obj) {
        $variation_id = $item_obj->get_variation_id();
        $variation_obj = wc_get_product($variation_id);
        $variation_attributes = $variation_obj->get_attributes();
        foreach( $variation_attributes as $taxonomy_key => $term_value ){

            if( $taxonomy_key == "pa_chef" && $term_value == $term_slug ){
                $recipient .= ','. $additional_email;
                $has_term = true;
                break; // stop the 2nd loop
            }
        }
        if( $has_term ) break; // stop the 1st loop
    }
    return $recipient;
}

Code goes in function.php file of your active child theme (or theme) or also in any plugin file.代码位于活动子主题(或主题)的 function.php 文件或任何插件文件中。

Made for WooCommerce version 3+专为 WooCommerce 版本 3+


Similar answers:类似的回答:

Add the New order email notification attachment to the vendor email 将新订单电子邮件通知附件添加到供应商电子邮件

Woocommerce email notification recipient conditionally based on custom field 基于自定义字段有条件地 Woocommerce 电子邮件通知收件人

WooCommerce email notifications: different email recipient for different cities WooCommerce 电子邮件通知:不同城市的不同电子邮件收件人

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 WooCommerce 电子邮件通知中基于产品类别的不同收件人 - Different recipients based on product category in WooCommerce email notification 显示 Woocommerce 中所选变体的产品属性术语 - Display the product attribute term for the selected variation in Woocommerce 添加基于产品属性的自定义 woocommerce 电子邮件 - Adding a custom woocommerce email based on the product attribute 根据产品类别将 email 附件添加到 WooCommerce 通知 - Add an email attachment to WooCommerce notifications based on product category WooCommerce:将产品版本说明添加到主页 - WooCommerce: Adding product variation description to homepage 将后缀添加到WooCommerce产品变体会被覆盖 - Adding postfix to WooCommerce product variation gets overwritten Woocommerce:使用现有属性为现有产品添加变体 - Woocommerce: Adding a variation to existing product with existing attributes WooCommerce 产品变化价格基于自定义字段 - WooCommerce product variation price based on custom fields Woocommerce电子邮件模板:将产品变化权重添加到电子邮件 - Woocommerce email template: Add Product Variation Weight to email 在 Woocommerce 管理产品变体中添加基于产品类别的下拉菜单 - Add a dropdown based on product categories in Woocommerce admin product variation
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM