首页 > 解决方案 > 在 WooCommerce 电子邮件通知中添加基于产品标签的自定义消息

问题描述

我想在 WooCommerce 的“订单完成”电子邮件中添加唯一文本,但仅当购买的产品具有特定产品标签时。

如果购买的商品带有“筹款活动”标签,我希望电子邮件中包含感谢他们支持我们的筹款活动的文字。长话短说,但我们不能为此使用类别,我们必须使用标签。

我已经尝试编辑 customer-complete-order.php 文件并且这个文件有效,但是我不擅长 PHP 代码并且没有如何调用标签的示例。

我还尝试根据其他人所做的编辑functions.php,这就是我所得到的。

function woocommerce_custom_email_per_product_depending_on_product_tag( $email_heading, $order ) {
global $woocommerce;
$items = $order->get_items();
foreach ( $items as $item ) {
    $product_tag = $item['product_tag'];
    if ( $product_tag == fundraiser ) {
        $email_body = 'Thanks for buying a fundraiser.';
    }
    return $email_heading;
}   }

或者在新的电子邮件模板中定义这个自定义文本可能会更好?这是 woocommerce 提供的订单确认模板。

<?php
/**
 * Customer completed order email
 *
 * This template can be overridden by copying it to yourtheme/woocommerce/emails/customer-completed-order.php.
 *
 * HOWEVER, on occasion WooCommerce will need to update template files and you
 * (the theme developer) will need to copy the new files to your theme to
 * maintain compatibility. We try to do this as little as possible, but it does
 * happen. When this occurs the version of the template file will be bumped and
 * the readme will list any important changes.
 *
 * @see https://docs.woocommerce.com/document/template-structure/
 * @package WooCommerce/Templates/Emails
 * @version 3.5.0
 */

if ( ! defined( 'ABSPATH' ) ) {
    exit;
}

/*
 * @hooked WC_Emails::email_header() Output the email header
 */
do_action( 'woocommerce_email_header', $email_heading, $email ); ?>

<?php /* translators: %s: Customer first name */ ?>
<p><?php printf( esc_html__( 'Hi %s,', 'woocommerce' ), esc_html( $order->get_billing_first_name() ) ); ?></p>
<?php /* translators: %s: Site title */ ?>
<p><?php esc_html_e( 'We have finished processing your order.', 'woocommerce' ); ?></p>
<?php

/*
 * @hooked WC_Emails::order_details() Shows the order details table.
 * @hooked WC_Structured_Data::generate_order_data() Generates structured data.
 * @hooked WC_Structured_Data::output_structured_data() Outputs structured data.
 * @since 2.5.0
 */
do_action( 'woocommerce_email_order_details', $order, $sent_to_admin, $plain_text, $email );

/*
 * @hooked WC_Emails::order_meta() Shows order meta data.
 */
do_action( 'woocommerce_email_order_meta', $order, $sent_to_admin, $plain_text, $email );

/*
 * @hooked WC_Emails::customer_details() Shows customer details
 * @hooked WC_Emails::email_address() Shows email address
 */
do_action( 'woocommerce_email_customer_details', $order, $sent_to_admin, $plain_text, $email );

?>
<p>
<?php esc_html_e( 'Thanks for shopping with us.', 'woocommerce' ); ?>
</p>
<?php

/*
 * @hooked WC_Emails::email_footer() Output the email footer
 */
do_action( 'woocommerce_email_footer', $email );

标签: wordpresswoocommercetagsproductemail-notifications

解决方案


  • 无需覆盖模板文件,可以使用woocommerce_email_before_order_table钩子
  • 通过$email->id您可以定位所需的电子邮件

所以你得到:

function woocommerce_custom_email_per_product_depending_on_product_tag( $order, $sent_to_admin, $plain_text, $email ) {
    $items = $order->get_items();
    
    foreach ( $items as $item ) {               
        // get an array of the WP_Term objects for a defined product ID
        $terms = wp_get_post_terms( $item['product_id'], 'product_tag' );
        
        foreach($terms as $term){
            $term_names[] = $term->name; // Product tag Name
            
            //$term->term_id; Product tag Id
            //$term->slug; Product tag slug
        }
    }
    
    if ( $email->id == 'customer_processing_order' && in_array('Fundraiser', $term_names) ) {
        echo 'Thanks for buying a fundraiser.';
    }
}
add_action( 'woocommerce_email_before_order_table', 'woocommerce_custom_email_per_product_depending_on_product_tag', 10, 4 );

推荐阅读