• Hi,

    for some of my product I need to send an additional pdf (not an invoice) to my customers.
    With the help of this post:
    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.
    Then I tried to change the code to filter by product sku.

    In this post I found some infos about the variables that are available and read about some changes for WP3:
    https://stackoverflow.com/questions/39401393/how-to-get-woocommerce-order-details/44708344#44708344

    Here my code that unfortunately doesn’t work and is not attaching anything to the confirmation email anymore:

    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;
      }

    Can anyone help me fixing it?
    How would I have to change it to check for product category instead of SKU?

    A general question would be: How can I debug this php code? Is there a way to show e.g. variables like email_id etc to check if the code gets correct values?

    Thank you very much,
    Toby

Viewing 2 replies - 1 through 2 (of 2 total)
  • Plugin Support RK a11n

    (@riaanknoetze)

    Hi there,

    Just to make sure: Have you considered using a more intuitive approach like sending a follow-up email based on the products/categories contained in the order? This can be done using https://woocommerce.com/products/follow-up-emails/ or https://woocommerce.com/products/automatewoo/ and doesn’t require debugging any code 🙂

    Thread Starter einhornmaedchen

    (@einhornmaedchen)

    Hi,
    I have considered it, but I couldn’t find any free plugin and unfortunately my business is so small that at least at the moment I can’t afford to pay those fees. On the other hand, I don’t mind debugging code while always learning something new 🙂

    Second reason would be that I do not really want to send another email to my customers. Will try later to reduce the number of emails even more and send the invoice together with the other file attached to the confirmation email. Found a nice thread for that.
    But first i have to solve this issue here 🙂

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Send specific attachment according to products sku / category’ is closed to new replies.