This is my first question here, and I need your help. I tried researching this topic, but did not manage to understand what exactly I am doing wrong, so please bear with me. I am using the following code: ` add_filter( 'woocommerce_email_recipient_new_order', 'add_recipient', 10, 2 ); function add_recipient( $recipient, $order ) { if ( ! is_a( $order, 'WC_Order' ) ) return $recipient;
// Additional email recipient
$additional_email1 = "email#[email protected]";
$additional_email2 = "email#[email protected]";
// 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;
} `
I am now trying to adapt this code to work with the parent product. Ideally, this code should work for both variable and non-varible products, such that if a product contains the term "d1", regardless of whether it is variable or not, the email should always go to email#1. Same for d2 and email#2.
However, the code is not outputting any errors. Should I use $product->get_id() instead? Or what am I doing wrong?
Try the below code. I revised your code. try the below code.
// Additional email recipient
$additional_email1 = "email#[email protected]";
$additional_email2 = "email#[email protected]";
// 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;
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Find the answer in similar questions on our website.
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.
PHP (from the English Hypertext Preprocessor - hypertext preprocessor) is a scripting programming language for developing web applications. Supported by most hosting providers, it is one of the most popular tools for creating dynamic websites.
The PHP scripting language has gained wide popularity due to its processing speed, simplicity, cross-platform, functionality and distribution of source codes under its own license.
https://www.php.net/
Welcome to the Q&A site for web developers. Here you can ask a question about the problem you are facing and get answers from other experts. We have created a user-friendly interface so that you can quickly and free of charge ask a question about a web programming problem. We also invite other experts to join our community and help other members who ask questions. In addition, you can use our search for questions with a solution.
Ask about the real problem you are facing. Describe in detail what you are doing and what you want to achieve.
Our goal is to create a strong community in which everyone will support each other. If you find a question and know the answer to it, help others with your knowledge.