wordpress - 如何在过滤器“woocommerce_email_attachments”中获取订单 ID
问题描述
我想将发票作为 PDF 附加到 WooCommerce 邮件。适用于静态 PDF(如条款和条件),但我需要使用可变 PDF 文件(如发票)的选项。
我使用这个过滤器:
add_filter( 'woocommerce_email_attachments', 'attach_pdf_to_email', 10, 3);
这个功能:
function attach_pdf_to_email ( $attachments, $status , $object ) {
$pdf_path = ABSPATH . "wp-content/uploads/terms.pdf";
$attachments[] = $pdf_path;
return $attachments;
}
完美运行。现在我想将 $pdf_path 更改为:
$pdf_path = ABSPATH . "wp-content/uploads/terms" . $order_id . ".pdf";
但我无法获得 $order_id。
我试过了:
global $order;
// First try
$order_id = $order->id;
// Second try
$order_id = $order->get_id();
// Third and fourth try (like above)
global $post;
问题是,过滤器既不发送订单,也不发送订单 ID。有什么办法或想法,我怎么能做到这一点?
解决方案
试试这个代码。
add_filter( 'woocommerce_email_attachments', 'attach_pdf_to_email', 10, 3);
function attach_agb_to_email ( $attachments, $status , $order ) {
if ( empty( $order ) ) {
return $attachments;
}
$order_id = $order->id;
$pdf_path = ABSPATH . "wp-content/uploads/terms" . $order_id . ".pdf";
$attachments[] = $pdf_path;
return $attachments;
}