【问题标题】:Add attachment to WooCommerce custom email将附件添加到 WooCommerce 自定义电子邮件
【发布时间】:2021-12-11 04:43:21
【问题描述】:

我有这个自定义代码电子邮件触发器,它正在向客户发送带有礼物的电子邮件。

我想在电子邮件中附上带有 pdf 的礼物。我已经尝试过了,但它不起作用。有什么建议吗?

你知道我做错了什么吗?

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 );
     }

   }

【问题讨论】:

    标签: php wordpress woocommerce email-notifications


    【解决方案1】:

    您的代码有一些错误和过时的代码

    • 路径文件 = 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 );
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-10-08
      • 2018-09-19
      • 2019-04-25
      • 2021-07-27
      • 2018-09-21
      • 1970-01-01
      相关资源
      最近更新 更多