我想在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 );
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 );
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句