简体   繁体   English

将附件添加到 WooCommerce 自定义电子邮件

[英]Add attachment to WooCommerce custom email

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.我想在电子邮件中附上礼物和 pdf。 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 );
     }

   }

Your code has some mistakes and outdated code您的代码有一些错误和过时的代码

  • Path file = yoursite.com/wp-content/file.pdf路径文件 = 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 );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM