简体   繁体   English

根据产品类别将 email 附件添加到 WooCommerce 通知

[英]Add an email attachment to WooCommerce notifications based on product category

For some of my product I need to send an additional pdf (not an invoice) to my customers.对于我的某些产品,我需要向我的客户发送额外的 pdf(不是发票)。

With the help of this post: https://wordpress.org/support/topic/attach-pdf-to-confirmation-email-for-specific-product/在这篇文章的帮助下: https://wordpress.org/support/topic/attach-pdf-to-confirmation-email-for-specific-product/

I was able to attach an attachment to every order confirmation email.我能够将附件附加到每个订单确认 email。 Then I tried to change the code to filter by product sku.然后我尝试更改代码以按产品 sku 过滤。

In this post I found some infos about the variables that are available and read about some changes for WP3: How to get WooCommerce order details在这篇文章中,我找到了一些关于可用变量的信息,并阅读了关于 WP3 的一些更改: 如何获取 WooCommerce 订单详细信息

Here my code that unfortunately doesn't work and is not attaching anything to the confirmation email anymore:不幸的是,我的代码在这里不起作用,并且不再向确认 email 附加任何内容:

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

$file_path = get_stylesheet_directory() . '/Lizenzen/TestAttachment.pdf'; 
$product_sku    = '1234';

if( $email_id === 'customer_processing_order' ){
        foreach ($order->get_items() as $item_key => $item ):
        $product        = $item->get_product();
        if ( $product_sku === $product->get_sku() ) {
            $attachments[] = $file_path;    
        }
    endforeach;
    }


return $attachments;
  }

How would I have to change it to check for product category instead of SKU?我将如何更改它以检查产品类别而不是 SKU?

A general question would be: How can I debug this php code?一个普遍的问题是:如何调试这个 php 代码? Is there a way to show eg variables like email_id etc to check if the code gets correct values?有没有办法显示例如 email_id 等变量来检查代码是否获得正确的值?

Following code adds the attachment based on以下代码基于添加附件

  • Product category产品分类
  • $email_id === 'customer_processing_order' . $email_id === 'customer_processing_order'

For other $email_ids , see How to target other WooCommerce order emails对于其他$email_ids ,请参阅如何定位其他 WooCommerce 订单电子邮件


Also make sure the file path is correct!还要确保文件路径正确!


So you get:所以你得到:

function filter_woocommerce_email_attachments( $attachments, $email_id, $order, $email_object = null ) {    
    // Avoiding errors and problems
    if ( ! is_a( $order, 'WC_Order' ) || ! isset( $email_id ) ) return $attachments;

    // File path
    $file_path = get_template_directory() . '/Lizenzen/TestAttachment.pdf';
    
    // Specific categories, several could be added, separated by a comma
    $specific_categories = array( 'Categorie-A', 'categorie-1' );
    
    // Email id equal to (view: https://stackoverflow.com/a/61459068/11987538)
    if ( $email_id === 'customer_processing_order' ) {
        // Loop through order items
        foreach( $order->get_items() as $item ) {
            // Product ID
            $product_id = $item->get_variation_id() > 0 ? $item->get_variation_id() : $item->get_product_id();

            // Has term (product category)
            if ( has_term( $specific_categories, 'product_cat', $product_id ) ) {
                // Push to array
                $attachments[] = $file_path;
            }
        }
    }
    
    return $attachments;
}
add_filter( 'woocommerce_email_attachments', 'filter_woocommerce_email_attachments', 10, 4 );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM