在另一个线程上找到此代码,但无法使其工作。
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()
对于父主题(没有儿童主题的网站)
电子邮件 IDcustomer_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
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)