Jus*_*ode 5 php wordpress orders woocommerce hook-woocommerce
使用添加在 WooCommerce 4+ 答案代码中发送电子邮件通知的新订单状态,我已经能够收到一个自定义状态的电子邮件通知,但无法了解如何发送电子邮件通知两种自定义状态。
请参阅下面的示例代码和我尝试过的多个自定义状态电子邮件通知的代码。我还包含了完整的代码,它显示了创建多个状态的整个过程。
希望对这个快速修复有所帮助!
另外,至于电子邮件正文 - 是否有可能通过此代码更改处理订单内容,而无需创建新的电子邮件模板?
正在运行 - 单个自定义状态电子邮件通知“已发货”
add_filter( 'woocommerce_email_actions', 'filter_woocommerce_email_actions' );
function filter_woocommerce_email_actions( $actions ){
$actions[] = 'woocommerce_order_status_wc-shipped';
return $actions;
}
// Send Customer Processing Order email notification when order status get changed from "tree" to "processing"
add_action('woocommerce_order_status_changed', 'shipped_status_custom_notification', 10, 4);
function shipped_status_custom_notification( $order_id, $from_status, $to_status, $order ) {
if( 'shipped' === $to_status ) {
// The email notification type
$email_key = 'WC_Email_Customer_Processing_Order';
// Get specific WC_emails object
$email_obj = WC()->mailer()->get_emails()[$email_key];
// Sending the customized email
$email_obj->trigger( $order_id );
}
}
// Customize email heading for this custom status email notification
add_filter( 'woocommerce_email_heading_customer_processing_order', 'email_heading_customer_shipped_order', 10, 2 );
function email_heading_customer_shipped_order( $heading, $order ){
if( $order->has_status( 'shipped' ) ) {
$email_key = 'WC_Email_Customer_Processing_Order'; // The email notification type
$email_obj = WC()->mailer()->get_emails()[$email_key]; // Get specific WC_emails object
$heading_txt = sprintf( __('Order #%s has been Shipped!', 'woocommerce'), '{order_number}' ); // New subject text
return $email_obj->format_string( $heading_txt );
}
return $heading;
}
// Customize email subject for this custom status email notification
add_filter( 'woocommerce_email_subject_customer_processing_order', 'email_subject_customer_shipped_order', 10, 2 );
function email_subject_customer_shipped_order( $subject, $order ){
if( $order->has_status( 'shipped' ) ) {
$email_key = 'WC_Email_Customer_Processing_Order'; // The email notification type
$email_obj = WC()->mailer()->get_emails()[$email_key]; // Get specific WC_emails object
$subject_txt = sprintf( __('Your %s Order #%s has been Shipped!', 'woocommerce'), '{site_title}', '{order_number}' ); // New subject text
return $email_obj->format_string( $subject_txt );
}
return $subject;
}
Run Code Online (Sandbox Code Playgroud)
已尝试/未工作 - 两种自定义状态的电子邮件通知
// Adding action for 'awaiting-delivery'
add_filter( 'woocommerce_email_actions', 'custom_email_actions', 20, 1 );
function custom_email_actions( $action ) {
$actions[] = 'woocommerce_order_status_wc-shipped';
$actions[] = 'woocommerce_order_status_wc-readytocollect';
return $actions;
}
add_action( 'woocommerce_order_status_wc-shipped', array( WC(), 'send_transactional_email' ), 10, 1 );
add_action( 'woocommerce_order_status_wc-readytocollect', array( WC(), 'send_transactional_email' ), 10, 1 );
// Sending an email notification when order gets a custom status
add_action('woocommerce_order_status_changed', 'custom_status_trigger_email_notification', 10, 4);
function custom_status_trigger_email_notification( $order_id, $from_status, $to_status, $order ) {
$custom_statuses = array('Shipped', 'Ready to Collect'); // Here your custom statuses slugs
if( in_array($to_status, $custom_statuses) ) {
// Loop through each custom status
foreach ( $custom_statuses as $custom_status ) {
// Trigger an email notification when a order get a custom status
if ( $custom_status === $to_status ) {
WC()->mailer()->get_emails()['WC_Email_Customer_Processing_Order']->trigger( $order_id );
}
}
}
}
// Customize email heading for your custom statuses email notifications
add_filter( 'woocommerce_email_heading_customer_processing_order', 'custom_email_heading_for_custom_order_status', 10, 2 );
function custom_email_heading_for_custom_order_status( $heading, $order ){
// Here your custom statuses slugs / Heading texts pairs
$custom_statuses = array(
'Shipped' => __('Planet Vape {order_number} has been Shipped!','woocommerce'),
'Ready to Collect' => __('Planet Vape {order_number} is Ready to Collect!','woocommerce')
);
// Loop through each custom status / heading text pairs
foreach ( $custom_statuses as $custom_status => $heading_text ) {
// Change an email notification heading text when a order get a custom status
if( $order->has_status( $custom_status ) ) {
$email = WC()->mailer()->get_emails()['WC_Email_Customer_Processing_Order']; // Get the specific WC_emails object
return $email->format_string( $heading_text );
}
}
return $heading;
}
// Customize email subject for your custom statuses email notifications
add_filter( 'woocommerce_email_subject_customer_processing_order', 'custom_email_subject_for_custom_order_status', 10, 2 );
function custom_email_subject_for_custom_order_status( $subject, $order ){
// Here your custom statuses slugs / Heading texts pairs
$custom_statuses = array(
'Shipped' => __('Planet Vape {order_number} has been Shipped!','woocommerce'),
'Ready to Collect' => __('Planet Vape {order_number} is Ready to Collect!','woocommerce')
);
// Loop through each custom status / subject text pairs
foreach ( $custom_statuses as $custom_status => $subject_text ) {
// Change an email notification heading text when a order get a custom status
if( $order->has_status( $custom_status ) ) {
$email = WC()->mailer()->get_emails()['WC_Email_Customer_Processing_Order']; // Get the specific WC_emails object
return $email->format_string( $subject_text );
}
}
return $subject;
}
Run Code Online (Sandbox Code Playgroud)
包含单一自定义状态的工作邮件的完整代码。 这显示了我使用批量操作和操作按钮添加自定义状态的完整过程 - 一切正常。
add_action('init', 'register_custom_order_statuses');
function register_custom_order_statuses() {
register_post_status('wc-shipped ', array(
'label' => __( 'Shipped', 'woocommerce' ),
'public' => true,
'exclude_from_search' => false,
'show_in_admin_all_list' => true,
'show_in_admin_status_list' => true,
'label_count' => _n_noop('Shipped <span class="count">(%s)</span>', 'Shipped <span class="count">(%s)</span>')
));
register_post_status('wc-readytocollect ', array(
'label' => __( 'Ready to Collect', 'woocommerce' ),
'public' => true,
'exclude_from_search' => false,
'show_in_admin_all_list' => true,
'show_in_admin_status_list' => true,
'label_count' => _n_noop('Ready to Collect <span class="count">(%s)</span>', 'Ready to Collect <span class="count">(%s)</span>')
));
}
// Add a custom order status to list of WC Order statuses
add_filter('wc_order_statuses', 'add_custom_order_statuses');
function add_custom_order_statuses($order_statuses) {
$new_order_statuses = array();
// add new order status before processing
foreach ($order_statuses as $key => $status) {
$new_order_statuses[$key] = $status;
if ('wc-processing' === $key) {
$new_order_statuses['wc-shipped'] = __('Shipped', 'woocommerce' );
$new_order_statuses['wc-readytocollect'] = __('Ready to Collect', 'woocommerce' );
}
}
return $new_order_statuses;
}
// Adding custom status 'awaiting-delivery' to admin order list bulk dropdown
add_filter( 'bulk_actions-edit-shop_order', 'custom_dropdown_bulk_actions_shop_order', 50, 1 );
function custom_dropdown_bulk_actions_shop_order( $actions ) {
$new_actions = array();
// add new order status before processing
foreach ($actions as $key => $action) {
if ('mark_processing' === $key)
$new_actions['mark_shipped'] = __( 'Change status to shipped', 'woocommerce' );
$new_actions['mark_readytocollect'] = __( 'Change status to Ready to Collect', 'woocommerce' );
$new_actions[$key] = $action;
}
return $new_actions;
}
// Add a custom order status action button (for orders with "processing" status)
add_filter( 'woocommerce_admin_order_actions', 'add_custom_order_status_actions_button', 100, 2 );
function add_custom_order_status_actions_button( $actions, $order ) {
// Display the button for all orders that have a 'processing', 'pending' or 'on-hold' status
if ( $order->has_status( array( 'on-hold', 'processing', 'pending' ) ) ) {
// The key slug defined for your action button
$action_slug = 'shipped';
// Set the action button
$actions[$action_slug] = array(
'url' => wp_nonce_url( admin_url( 'admin-ajax.php?action=woocommerce_mark_order_status&status='.$action_slug.'&order_id='.$order->get_id() ), 'woocommerce-mark-order-status' ),
'name' => __( 'Shipped', 'woocommerce' ),
'action' => $action_slug,
);
}
return $actions;
}
// Set styling for custom order status action button icon and List icon
add_action( 'admin_head', 'add_custom_order_status_actions_button_css' );
function add_custom_order_status_actions_button_css() {
$action_slug = "shipped"; // The key slug defined for your action button
?>
<style>
.wc-action-button-<?php echo $action_slug; ?>::after {
font-family: woocommerce !important; content: "\e029" !important;
}
</style>
<?php
}
add_filter( 'woocommerce_email_actions', 'filter_woocommerce_email_actions' );
function filter_woocommerce_email_actions( $actions ){
$actions[] = 'woocommerce_order_status_wc-shipped';
return $actions;
}
// Send Customer Processing Order email notification when order status get changed from "tree" to "processing"
add_action('woocommerce_order_status_changed', 'shipped_status_custom_notification', 10, 4);
function shipped_status_custom_notification( $order_id, $from_status, $to_status, $order ) {
if( 'shipped' === $to_status ) {
// The email notification type
$email_key = 'WC_Email_Customer_Processing_Order';
// Get specific WC_emails object
$email_obj = WC()->mailer()->get_emails()[$email_key];
// Sending the customized email
$email_obj->trigger( $order_id );
}
}
// Customize email heading for this custom status email notification
add_filter( 'woocommerce_email_heading_customer_processing_order', 'email_heading_customer_shipped_order', 10, 2 );
function email_heading_customer_shipped_order( $heading, $order ){
if( $order->has_status( 'shipped' ) ) {
$email_key = 'WC_Email_Customer_Processing_Order'; // The email notification type
$email_obj = WC()->mailer()->get_emails()[$email_key]; // Get specific WC_emails object
$heading_txt = sprintf( __('Order #%s has been Shipped!', 'woocommerce'), '{order_number}' ); // New subject text
return $email_obj->format_string( $heading_txt );
}
return $heading;
}
// Customize email subject for this custom status email notification
add_filter( 'woocommerce_email_subject_customer_processing_order', 'email_subject_customer_shipped_order', 10, 2 );
function email_subject_customer_shipped_order( $subject, $order ){
if( $order->has_status( 'shipped' ) ) {
$email_key = 'WC_Email_Customer_Processing_Order'; // The email notification type
$email_obj = WC()->mailer()->get_emails()[$email_key]; // Get specific WC_emails object
$subject_txt = sprintf( __('Your %s Order #%s has been Shipped!', 'woocommerce'), '{site_title}', '{order_number}' ); // New subject text
return $email_obj->format_string( $subject_txt );
}
return $subject;
}
Run Code Online (Sandbox Code Playgroud)
有一些错误\xe2\x80\xa6 对于具有自定义电子邮件通知的多个订单状态,请使用以下完整代码(在所有相关代码之前删除):
\n// Enable custom statuses for WooCommerce Orders\nadd_action('init', 'register_custom_order_statuses');\nfunction register_custom_order_statuses() {\n\n register_post_status('wc-shipped ', array(\n 'label' => __( 'Shipped', 'woocommerce' ),\n 'public' => true,\n 'exclude_from_search' => false,\n 'show_in_admin_all_list' => true,\n 'show_in_admin_status_list' => true,\n 'label_count' => _n_noop('Shipped <span class="count">(%s)</span>', 'Shipped <span class="count">(%s)</span>')\n ));\n\n register_post_status('wc-readytocollect ', array(\n 'label' => __( 'Ready to Collect', 'woocommerce' ),\n 'public' => true,\n 'exclude_from_search' => false,\n 'show_in_admin_all_list' => true,\n 'show_in_admin_status_list' => true,\n 'label_count' => _n_noop('Ready to Collect <span class="count">(%s)</span>', 'Ready to Collect <span class="count">(%s)</span>')\n ));\n}\n\n// Add a custom order status to list of WC Order statuses\nadd_filter('wc_order_statuses', 'add_custom_order_statuses');\nfunction add_custom_order_statuses($order_statuses) {\n $new_order_statuses = array();\n\n // add new order status before processing\n foreach ($order_statuses as $key => $status) {\n $new_order_statuses[$key] = $status;\n if ('wc-processing' === $key) {\n $new_order_statuses['wc-shipped'] = __('Shipped', 'woocommerce' );\n $new_order_statuses['wc-readytocollect'] = __('Ready to Collect', 'woocommerce' );\n }\n }\n return $new_order_statuses;\n}\n\n// Adding custom status statuses to admin order list bulk dropdown\nadd_filter( 'bulk_actions-edit-shop_order', 'custom_dropdown_bulk_actions_shop_order', 50, 1 );\nfunction custom_dropdown_bulk_actions_shop_order( $actions ) {\n $new_actions = array();\n\n // add new order status before processing\n foreach ($actions as $key => $action) {\n if ('mark_processing' === $key) {\n $new_actions['mark_shipped'] = __( 'Change status to shipped', 'woocommerce' );\n $new_actions['mark_readytocollect'] = __( 'Change status to Ready to Collect', 'woocommerce' );\n }\n $new_actions[$key] = $action;\n }\n return $new_actions;\n}\n\n// Add a custom order statuses action button (for orders with "processing" status)\nadd_filter( 'woocommerce_admin_order_actions', 'add_custom_order_status_actions_button', 100, 2 );\nfunction add_custom_order_status_actions_button( $actions, $order ) {\n $allowed_statuses = array( 'on-hold', 'processing', 'pending', 'shipped', 'readytocollect' ); // Define allowed statuses\n\n // Display the button for all orders that have allowed status (as defined in the array)\n if ( in_array( $order->get_status(), $allowed_statuses ) ) {\n\n // The key slug defined from status with the name\n $action_slugs = array(\n 'shipped' => __('Shipped', 'woocommerce'),\n 'readytocollect' => __('Ready to Collect', 'woocommerce')\n );\n\n // Loop through custom statuses\n foreach ( $action_slugs as $action_slug => $name ) {\n // Display "processing" action button\n $actions['processing'] = array(\n 'url' => wp_nonce_url( admin_url( 'admin-ajax.php?action=woocommerce_mark_order_status&status=processing&order_id=' . $order->get_id() ), 'woocommerce-mark-order-status' ),\n 'name' => __( 'Processing', 'woocommerce' ),\n 'action' => 'processing',\n );\n\n // Display "complete" action button\n $actions['complete'] = array(\n 'url' => wp_nonce_url( admin_url( 'admin-ajax.php?action=woocommerce_mark_order_status&status=completed&order_id=' . $order->get_id() ), 'woocommerce-mark-order-status' ),\n 'name' => __( 'Complete', 'woocommerce' ),\n 'action' => 'complete',\n );\n\n // Display custom status action buttons\n if( $order->get_status() !== $action_slug ) {\n // Set the action button\n $actions[$action_slug] = array(\n 'url' => wp_nonce_url( admin_url( 'admin-ajax.php?action=woocommerce_mark_order_status&status='.$action_slug.'&order_id='.$order->get_id() ), 'woocommerce-mark-order-status' ),\n 'name' => $name,\n 'action' => $action_slug,\n );\n }\n }\n }\n return $actions;\n}\n\n// Set styling for custom order status action button icon and List icon\nadd_action( 'admin_head', 'add_custom_order_status_actions_button_css' );\nfunction add_custom_order_status_actions_button_css() {\n // The key slug defined from status with the icon code\n $action_slugs = array(\n 'shipped' => '\\e019',\n 'readytocollect' => '\\e029'\n );\n ?>\n <style>\n <?php foreach ( $action_slugs as $action_slug => $icon_code ) : ?>\n .wc-action-button-<?php echo $action_slug; ?>::after {\n font-family: woocommerce !important; content: "<?php echo $icon_code; ?>" !important;\n }\n <?php endforeach; ?>\n </style>\n <?php\n}\n\n// Adding action for custom statuses\nadd_filter( 'woocommerce_email_actions', 'custom_email_actions', 20, 1 );\nfunction custom_email_actions( $action ) {\n $actions[] = 'woocommerce_order_status_wc-shipped';\n $actions[] = 'woocommerce_order_status_wc-readytocollect';\n\n return $actions;\n}\n\nadd_action( 'woocommerce_order_status_wc-shipped', array( 'WC_Emails', 'send_transactional_email' ), 10, 1 );\nadd_action( 'woocommerce_order_status_wc-readytocollect', array( 'WC_Emails', 'send_transactional_email' ), 10, 1 );\n\n\n// Sending an email notification when order get a custom status\nadd_action('woocommerce_order_status_shipped', 'cutom_status_trigger_email_notification', 10, 2 );\nadd_action('woocommerce_order_status_readytocollect', 'cutom_status_trigger_email_notification', 10, 2 );\nfunction cutom_status_trigger_email_notification( $order_id, $order ) {\n WC()->mailer()->get_emails()['WC_Email_Customer_Processing_Order']->trigger( $order_id );\n}\n\n// Customize email heading for your custom statuses email notifications\nadd_filter( 'woocommerce_email_heading_customer_processing_order', 'custom_email_heading_for_custom_order_status', 10, 2 );\nfunction custom_email_heading_for_custom_order_status( $heading, $order ){\n // Here your custom statuses slugs / Heading texts pairs\n $data = array(\n 'shipped' => __('Planet Vape {order_number} has been Shipped!','woocommerce'),\n 'readytocollect' => __('Planet Vape {order_number} is Ready to Collect!','woocommerce')\n );\n\n // Loop through each custom status / heading text pairs\n foreach ( $data as $custom_status => $heading_text ) {\n // Change an email notification heading text when a order get a custom status\n if( $order->has_status( $custom_status ) ) {\n $email = WC()->mailer()->get_emails()['WC_Email_Customer_Processing_Order']; // Get the specific WC_emails object\n\n $heading = $email->format_string( $heading_text ); // don't return directly\n }\n }\n return $heading;\n}\n\n// Customize email subject for your custom statuses email notifications\nadd_filter( 'woocommerce_email_subject_customer_processing_order', 'custom_email_subject_for_custom_order_status', 10, 2 );\nfunction custom_email_subject_for_custom_order_status( $subject, $order ){\n // Here your custom statuses slugs / Heading texts pairs\n $data = array(\n 'shipped' => __('Planet Vape {order_number} has been Shipped!','woocommerce'),\n 'readytocollect' => __('Planet Vape {order_number} is Ready to Collect!','woocommerce')\n );\n\n // Loop through each custom status / subject text pairs\n foreach ( $data as $custom_status => $subject_text ) {\n // Change an email notification heading text when a order get a custom status\n if( $order->has_status( $custom_status ) ) {\n $email = WC()->mailer()->get_emails()['WC_Email_Customer_Processing_Order']; // Get the specific WC_emails object\n\n $subject = $email->format_string( $subject_text ); // don't return directly\n }\n }\n return $subject;\n}\nRun Code Online (Sandbox Code Playgroud)\n代码位于活动子主题(或活动主题)的functions.php 文件中。经过测试并有效。
\n| 归档时间: |
|
| 查看次数: |
2609 次 |
| 最近记录: |