1

I am trying to create a simple shortcode [resend_woo_emails]. This shortcode will create a form with a dropdown with all email classes, an input field for cc emails, and another input field for order_id (Later it could be a dropdown of all orders).

Here's my code so far -

function resend_woocommerce_emails_shortcode() {
 if ( isset( $_POST['email_resend_submit'] ) ) {
    $email_type = isset( $_POST['email_type'] ) ? sanitize_text_field( $_POST['email_type'] ) : '';
    $cc_emails = isset( $_POST['cc_emails'] ) ? sanitize_text_field( $_POST['cc_emails'] ) : '';
    $order_id = isset( $_POST['order_id'] ) ? absint( $_POST['order_id'] ) : '';

    $errors = array();
    // Handling all input errors

    // If no error
    if ( empty( $errors ) ) {
       // Getting all WC_emails array objects
       $mailer = WC()->mailer()->get_emails();

       // Send the email notification
       $mailer[$email_type]->trigger( $order_id );
    }
 }
 // Get all WooCommerce email classes
 $mailer = WC()->mailer()->get_emails();
 $email_classes = array_keys( $mailer );

 // Output the form
 ob_start();

<form method="post">
   <div class="form-group">
      <label for="email_type">Select Email Type</label>
      <select name="email_type" id="email_type" class="form-control">
         <option value="">Select Email Type</option>
         <?php foreach ( $email_classes as $email_class ) : ?>
               <option value="<?php echo esc_attr( $email_class ); ?>"><?php echo esc_html( $email_class ); ?></option>
         <?php endforeach; ?>
      </select>
   </div>
    
   <div class="form-group">
      <label for="cc_emails">CC Email Addresses (separate multiple emails with commas)</label>
      <input type="text" id="cc_emails" name="cc_emails" class="form-control" />
   </div>
    
   <div class="form-group">
      <label for="order_id">Order ID</label>
      <input type="number" id="order_id" name="order_id" class="form-control" />
   </div>
    
   <button type="submit" name="email_resend_submit" value="1">Resend Email</button>
</form>
<?php
    return ob_get_clean();
}

This is just the basic version but I am not receiving emails. But I received email once... Don't know how.

I want to add the cc emails too like this - But still stuck at the first step as I am not receiving emails.

  // Add CC emails to the email recipients
  add_filter( "woocommerce_email_recipient_{$email_type}", function ( $recipient, $order ) use ( $cc_emails ) {
    return $recipient .= $cc_emails;
  }, 10, 2 );

Any help or guidance in the right direction is appreciated. Thanks.

2
  • There is a missing ?> after ob_start() which I missed when copying code here.
    – Joy
    Jul 20, 2023 at 19:12
  • If an order is in Processing, And I am trying to send On-Hold email - the email will not work, as the On-Hold email is not generated yet.... Does woo emails work like this?
    – Joy
    Jul 20, 2023 at 19:18

1 Answer 1

0

Okay... so this code actually works. The issue was you cannot resend an woo-commerce email that is not generated yet. And woo-commerce emails are generated only when status is changed.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.