在 WooCommerce 中为特定产品属性添加自定义电子邮件订单收件人?
Adding a custom email order recipient for specific product attributes in WooCommerce?
这是我的第一个问题,我需要你的帮助。我试着研究这个话题,但没能理解我到底做错了什么,所以请多多包涵。
我正在使用以下代码:
`
add_filter( 'woocommerce_email_recipient_new_order', 'add_recipient', 10, 2 );
函数 add_recipient( $recipient, $order )
{
如果 ( ! is_a( $order, 'WC_Order' ) ) return $recipient;
// Additional email recipient
$additional_email1 = "email#1@email.com";
$additional_email2 = "email#2@email.com";
// The term slug
$term_slug1 = "d1";
$term_slug2 = "d2";
$has_term = false;
// Iterating through each order item
foreach ($order->get_items() as $item_id => $item_obj) {
$product_id = $item_obj->get_product_id();
$product_obj = wc_get_product($product_id);
$product_attributes = $product_obj->get_attributes();
foreach( $product_attributes as $taxonomy_key => $term_value ){
if( $taxonomy_key == "pa_d" && $term_value == $term_slug1 ){
$recipient .= ','. $additional_email1;
$has_term = true;
break; // stop searching
}
if( $taxonomy_key == "pa_d" && $term_value == $term_slug2 ){
$recipient .= ','. $additional_email2;
$has_term = true;
break; // stop searching
}
}
if( $has_term ) break; // stop the main loop
}
return $recipient;
}`
我现在正在尝试调整此代码以与父产品一起使用。理想情况下,此代码应适用于可变和非可变产品,这样如果产品包含术语“d1”,无论它是否可变,电子邮件都应始终转到电子邮件#1。 d2 和电子邮件#2 相同。
但是,代码没有输出任何错误。我应该改用 $product->get_id() 吗?或者我做错了什么?
试试下面的代码。我修改了你的代码。试试下面的代码。
// Additional email recipient
$additional_email1 = "email#1@email.com";
$additional_email2 = "email#2@email.com";
// The term slug
$term_slug1 = "d1";
$term_slug2 = "d2";
$has_term = false; // Initializing
// Loop though order items
foreach ( $order->get_items() as $item ){
// Only for product variations
if( $item->get_variation_id() > 0 ){
$product = $item->get_product(); // The WC_Product Object
$product_id = $item->get_product_id(); // Product ID
// Loop through product attributes set in the variation
foreach( $product->get_attributes() as $taxonomy => $term_slug ){
// comparing attribute parameter value with current attribute value
if ( $term_slug === $term_slug1 ) {
$has_term = true;
$recipient .= ','. $additional_email1;
}
if ( $term_slug === $term_slug2 ) {
$has_term = true;
$recipient .= ','. $additional_email2;
}
}
}
if( $has_term ) break; // stop the main loop
}
return $recipient;
这是我的第一个问题,我需要你的帮助。我试着研究这个话题,但没能理解我到底做错了什么,所以请多多包涵。 我正在使用以下代码: ` add_filter( 'woocommerce_email_recipient_new_order', 'add_recipient', 10, 2 ); 函数 add_recipient( $recipient, $order ) { 如果 ( ! is_a( $order, 'WC_Order' ) ) return $recipient;
// Additional email recipient
$additional_email1 = "email#1@email.com";
$additional_email2 = "email#2@email.com";
// The term slug
$term_slug1 = "d1";
$term_slug2 = "d2";
$has_term = false;
// Iterating through each order item
foreach ($order->get_items() as $item_id => $item_obj) {
$product_id = $item_obj->get_product_id();
$product_obj = wc_get_product($product_id);
$product_attributes = $product_obj->get_attributes();
foreach( $product_attributes as $taxonomy_key => $term_value ){
if( $taxonomy_key == "pa_d" && $term_value == $term_slug1 ){
$recipient .= ','. $additional_email1;
$has_term = true;
break; // stop searching
}
if( $taxonomy_key == "pa_d" && $term_value == $term_slug2 ){
$recipient .= ','. $additional_email2;
$has_term = true;
break; // stop searching
}
}
if( $has_term ) break; // stop the main loop
}
return $recipient;
}`
我现在正在尝试调整此代码以与父产品一起使用。理想情况下,此代码应适用于可变和非可变产品,这样如果产品包含术语“d1”,无论它是否可变,电子邮件都应始终转到电子邮件#1。 d2 和电子邮件#2 相同。
但是,代码没有输出任何错误。我应该改用 $product->get_id() 吗?或者我做错了什么?
试试下面的代码。我修改了你的代码。试试下面的代码。
// Additional email recipient
$additional_email1 = "email#1@email.com";
$additional_email2 = "email#2@email.com";
// The term slug
$term_slug1 = "d1";
$term_slug2 = "d2";
$has_term = false; // Initializing
// Loop though order items
foreach ( $order->get_items() as $item ){
// Only for product variations
if( $item->get_variation_id() > 0 ){
$product = $item->get_product(); // The WC_Product Object
$product_id = $item->get_product_id(); // Product ID
// Loop through product attributes set in the variation
foreach( $product->get_attributes() as $taxonomy => $term_slug ){
// comparing attribute parameter value with current attribute value
if ( $term_slug === $term_slug1 ) {
$has_term = true;
$recipient .= ','. $additional_email1;
}
if ( $term_slug === $term_slug2 ) {
$has_term = true;
$recipient .= ','. $additional_email2;
}
}
}
if( $has_term ) break; // stop the main loop
}
return $recipient;