我正在尝试将pdf附加到订单响应中,其中pdf的格式为“sku.pdf”,使用此代码;
add_filter( 'woocommerce_email_attachments', 'woocommerce_attachments_filter', 10, 3 );
function woocommerce_attachments_filter ( $attachments, $status , $order, $sent_to_admin ) {
if (! $sent_to_admin ) {
$order_items = $order->get_items();
foreach ($order_items as $item) { // loop through order items
$_product = $item->get_product_from_item($item);
$sku = $_product->get_sku();
$pdf_download = 'downloads/'.$sku.'.pdf';
$attachments[] = $pdf_download;
//$joining_instructions = 'downloads/W611.pdf';
//$attachments[] = $joining_instructions;
}
}
return $attachments;
}
不幸的是它抛出错误“SyntaxError:JSON Parse error:Unrecognized token'<'” - 显然是从行$_product = $item->get_product_from_item($item);
我哪里错了?
(评论的行是我成功的,即附件代码有效)
答案 0 :(得分:1)
您可以使用 get_product_from_item()
来获取实例,而不是使用 wc_get_product()
,因为您可以轻松直接地获取产品ID $ product对象......
这是您的功能代码:
add_filter( 'woocommerce_email_attachments', 'woocommerce_attachments_filter', 10, 4 );
function woocommerce_attachments_filter ( $attachments, $status , $order, $sent_to_admin ) {
if (! $sent_to_admin ) {
foreach ($order->get_items() as $item) { // loop through order items
$product = wc_get_product($item['product_id']);
$sku = $product->get_sku();
$pdf_download = 'downloads/'.$sku.'.pdf';
$attachments[] = $pdf_download;
//$joining_instructions = 'downloads/W611.pdf';
//$attachments[] = $joining_instructions;
}
}
return $attachments;
}
同样在你的add_action()
中,你需要在这里输入4个正确数量的参数。如果没有,$sent_to_admin
可能会被忽略,或者您可能会遇到一些错误或错误......