get_post_by_title( $coupon_code, OBJECT, 'shop_coupon' ); // phpcs:ignore
$sanitized_coupon_code = sanitize_title( $coupon_code ); // The generated string will be checked in an array key to locate post object.
if ( empty( $posts ) || ! array_key_exists( $sanitized_coupon_code, $posts ) ) {
return;
}
$post = ( ! empty( $posts[ $sanitized_coupon_code ] ) ) ? $posts[ $sanitized_coupon_code ] : null;
$revert = false;
if ( ! empty( $post->post_status ) && 'publish' !== $post->post_status ) {
$args = array(
'ID' => $post->ID,
'post_status' => 'publish',
);
wp_update_post( $args );
$revert = true;
}
WC()->mailer();
if ( class_exists( 'WC_SC_Email_Coupon' ) ) {
$email_coupon = new WC_SC_Email_Coupon();
$email_args = array(
'coupon' => array(
'code' => $coupon_code,
'amount' => 0,
),
);
$email_coupon->email_args = wp_parse_args( $email_args, $email_coupon->email_args );
$email_coupon->set_placeholders();
$email_content = $email_coupon->get_content();
// Replace placeholders with values in the email content.
$email_content = ( is_callable( array( $email_coupon, 'format_string' ) ) ) ? $email_coupon->format_string( $email_content ) : $email_content;
if ( true === $revert ) {
$args = array(
'ID' => $post->ID,
'post_status' => 'auto-draft',
);
wp_update_post( $args );
$revert = false;
}
ob_start();
wc_get_template( 'emails/email-styles.php' );
$css = ob_get_clean();
$css = apply_filters( 'woocommerce_email_styles', str_replace( '"', "'", $css ), $email_coupon );
ob_start();
echo '
'; // phpcs:ignore
echo $email_content; // phpcs:ignore
echo ob_get_clean(); // phpcs:ignore
}
}
?>