Created
August 17, 2017 10:38
-
-
Save manlioma/346e1f37f2e74954e1c383604e112ade to your computer and use it in GitHub Desktop.
Add WooCommerce "New Order" email recipient if a product in the order needs shipping Raw
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Add another email recipient for admin New Order emails if a shippable product is ordered | |
* | |
* @param string $recipient a comma-separated string of email recipients (will turn into an array after this filter!) | |
* @param \WC_Order $order the order object for which the email is sent | |
* @return string $recipient the updated list of email recipients | |
*/ | |
function sv_conditional_email_recipient( $recipient, $order ) { | |
// Bail on WC settings pages since the order object isn't yet set yet | |
// Not sure why this is even a thing, but shikata ga nai | |
$page = $_GET['page'] = isset( $_GET['page'] ) ? $_GET['page'] : ''; | |
if ( 'wc-settings' === $page ) { | |
return $recipient; | |
} | |
// just in case | |
if ( ! $order instanceof WC_Order ) { | |
return $recipient; | |
} | |
$items = $order->get_items(); | |
// check if a shipped product is in the order | |
foreach ( $items as $item ) { | |
$product = $order->get_product_from_item( $item ); | |
// add our extra recipient if there's a shipped product - commas needed! | |
// we can bail if we've found one, no need to add the recipient more than once | |
if ( $product && $product->needs_shipping() ) { | |
$recipient .= ', [email protected]'; | |
return $recipient; | |
} | |
} | |
return $recipient; | |
} | |
add_filter( 'woocommerce_email_recipient_new_order', 'sv_conditional_email_recipient', 10, 2 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment