Home > database >  Add attachment to WooCommerce custom email
Add attachment to WooCommerce custom email

Time:10-26

I have this custom code email triggerer which is sending an email to customer with a gift.

I want to attach the gift with the pdf in the email. I have tried this but it's not working. Any advice?

Do you have any idea what am I doing wrong?

add_action("woocommerce_order_status_changed", "dobierka_gift");

function dobierka_gift($order_id, $checkout=null) {
   global $woocommerce;
   $order = new WC_Order( $order_id );
   if( $order->has_status( 'completed' ) && 'dobirka' == $order->get_payment_method() ) {
      // Create a mailer
      $mailer = $woocommerce->mailer();
      $attachments = array(WP_CONTENT_DIR . 'PDF HERE NOT WORKING');

       $message_body = sprintf( __( 'Message here'), $order->get_billing_first_name());

      $message = $mailer->wrap_message(
        // Message head and message body.
        sprintf( __( 'Head here' ), $order->get_order_number() ), $message_body );

     
     $mailer->send( $order->billing_email, sprintf( __( 'Subject here' ), $order->get_order_number() ), $message, $attachments );
     }

   }

CodePudding user response:

Your code has some mistakes and outdated code

  • Path file = yoursite.com/wp-content/file.pdf

So you get:

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 );
  • Related