• Resolved plexusllc

    (@plexusllc)


    Hi, I am looking for a way to conditionally attach an invoice to one of the WC order related emails depending on the status of the order.

    Specifically, I’d like to be able to check the box to attach an invoice to an email (it’s actually an email provided by WooCommerce Subscriptions, the customer renewal invoice email), but then not attach the invoice if $order->get_status() == 'failed'.

    Is there a way I could use the wpo_wcpdf_before_attachment_creation action or some other hook to suppress the attachment when the order status is failed?

Viewing 4 replies - 1 through 4 (of 4 total)
  • Thread Starter plexusllc

    (@plexusllc)

    I think I may have figured out how to do this for my purposes. Is there any problem with the following snippet?

    add_filter('wpo_wcpdf_custom_attachment_condition', 'ms_no_invoice_failed_orders', 10, 4);
    function ms_no_invoice_failed_orders ($attach, $order, $email_id, $document_type){
      if ($order->get_status() == 'failed') {
        return false;
      }
      return $attach;
    }
    Plugin Contributor Ewout

    (@pomegranate)

    Looks good to me! Are you getting errors with this snippet?

    Thread Starter plexusllc

    (@plexusllc)

    No errors – it’s working well as far as I can tell.

    I mainly wasn’t sure if it would be more comprehensive to do something with the $document_type argument. My client’s use-case only includes invoices at the present time, so since that’s the only document they are generating, I ignored it.

    Is there any documentation for the wpo_wcpdf_custom_attachment_condition filter anywhere? I only found it by digging through the plugin code.

    It’s a very nice plugin.

    Plugin Contributor Ewout

    (@pomegranate)

    There’s no specific documentation, it receives 4 arguments: $attach, $order, $email_id, $document_type. Return false to disable attachment, true to enable.

    Indeed you could use the $document_type variable to limit the condition to a specific document (although the free version only attaches the invoice anyway). In your case:

    
    add_filter('wpo_wcpdf_custom_attachment_condition', 'ms_no_invoice_failed_orders', 10, 4);
    function ms_no_invoice_failed_orders ($attach, $order, $email_id, $document_type){
      if ($document_type == 'invoice' && $order->get_status() == 'failed') {
        return false;
      }
      return $attach;
    }
    
Viewing 4 replies - 1 through 4 (of 4 total)

The topic ‘Hook to allow conditional email attachment?’ is closed to new replies.