gpt4 book ai didi

php - WooCommerce 电子邮件通知中基于产品类别的不同收件人

转载 作者:行者123 更新时间:2023-12-04 02:53:19 26 4
gpt4 key购买 nike

我正在为一所销售虚拟产品(费用和远足费用)和实体(制服)的学校建立一个网站,但是他们希望将每个类别的订单通知发送给不同的接收者,因为它们由不同的部门处理.

例如,所有统一类别的订单都转到收件人一,而所有其他类别的订单都转到收件人二...我遇到过多种方法可以根据产品发送自定义电子邮件,但是这些方法都不符合我的要求。

我还希望能够在不使用 Woocommerce Advanced Notifications 之类的插件的情况下做到这一点。

对此的任何帮助将不胜感激。

最佳答案

以下将根据您的产品类别向“新订单”电子邮件通知添加其他收件人,您将在带有电子邮件收件人/产品类别对的索引数组中定义这些收件人:

add_filter( 'woocommerce_email_recipient_new_order', 'custom_email_recipient_new_order', 10, 2 );
function custom_email_recipient_new_order( $recipient, $order ) {
// Not in backend when using $order (avoiding an error)
if( ! is_a($order, 'WC_Order') ) return $recipient;

// Define the email recipients / categories pairs in the array
$recipients_categories = array(
'email.one@email.com' => 'category-one',
'email.two@email.com' => 'category-two',
'email.three@email.com' => 'category-three',
);

// Loop through order items
foreach ( $order->get_items() as $item ) {
// Loop through defined product categories
foreach ( $recipients_categories as $email => $category ) {
if( has_term( $category, 'product_cat', $item->get_product_id() ) && strpos($recipient, $email) === false ) {
$recipient .= ',' . $email;
}
}
}
return $recipient;
}
代码位于事件子主题(或事件主题)的 functions.php 文件中。测试和工作。

Notes:

  • The defined Product categories can be term Ids, term slugs or term names.
  • Each product category need to be defined in the related products as has_term() WordPress conditional function doesn't handle parent terms.


###Addition 处理父产品类别:
// Custom conditional function that handle parent product categories too
function has_product_categories( $categories, $product_id = 0 ) {
$parent_term_ids = $categories_ids = array(); // Initializing
$taxonomy = 'product_cat';
$product_id = $product_id == 0 ? get_the_id() : $product_id;

if( is_string( $categories ) ) {
$categories = (array) $categories; // Convert string to array
}

// Convert categories term names and slugs to categories term ids
foreach ( $categories as $category ){
$result = (array) term_exists( $category, $taxonomy );
if ( ! empty( $result ) ) {
$categories_ids[] = reset($result);
}
}

// Loop through the current product category terms to get only parent main category term
foreach( get_the_terms( $product_id, $taxonomy ) as $term ){
if( $term->parent > 0 ){
$parent_term_ids[] = $term->parent; // Set the parent product category
$parent_term_ids[] = $term->term_id; // (and the child)
} else {
$parent_term_ids[] = $term->term_id; // It is the Main category term and we set it.
}
}
return array_intersect( $categories_ids, array_unique($parent_term_ids) ) ? true : false;
}

// Adding custom recipients based on product categories
add_filter( 'woocommerce_email_recipient_new_order', 'custom_email_recipient_new_order', 10, 2 );
function custom_email_recipient_new_order( $recipient, $order ) {
// Not in backend when using $order (avoiding an error)
if( ! is_a($order, 'WC_Order') ) return $recipient;

// Define the email recipients / categories pairs in the array
$recipients_categories = array(
'email.one@email.com' => 'category-one',
'email.two@email.com' => 'category-two',
'email.three@email.com' => 'category-three',
);

// Loop through order items
foreach ( $order->get_items() as $item ) {
// Loop through defined product categories
foreach ( $recipients_categories as $email => $category ) {
if( has_product_categories( $item->get_product_id(), array( $category ) ) && strpos($recipient, $email) === false ) {
$recipient .= ',' . $email;
}
}
}
return $recipient;
}
代码位于事件子主题(或事件主题)的 functions.php 文件中。测试和工作。

Note: The Product categories can be term Ids, term slugs or term names.



类似: Different recipients based on products sold in WooCommerce email notification

关于php - WooCommerce 电子邮件通知中基于产品类别的不同收件人,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54034812/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com