WooCommerce Custom Email Notifications
This custom plugin for WooCommerce sends separate order notifications to specific email addresses based on the categories of the products in the order. It hooks into the WooCommerce order status completed action and iterates through the order items, matching product categories with predefined email addresses, and dispatching notifications accordingly.
<?php /** * Plugin Name: WooCommerce Custom Email Notifications * Plugin URI: https://codewp.ai * Description: This plugin sends order notifications for different categories to separate email addresses. * Version: 1.0.0 * Author: CodeWP Assistant * Author URI: https://codewp.ai */ // If this file is called directly, abort. if (!defined('WPINC')) { die; } add_action( 'woocommerce_order_status_completed', 'send_custom_email_notifications', 10, 1 ); function send_custom_email_notifications( $order_id ) { $order = wc_get_order( $order_id ); $items = $order->get_items(); $product_cat_emails = array( 'marketing' => 'marketing@example.com', 'product' => 'product@example.com', ); foreach ( $items as $item ) { $product_id = $item->get_product_id(); $terms = get_the_terms( $product_id, 'product_cat' ); foreach ( $terms as $term ) { $cat_name = $term->slug; if ( array_key_exists( $cat_name, $product_cat_emails ) ) { $to = $product_cat_emails[ $cat_name ]; $subject = 'New Order: ' . $order->get_order_number(); $body = 'A new order has been placed on your website. Order number: ' . $order->get_order_number(); wp_mail( $to, $subject, $body ); } } } }
Frequently Asked Questions
This code sends custom email notifications when a WooCommerce order is completed, based on the product categories in the order.