gpt4 book ai didi

php - 将 pdf 附件添加到 WooCommerce 已完成订单电子邮件通知

转载 作者:行者123 更新时间:2023-12-02 19:59:05 25 4
gpt4 key购买 nike

在另一个线程上找到此代码,但无法使其工作。PDF 已上传至 wp-content/child-theme/。

目标是将 pdf 附加到 woocommerce 将发送的已完成订单电子邮件中。

不确定customer_completed_order是否正确?

add_filter( 'woocommerce_email_attachments', 'attach_terms_conditions_pdf_to_email', 10, 3 );
function attach_terms_conditions_pdf_to_email ( $attachments , $email_id, $email_object ) {
// Avoiding errors and problems
if ( ! is_a( $order, 'WC_Order' ) || ! isset( $email_id ) ) {
return $attachments;
}


if( $email_id === 'customer_completed_order' ){

$your_pdf_path = get_stylesheet_directory() . '/Q-0319B.pdf';
$attachments[] = $your_pdf_path;
}

return $attachments;
}

最佳答案

您的代码中存在一些错误:$email_object 函数参数是错误的变量名称,应改为 $order 以与您的第一个 if 语句匹配。

现在对于链接到主题的附件路径,您将使用:

  • get_stylesheet_directory() 用于子主题
  • get_template_directory() 用于父主题(没有子主题的网站)

电子邮件 ID customer_completed_order 对于目标客户“已完成”电子邮件通知是正确的。

由于您没有在代码中使用 $order 变量参数,!不需要 is_a( $order, 'WC_Order' ) ,因此工作代码将是:

add_filter( 'woocommerce_email_attachments', 'attach_pdf_file_to_customer_completed_email', 10, 3);
function attach_pdf_file_to_customer_completed_email( $attachments, $email_id, $order ) {
if( isset( $email_id ) && $email_id === 'customer_completed_order' ){
$attachments[] = get_stylesheet_directory() . '/Q-0319B.pdf'; // Child theme
}
return $attachments;
}

代码位于事件子主题(或事件主题)的functions.php 文件中。经过测试并可以工作。


对于父主题替换:

$attachments[] = get_stylesheet_directory() . '/Q-0319B.pdf'; // Child theme

通过以下行:

$attachments[] = get_template_directory() . '/Q-0319B.pdf'; // Parent theme

关于php - 将 pdf 附件添加到 WooCommerce 已完成订单电子邮件通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56288324/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com