To add custom order status in WooCommerce and customize the email templates for these custom statuses, you can follow these steps:
Step 1: Add Custom Order Status
Use the `register_post_status` function to add a custom order status. Add the following code to your theme’s functions.php file or in a custom plugin:
// Add custom order status function add_custom_order_status() { register_post_status( 'wc-custom-status', array( 'label' => _x( 'Custom Status', 'Order status', 'your-text-domain' ), 'public' => true, 'exclude_from_search' => false, 'show_in_admin_all_list' => true, 'show_in_admin_status_list' => true, 'label_count' => _n_noop( 'Custom Status <span class="count">(%s)</span>', 'Custom Status <span class="count">(%s)</span>', 'your-text-domain' ) ) ); } add_action( 'init', 'add_custom_order_status' );
Step 2: Add Custom Order Status to Order Admin Filter Dropdown
Make your custom order status appear in the order admin filter dropdown by using the `wc_order_statuses` filter:
// Add custom order status to admin filter dropdown function add_custom_order_status_to_filter( $order_statuses ) { $order_statuses['wc-custom-status'] = _x( 'Custom Status', 'Order status', 'your-text-domain' ); return $order_statuses; } add_filter( 'wc_order_statuses', 'add_custom_order_status_to_filter' );
Step 3: Register Custom Email Action for Custom Order Status
Register a custom email action for your custom order status using the `woocommerce_email_actions` filter:
// Register custom email action for custom order status function add_custom_email_action( $email_actions ) { $email_actions[] = 'woocommerce_order_status_wc-custom-status'; return $email_actions; } add_filter( 'woocommerce_email_actions', 'add_custom_email_action' );
Step 4: Create a Custom Email Class
Create a custom email class that extends the `WC_Email` class to customize the email content and settings for your custom order status. Save this class in a separate file (e.g., custom-new-order-email.php).
// custom-new-order-email.php class Custom_New_Order_Email extends WC_Email { /** * Constructor. */ public function __construct() { $this->id = 'custom_new_order'; $this->title = __( 'Custom New Order', 'your-text-domain' ); $this->description = __( 'Custom new order emails are sent when the order status is changed to the custom order status.', 'your-text-domain' ); $this->heading = __( 'Custom New Order', 'your-text-domain' ); $this->subject = __( 'New Order: #{order_number}', 'your-text-domain' ); $this->template_html = 'emails/custom-new-order.php'; $this->template_plain = 'emails/plain/custom-new-order.php'; $this->template_base = plugin_dir_path( __FILE__ ); // Path to your custom-new-order-email.php file directory $this->placeholders = array( '{order_number}' => '', // Add more placeholders if needed ); // Call parent constructor parent::__construct(); // Other settings, hooks, and filters can be added here } /** * Trigger. * * @param int $order_id The order ID. */ public function trigger( $order_id ) { // Get order object $this->object = wc_get_order( $order_id ); // Set email recipient $this->recipient = $this->object->get_billing_email(); // Set email heading and subject $this->placeholders['{order_number}'] = $this->object->get_order_number(); $this->heading = apply_filters( 'woocommerce_email_heading_' . $this->id, $this->heading, $this->object ); $this->subject = apply_filters( 'woocommerce_email_subject_' . $this->id, $this->subject, $this->object ); // Call parent trigger to send the email parent::trigger( $order_id ); } }
Step 5: Hook the Custom Email Class to WooCommerce
Finally, hook your custom email class to WooCommerce using the `woocommerce_email_classes` filter:
// Hook custom email class to WooCommerce function add_custom_email_class( $email_classes ) { require_once 'path/to/custom-new-order-email.php'; // Replace with the actual path to your custom-new-order-email.php file $email_classes['Custom_New_Order_Email'] = new Custom_New_Order_Email(); return $email_classes; } add_filter( 'woocommerce_email_classes', 'add_custom_email_class' );
Make sure to replace `’path/to/custom-new-order-email.php’` with the actual path to your custom-new-order-email.php file.
That’s it! Now you have a custom order status and a customized email template for that status. The email template will be used when an order is set to the custom order status. You can customize the email content, subject, and other settings within the `Custom_New_Order_Email` class to suit your requirements.
Remember to use a child theme or custom plugin to add custom code, and always test your customizations thoroughly before deploying them to a live site.