2

I hope you can help me with this little problem ... My problem is this, I want to send emails when I change the status of the customized order, in addition to changing the order status for the customer and the admin in woocommerce.

In short: Everything is created correctly, but it does not change the order status or send emails about the change of Status.

I use WooCommerce 4.7.1

I tested the following code, from creating status to sending emails.

Code just below

add_action( 'init', 'register_custom_post_status', 20 );
function register_custom_post_status() {
  register_post_status( 'separacao', array(
      'label'                     => _x( 'Área de Separação', 'Order status', 'woocommerce' ),
      'public'                    => true,
      'exclude_from_search'       => false,
      'show_in_admin_all_list'    => true,
      'show_in_admin_status_list' => true,
      'label_count'               => _n_noop( 'Seu Pedido está na Área de separação <span class="count">(%s)</span>', 'Área de Separação <span class="count">(%s)</span>', 'woocommerce' )
  ) );

  register_post_status( 'esperando-entrega', array(
      'label'                     => _x( 'Pronto para ser Entregue', 'Order status', 'woocommerce' ),
      'public'                    => true,
      'exclude_from_search'       => false,
      'show_in_admin_all_list'    => true,
      'show_in_admin_status_list' => true,
      'label_count'               => _n_noop( 'Seu Pedido está aguardando para ser entregue <span class="count">(%s)</span>', 'Seu Pedido está aguardando para ser entregue <span class="count">(%s)</span>', 'woocommerce' )
  ) );

  register_post_status( 'entregando', array(
      'label'                     => _x( 'Saiu para Entrega', 'Order status', 'woocommerce' ),
      'public'                    => true,
      'exclude_from_search'       => false,
      'show_in_admin_all_list'    => true,
      'show_in_admin_status_list' => true,
      'label_count'               => _n_noop( 'Seu Pedido está saindo para Entrega <span class="count">(%s)</span>', 'Seu Pedido está saindo para entrega <span class="count">(%s)</span>', 'woocommerce' )
  ) );
}

// Adding custom statuses to order edit pages dropdown
add_filter( 'wc_order_statuses', 'custom_wc_order_statuses', 20, 1 );
function custom_wc_order_statuses( $order_statuses ) {
  $order_statuses['separacao'] = _x( 'Indo para Separação', 'Order status', 'woocommerce' );
  $order_statuses['esperando-entrega'] = _x( 'Indo para Separação', 'Order status', 'woocommerce' );
  $order_statuses['entregando'] = _x( 'Encaminhado para Entrega', 'Order status', 'woocommerce' );

  return $order_statuses;
}

// Adding custom statuses to admin order list bulk dropdown
add_filter( 'bulk_actions-edit-shop_order', 'custom_dropdown_bulk_actions_shop_order', 20, 1 );
function custom_dropdown_bulk_actions_shop_order( $actions ) {
  $actions['mark_separacao'] = __( 'Marcar Indo para Separação', 'woocommerce' );
  $actions['mark_esperando-entrega'] = __( 'Marcar Pronto para ser Entregue', 'woocommerce' );
  $actions['mark_entregando'] = __( 'Marcar Encaminhado para Entrega', 'woocommerce' );
  return $actions;
}

// 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-separacao';
  $actions[] = 'woocommerce_order_status_wc-esperando-entrega';
  $actions[] = 'woocommerce_order_status_wc-entregando';
  return $actions;
}

add_action( 'woocommerce_order_status_wc-separacao', array( WC(), 'send_transactional_email' ), 10, 1 );
add_action( 'woocommerce_order_status_wc-esperando-entrega', array( WC(), 'send_transactional_email' ), 10, 1 );
add_action( 'woocommerce_order_status_wc-entregando', 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('separacao', 'esperando-entrega', 'entregando'); // 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(
       'separacao'         => __('Seu pedido {order_number} está sendo separado!','woocommerce'),
       'esperando-entrega' => __('Seu pedido {order_number} está esperando para ser Entregue!','woocommerce'),
       'entregando'        => __('Seu pedido {order_number} está saindo para Entrega!','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(
       'separacao'         => __('Seu pedido foi para Área de Separação','woocommerce'),
       'esperando-entrega' => __('Seu pedido esta pronto para ser Entregue!','woocommerce'),
       'entregando'        => __('Seu pedido está indo até você!','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;
}

Edit1: Code update provided by @LoicTheAztec

0 Answers0