Last active
October 19, 2023 01:25
-
-
Save haze83/05f734263203f9442c66ef5c248b176d to your computer and use it in GitHub Desktop.
F4 Shipping Phone and E-Mail for WooCommerce
This file contains hidden or 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
<? | |
/** | |
* Adds shipping email address as cc to WoocCmmerce customer emails | |
* | |
* @param string $header WC_Email header | |
* @param string $id WC_Email id | |
* @param \WC_Order $object | |
* @return string | |
* | |
* @see https://gist.githubusercontent.com/haze83/05f734263203f9442c66ef5c248b176d | |
*/ | |
function f4t_wc_email_cc_shipping($header, $id, $object = null) { | |
if(!is_a( $object, 'WC_Order' )) { | |
if(defined('WP_DEBUG') && WP_DEBUG) { | |
$logger = wc_get_logger(); | |
$logger_context = ['source' => 'f4_wcspe_hooks']; | |
$logger->info(wc_print_r([ | |
'message' => 'not an order object', | |
'header' => $header, | |
'object' => $object | |
], true), $logger_context); | |
} | |
return $header; | |
} | |
$apply_on_email_ids = [ | |
'customer_on_hold_order', | |
'customer_processing_order', | |
'customer_completed_order', | |
'customer_refunded_order', | |
'customer_note', | |
'customer_invoice', | |
]; | |
if(in_array($id, $apply_on_email_ids)) { | |
$shipping_email = get_post_meta($object->get_id(), '_shipping_email', true); | |
if($shipping_email !== $object->get_billing_email()) { | |
$header .= 'CC: ' . $object->get_shipping_first_name() . ' ' . $object->get_shipping_last_name() . ' <' . $shipping_email . ">\r\n"; | |
} | |
} | |
return $header; | |
} | |
add_filter('woocommerce_email_headers', 'f4t_wc_email_cc_shipping', 10, 3); |
This file contains hidden or 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 | |
/** | |
* Adds shipping email address to WoocCmmerce customer emails | |
* | |
* @param string $recipient Billing email address | |
* @param \WC_Order $object | |
* @return string | |
* | |
* @see https://gist.githubusercontent.com/haze83/05f734263203f9442c66ef5c248b176d/ | |
*/ | |
function f4t_wc_email_to_shipping($recipient, $object) { | |
if(!is_a( $object, 'WC_Order' )) { | |
if(defined('WP_DEBUG') && WP_DEBUG) { | |
$logger = wc_get_logger(); | |
$logger_context = ['source' => 'f4_wcspe_hooks']; | |
$logger->info(wc_print_r([ | |
'message' => 'not an order object', | |
'object' => $object | |
], true), $logger_context); | |
} | |
return $recipient; | |
} | |
$shipping_email = get_post_meta($object->get_id(), '_shipping_email', true); | |
$recipients = array_map( 'trim', explode( ',', $recipient ) ); | |
if(!empty($shipping_email) && !in_array($shipping_email, $recipients)) { | |
return $recipient . ',' . $shipping_email; | |
} | |
return $recipient; | |
} | |
/** | |
* Add the shipping email to the relevant woocommerce_email_recipient_* hooks | |
*/ | |
add_filter('woocommerce_email_recipient_customer_on_hold_order', 'f4t_wc_email_to_shipping', 10, 2); | |
add_filter('woocommerce_email_recipient_customer_processing_order', 'f4t_wc_email_to_shipping', 10, 2); | |
add_filter('woocommerce_email_recipient_customer_completed_order', 'f4t_wc_email_to_shipping', 10, 2); | |
add_filter('woocommerce_email_recipient_customer_refunded_order', 'f4t_wc_email_to_shipping', 10, 2); | |
add_filter('woocommerce_email_recipient_customer_note', 'f4t_wc_email_to_shipping', 10, 2); | |
add_filter('woocommerce_email_recipient_customer_invoice', 'f4t_wc_email_to_shipping', 10, 2); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment