1

在一些帮助下,我设法创建了一个插件,以在完成的订单电子邮件中附加发票。

        add_filter( 'woocommerce_email_attachments', 'attach_terms_conditions_pdf_to_email', 10, 3);
    
    function attach_terms_conditions_pdf_to_email ( $attachments, $status , $order ) {
        $allowed_statuses = array( 'customer_completed_order' );
    
        if( isset( $status ) && in_array ( $status, $allowed_statuses ) ) {
            $pdf_name = get_post_meta( get_the_id(), 'email_fatura', true );
            $pdf_path = get_home_path() . '/Faturas/GestaoDespesas/' . $pdf_name;
            $attachments[] = $pdf_path;
        }
    
        return $attachments;
    }

此代码用于检查“email_fatura”(翻译为“email_invoice”)的订单元数据,并获取该字段的值。此值采用路径根/Faturas/GestaoDespesas/ORDER123.pdf并将其附加pdf到电子邮件。

但是,问题是当没有“email_fatura”字段时,它仍然附加一个名为“GestaoDespesas”的文件,该文件来自/Faturas/**GestaoDespesas**/

对于那些了解 PHP 的人,我认为很容易解决这个问题。

提前感谢您的任何帮助。

4

1 回答 1

1

我会首先检查该字段是否为空,如果是则返回:

add_filter( 'woocommerce_email_attachments', 'attach_terms_conditions_pdf_to_email', 10, 3);

function attach_terms_conditions_pdf_to_email ( $attachments, $status , $order ) {
    $allowed_statuses = array( 'customer_completed_order' );
 
    $pdf_name = get_post_meta( get_the_id(), 'email_fatura', true );

    if ( empty($pdf_name) ){
        return;
    }

    if( isset( $status ) && in_array ( $status, $allowed_statuses ) ) {
        $pdf_name = get_post_meta( get_the_id(), 'email_fatura', true );
        $pdf_path = get_home_path() . '/Faturas/GestaoDespesas/' . $pdf_name;
        $attachments[] = $pdf_path;
    }

    return $attachments;
}
于 2021-01-26T19:58:45.463 回答