Reputation: 12986
Some of our products will be drop-ship products. Some manufacturers only accepts orders by email. Thus, when specific products are ordered, I would like to have them tagged to be sent to those specific manufactures in an order email.
How can this be accomplished?
Upvotes: 0
Views: 85
Reputation: 253901
A good way is to use custom field that you will fill with the manufacture email for those specific products only.
You can use an admin product select field (dropdown), where you will chose a Manufacture name for your related products (each manufacture name will have a corresponding email hidden option value).
Then When a related product is purchased, the selected Manufacture will receive also the New order email notification.
You will have to set all the different related manufactures name(s) and email(s) (pairs) in the first function (array).
The code:
// Custom function that handle settings for manufactures names and emails (pairs)
function manufacture_emails_and_names_pairs() {
// Below in the array set the email as Key and the manufacture name as value (pairs) for each maufacture (one by line)
return array(
'[email protected]' => 'Cocacola',
'[email protected]' => 'Levis',
'[email protected]' => 'Liebing Inc',
'[email protected]' => 'Soup is Good',
);
}
// Display an admin product custom Field (dropdown)
add_action( 'woocommerce_product_options_general_product_data', 'add_admin_custom_product_field' );
function add_admin_custom_product_field() {
global $product_object;
echo '<div class="options_group">';
$field_id = 'manufacture_email';
$default = array( '' => __( 'Chose a Manufacture (email notification)', 'woocommerce' ) );
$value = $product_object->get_meta('_'.$field_id);
woocommerce_wp_select( array(
'id' => $field_id,
'label' => __( 'Manufactures (email)', 'woocommerce' ),
'description' => __( 'Chose here a Manufacture to send an email notification to that manufacture when this product is purchased', 'woocommerce' ),
'desc_tip' => true,
'options' => array_merge( $default, manufacture_emails_and_names_pairs() ),
'value' => $value,
) );
echo '</div>';
}
// Save admin product custom Field selected value
add_action( 'woocommerce_admin_process_product_object', 'save_admin_custom_product_field_value' );
function save_admin_custom_product_field_value( $product ){
$field_id = 'manufacture_email';
if( isset( $_POST[$field_id] ) ) {
$product->update_meta_data( '_'.$field_id, sanitize_email( $_POST[$field_id] ) );
}
}
// Save the manufacture name and email on related order items visible only on admin
add_action( 'woocommerce_checkout_create_order_line_item', 'giftcard_price_field_order_data', 10, 4 );
function giftcard_price_field_order_data( $item, $cart_item_key, $values, $order ) {
$meta_key_1 = '_manufacture_name';
$meta_key_2 = '_manufacture_email';
$email = get_post_meta( $values['product_id'], $meta_key_2, true ); // Get manufacture email value
if ( ! empty( $email ) ) {
$setting_data = manufacture_emails_and_names_pairs(); // Load settings
$item->update_meta_data( $meta_key_1, $setting_data[$email] );
$item->update_meta_data( $meta_key_2, $email );
}
}
// Add selected Manufacture email recipient to "New Order" email notification
add_filter('woocommerce_email_recipient_new_order', 'add_manufacture_email_recipient_to_new_order_notification', 10, 2);
function add_manufacture_email_recipient_to_new_order_notification( $recipient, $order ) {
if ( ! is_a( $order, 'WC_Order' ) )
return $recipient;
$meta_key = '_manufacture_email';
foreach ( $order->get_items() as $item ) {
$email = $item->get_meta( $meta_key );
if ( ! empty( $email ) ) {
$recipient .= ',' . $email;
}
}
return $recipient;
}
Code goes in functions.php file of your active child theme (or active theme). Tested and works.
This is just a quick example that shows the right way. Now you can send a custom email notification instead to the related Manufactures, but this is a real development.
Upvotes: 1