根据 Woocommerce 中的产品类别按项目自定义电子邮件通知
Posted
技术标签:
【中文标题】根据 Woocommerce 中的产品类别按项目自定义电子邮件通知【英文标题】:Custom email notification by item based on product category in Woocommerce 【发布时间】:2019-04-09 12:07:25 【问题描述】:我开发了一个自定义代码,它可以根据客户购买的产品类别向他们发送电子邮件。我的网站的问题是一种产品有很多类别。该脚本工作正常,除了它发送的电子邮件数量等于产品的类别数量。我希望每个产品只发送一封电子邮件。
代码如下:
add_action("woocommerce_order_status_changed", "wcepp_notification_email");
function wcepp_notification_email( $order_id, $checkout = null )
global $woocommerce;
$order = new WC_Order( $order_id );
if($order->status === 'processing' )
function wcepp_get_custom_email_html( $order, $heading = false, $mailer, $cat_slug )
$template = 'emails/woo-custom-emails-per-product-'.$cat_slug.'.php';
return wc_get_template_html( $template, array(
'order' => $order,
'email_heading' => $heading,
'sent_to_admin' => true,
'plain_text' => false,
'email' => $mailer
)
);
$liquify_array = array('cutting-agent', 'dabjuice-liquify');
$terpene_array = array('terpene-blends', 'strain-specific', 'hybrid', 'indica', 'sativa', 'terpene-sample-kits', 'hybrid-sample-kit', 'indica-sample-kit', 'sativa-sample-kit');
$isolates_array = array('raw-terpene-isolates', 'alpha', 'beta');
// getting the order products
$items = $order->get_items();
// let's loop through each of the items
foreach ( $items as $item )
$categories = get_the_terms( $item['product_id'] , 'product_cat' );
foreach( $categories as $categorie )
$cat_slug = $categorie->slug;
if(in_array($cat_slug, $liquify_array))
$new_cat_slug = 'liquify';
elseif(in_array($cat_slug, $terpene_array))
$new_cat_slug = 'terpenes';
elseif(in_array($cat_slug, $isolates_array))
$new_cat_slug = 'isolates';
if(isset($new_cat_slug))
// Create a mailer
$mailer = $woocommerce->mailer();
//Prepare Email
$recipient = $order->billing_email;
if($new_cat_slug == 'liquify')
$subject = 'Instructions for usage of your Liquify Product';
elseif($new_cat_slug == 'terpenes')
$subject = 'Instructions for usage of your Terpenes Product';
elseif($new_cat_slug == 'isolates')
$subject = 'Instructions for usage of your Raw Terpenes Isolates';
$content = wcepp_get_custom_email_html( $order, $subject, $mailer, $new_cat_slug );
$headers = "Content-Type: text/html\r\n";
//send the email through wordpress
$mailer->send( $recipient, $subject, $content, $headers );
我们将不胜感激。
【问题讨论】:
如果您可以添加自定义电子邮件模板的代码,它应该对其他人有用。谢谢。 我的自定义电子邮件模板是一个 HTML 文件,其中仅包含产品的文本说明。 请在您的问题中添加它。 【参考方案1】:您的代码自 Woocommerce 3 以来已经过时并且有很多错误。如果您在 Wordpress 上启用调试,您将收到很多错误消息。请尝试以下方法,当每个产品与定义的产品类别匹配时,它会为每个产品发送一封电子邮件:
// Get email content from the custom template
function get_custom_email_content_html( $order, $heading = false, $mailer, $category_slug )
$template = 'emails/woo-custom-emails-per-product-'.$category_slug.'.php';
return wc_get_template_html( $template, array(
'order' => $order,
'email_heading' => $heading,
'sent_to_admin' => true,
'plain_text' => false,
'email' => $mailer
)
);
// Send a custom email notification for each order item on order processing status change
add_action( 'woocommerce_order_status_changed', 'processing_custom_email_notification', 10, 4 );
function processing_custom_email_notification( $order_id, $status_from, $status_to, $order )
if( $status_to === 'processing' )
$liquify_array = array('cutting-agent', 'dabjuice-liquify');
$terpene_array = array('terpene-blends', 'strain-specific', 'hybrid', 'indica', 'sativa', 'terpene-sample-kits', 'hybrid-sample-kit', 'indica-sample-kit', 'sativa-sample-kit');
$isolates_array = array('raw-terpene-isolates', 'alpha', 'beta');
// Loop through order items
foreach ( $order->get_items() as $item )
$product_category = '';
$categories_slugs = get_the_terms( $item->get_product_id() , 'product_cat', array( 'fields' => 'slugs' ) );
foreach( $categories_slugs as $term_slug )
if( in_array( $term_slug, $liquify_array ) )
$product_category = 'liquify';
break;
elseif(in_array( $term_slug, $terpene_array ) )
$product_category = 'terpenes';
break;
elseif(in_array( $term_slug, $isolates_array ) )
$product_category = 'isolates';
break;
if( isset( $product_category ) )
// Email subject
if( $product_category == 'liquify' )
$subject = __("Instructions for usage of your Liquify Product");
elseif($product_category == 'terpenes' )
$subject = __("Instructions for usage of your Terpenes Product");
elseif( $product_category == 'isolates' )
$subject = __("Instructions for usage of your Raw Terpenes Isolates");
$mailer = WC()->mailer(); // Get an instance of the WC_Emails Object
$recipient = $order->get_billing_email(); // Email recipient
$content = get_custom_email_content_html( $order, $subject, $mailer, $product_category ); // Email content (template)
$headers = "Content-Type: text/html\r\n"; // Email headers
// Send the email
WC()->mailer()->send( $recipient, $subject, $content, $headers );
它现在应该可以更好地工作了(但未针对您的产品类别进行测试)。
3 个自定义电子邮件模板应位于
woocommerce
文件夹 >emails
子文件夹(活动主题):woo-custom-emails-per-product-liquify.php
woo-custom-emails-per-product-terpenes.php
woo-custom-emails-per-product-isolates.php
【讨论】:
感谢您的帮助,但如果订单包含所有主要类别的产品,它会发送所有三封电子邮件吗? @ShahidKhattak 您在问题中要求根据您定义的产品类别按产品(订单项)发送电子邮件通知。我的答案代码将采用第一个匹配的产品类别(它定义了主题,当然还有内容),并将按产品发送一封电子邮件(如果有匹配的产品类别)。 所以它按产品发送 1 封电子邮件。 此代码正在发送带有错误消息的电子邮件,消息在这里 注意:wc_get_template 调用不正确。 /home2/amayfield/test.adriennemayfield.com/wp-content/plugins/woocommerce/templates/emails/woo-custom-emails-per-product-.php 不存在。回溯:edit_post、wp_update_post、wp_insert_post、do_action('save_post')、WP_Hook->do_action、WP_Hook->apply_filters、WC_Admin_Meta_Boxes->save_meta_boxes、do_action('woocommerce_process_shop_order_meta')、WP_Hook->do_action、WP_Hook->apply_filters、WC_Meta_Boxes保存,WC_Order->保存,WC_Order->status_transition,do_action('woocommerce_order_status_changed'),WP_Hook->do_action, 无法复制完整的错误信息,但您可以清楚地看到它没有在电子邮件模板名称中添加该类别的slug后缀。以上是关于根据 Woocommerce 中的产品类别按项目自定义电子邮件通知的主要内容,如果未能解决你的问题,请参考以下文章
相关产品仅限于WooCommerce 3中的标签,而不是类别