【问题标题】:WooCommerce how to attach custom invoice to EmailWooCommerce 如何将自定义发票附加到电子邮件
【发布时间】:2021-01-26 17:09:46
【问题描述】:

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

        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 的人,我认为解决这个问题很容易。

提前感谢您的任何帮助。

【问题讨论】:

    标签: php wordpress woocommerce woocommerce-theming woocommerce-email


    【解决方案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;
    }
    

    【讨论】:

    • 成功了!当没有名为“email_fatura”的元字段时,它不会附加任何内容。非常感谢您的帮助!
    猜你喜欢
    • 2021-12-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多