Jozef Pintér
Jozef Pintér

Reputation: 44

WooCommerce Preview Email Template. Separate sending and preview function

i have this part of code:

// load the email classs
$wc_emails = new WC_Emails();
$emails = $wc_emails->get_emails();

// select the email we want & trigger it to send
$email = $emails[ $email_class ];

// send email
$email->trigger( $wc_email_test_order_id );

// preview email content for browser
echo apply_filters( 'woocommerce_mail_content', $email->style_inline( $email->get_content_html() ) );

Everything in one function. It is working this way:

  1. select email
  2. send email
  3. preview email

I need to separate sending and preview emails.

I need to make it work without the trigger (without sending emails out)

Upvotes: 0

Views: 391

Answers (1)

Muhammad Usama
Muhammad Usama

Reputation: 160

Put a condition for preview email and add filters to disable email sending before the trigger method.

// load the email classs
$wc_emails = new WC_Emails();
$emails = $wc_emails->get_emails();

// select the email we want & trigger it to send
$email = $emails[ $email_class ];

// Disable email if preview check enable. 
add_filter( 'woocommerce_email_enabled_' . $email->id , '__return_false' );
add_filter( 'woocommerce_email_recipient_' . $email->id , '__return_false' );

// send email
$email->trigger( $wc_email_test_order_id );

// preview email content for browser
echo apply_filters( 'woocommerce_mail_content', $email->style_inline( $email->get_content_html() ) );

So, it should display preview email but don't send an email with the trigger method.

Upvotes: 1

Related Questions