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.