根据产品变化条款将收件人添加到Woocommerce电子邮件通知中 [英] Adding recipients to Woocommerce email notifications based on product variation term

查看:54
本文介绍了根据产品变化条款将收件人添加到Woocommerce电子邮件通知中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个Woocommerce插件,并要求它做两件事:

I have created a Woocommerce plugin and require it to do two things:

  1. 根据购物车中的产品变化,将通知消息发送到特定的电子邮件地址.

  1. Send a notification message to a specific email address, based on which product variation is in the cart.

电子邮件必须仅包含相关产品,而不能包含包含其他属性的产品.

The email must contain only the relevant product, and not products that contain other attributes.

例如:

产品A具有一个名为Chef的属性,其中Chef-1和Chef-2作为可变术语.用户可以从一名厨师或两名厨师中选择产品A.

Product A has an Attribute named Chef, with chef-one and chef-two as variable Terms. The user may select Product A from chef-one or chef-two.

如果用户从厨师一中选择产品A,则必须将包含订购产品名称的通知电子邮件发送到chefone@email.com(因为它将显示在常规的Woocommerce通知电子邮件中).

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).

如果用户从一位厨师中选择产品A,从两位厨师中选择产品B,则必须将通知电子邮件发送给仅包含产品A的一位厨师,并且将通知电子邮件发送给仅包含产品A的厨师2.B.

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.

我已使用

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.

我还修改了代码,找到以下解决方案:根据产品属性添加自定义woocommerce电子邮件 Woocommerce-需要将电子邮件发送到特定基于邮政编码的地址

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

这是我插件的类文件中的代码:

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.

推荐答案

过滤器 new_order 在WooCommerce中不存在 (或您的代码中)

正确的过滤器挂钩(位于 WC_Email 类核心代码,第269行)是这样的:

The correct filter hook (located in WC_Email class core code, line 269) is this one:

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

在此挂钩中, $ this-> id 对您来说是" new_order ".

您的代码中存在重大错误:

There is big errors in your code:

  • 术语名称应类似于"one" "chef-one" 但绝对不是 "pa_chef-一个" ,因为 "pa_chef" 是您的属性"Chef"的分类标准.
  • 多个电子邮件收件人不在数组中,而是用逗号分隔的字符串.

因此正确的代码应类似于:

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;
}

代码会出现在您活动的子主题(或主题)的function.php文件中,也可能会出现在任何插件文件中.

Code goes in function.php file of your active child theme (or theme) or also in any plugin file.

适用于WooCommerce版本3 +

Made for WooCommerce version 3+

类似的答案:

添加新订购供应商电子邮件的电子邮件通知附件

Woocommerce电子邮件通知收件人有条件地基于自定义字段

WooCommerce电子邮件通知:不同的电子邮件收件人针对不同的城市

这篇关于根据产品变化条款将收件人添加到Woocommerce电子邮件通知中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆