• Resolved Dave

    (@airdaveit)


    Hi everyone,

    I need help configuring the automatic generation of PDF invoices only when a specific payment method is selected. My payment option is “con_fattura”.

    I found and used this code in an old thread to exclude certain payment methods from automatic PDF generation:

    add_filter('wpo_wcpdf_custom_attachment_condition', 'wpo_wcpdf_exclude_payment_method', 100, 4);
    function wpo_wcpdf_exclude_payment_method($condition, $order, $status, $document) {
    $excluded_methods = array('stripe', 'bacs');
    $payment_method = get_post_meta($order->id, '_payment_method', true);
    if ($document == 'invoice' && in_array($payment_method, $excluded_methods)) {
    return false;
    } else {
    return $condition;
    }
    }


    Unfortunately, this code didn’t work as I wanted. So I developed this code to include the PDF only for the “con_fattura” payment method:

    add_filter('wpo_wcpdf_custom_attachment_condition', 'wpo_wcpdf_include_specific_payment_method', 10, 4);
    function wpo_wcpdf_include_specific_payment_method($condition, $order, $status, $document) {
    $included_methods = array('con_fattura');
    $payment_method = get_post_meta($order->get_id(), '_payment_method', true);
    if ($document == 'invoice' && in_array($payment_method, $included_methods)) {
    return true;
    } elseif ($document == 'invoice') {
    return false;
    } else {
    return $condition;
    }
    }

    However, this code also doesn’t work correctly. Has anyone faced and resolved a similar situation? How can I configure the PDF generation only when the selected payment method is “con_fattura”?

    Thanks in advance for your help!

    • This topic was modified 4 months, 3 weeks ago by Dave.
Viewing 3 replies - 1 through 3 (of 3 total)
  • Plugin Contributor alexmigf

    (@alexmigf)

    Hi @airdaveit

    Please try this instead:

    add_filter( 'wpo_wcpdf_document_is_allowed', function( $allowed, $document ) {
    if ( ! empty( $document->order ) && 'invoice' === $document->get_type() ) {
    $order = $document->order;
    $payment_method = is_callable( array( $order, 'get_payment_method' ) ) ? $order->get_payment_method() : '';

    if ( false === stripos( $payment_method, 'con_fattura' ) ) {
    $allowed = false;
    }
    }

    return $allowed;
    }, 10, 2 );

    Let me know.

    Thread Starter Dave

    (@airdaveit)

    Hi @alexmigf ,

    Thank you so much for the help you provided in the forum! Your solution worked perfectly, and I greatly appreciated your support.

    Thanks again!
    Dave

    Plugin Contributor alexmigf

    (@alexmigf)

    You’re welcome ??

    Have a great day!

Viewing 3 replies - 1 through 3 (of 3 total)
  • You must be logged in to reply to this topic.