Skip to content

Instantly share code, notes, and snippets.

@hslaszlo
Created December 8, 2023 17:25
Show Gist options
  • Save hslaszlo/5b02331b520c0cfdb26944ce7805fb96 to your computer and use it in GitHub Desktop.
Save hslaszlo/5b02331b520c0cfdb26944ce7805fb96 to your computer and use it in GitHub Desktop.
Add attachment to WooCommerce custom email
<?php
// Path file = yoursite.com/wp-content/file.pdf
function action_woocommerce_order_status_changed( $order_id, $old_status, $new_status, $order ) {
// Compare
if ( $new_status == 'completed' && $order->get_payment_method() == 'dobirka' ) {
// Mailer
$mailer = WC()->mailer();
// To, subject, message
$to = $order->get_billing_email();
$subject = __( 'My subject', 'woocommerce' );
$message_body = __( 'My message', 'woocommerce' );
// Message head and message body.
$message = $mailer->wrap_message( sprintf( __( 'Message here %s', 'woocommerce' ), $order->get_billing_first_name() ), $message_body );
// Headers
$headers = 'Content-Type: text/html\r\n';
// Path to file
$attachments = array( WP_CONTENT_DIR . '/file.pdf' );
// Send an email
$mailer->send( $to, $subject, $message, $headers, $attachments );
}
}
add_action( 'woocommerce_order_status_changed', 'action_woocommerce_order_status_changed', 10, 4 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment