Completed

PHP re-write Code Snippet

I need someone to rewrite the following snippet. This snippet handles different receiver per category.

The goal is: if in a order are products of more then 1 category, the order notification should only go to one email address. I guess for a php freak that isn't a big thing to optimize this snippet analogous my need.

// 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(

'email1 @ com' => 'category-one',

'email2 @ com'' => 'category-two',

'email3 @ 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;

}

Skills: PHP, MySQL, WordPress

About the Client:
( 171 reviews ) Lyss, Switzerland

Project ID: #34402034

Awarded to:

denysdenysivanch

⭐⭐⭐⭐⭐ Hello. I have rich experience in PHP. [login to view URL] [login to view URL] So I can rewrite snippet as you want. Please contact me to discuss more, looking forward to work with you. Thanks!

$20 USD in 7 days
(3 Reviews)
2.3

12 freelancers are bidding on average $22 for this job

(161 Reviews)
7.4
rainsys

--------- PLEASE GIVE ME A CHANCE TO WORK ON YOUR PROJECT --------- Dear Sir / Madam, I am Moffazzal from Rainsys. Rainsys is a Web & Software Development Company with 12 persons working together. We have 9+ years of More

$10 USD in 1 day
(86 Reviews)
5.9
(19 Reviews)
5.6
CreativeExpertIT

Hello Sir, We are experts of PHP development , having more then 8 years of experience. I am ready to work as per given description in best price Kindly PM me to discuss further

$10 USD in 7 days
(57 Reviews)
5.2
SeasonedDveloper

Hello, I can do this. Kindly inbox me.

$20 USD in 1 day
(3 Reviews)
3.8
tarungehlot88

Hi There, I hope you are doing well. Project - - I have read the description of your project and understood that you are looking for a website developed. I Have gone through your initial project requirement and I have More

$20 USD in 7 days
(15 Reviews)
3.9
MSaadJaved

Hi, Your posting for "PHP re-write Code Snippet" perfectly matches my expertise and experience. Having read the requirements carefully, I believe I can complete your project very nicely. I have extensive knowledge o More

$20 USD in 7 days
(0 Reviews)
0.0
suzanreyes89

Hello, I'm Suzan, professional Graphic Artist with skills including WordPress, PHP and MySQL. I have reviewed your project and I am qualified to carry it out with incredible goals for your fashion company. I will cre More

$20 USD in 1 day
(0 Reviews)
0.0
EasternJobs

Hello there, ***I can build your PHP site I assured you 100% customer satisfaction guarantee I will provide you responsive website, high level graphics I have the 6+ years of experience in web development & design More

$20 USD in 7 days
(2 Reviews)
0.0
fauzan428

Hi, i have more than 7 years of experience in web development (php), so i can do the best for you. Thankyou

$20 USD in 1 day
(0 Reviews)
0.0
elmarjanyamine65

I am gonna work with PHP with my own program . And it will be my pleasure to gain your trust from my first work with you.

$20 USD in 7 days
(0 Reviews)
0.0