• Hello,

    I use Checkout Field Editor for WooCommerce in my web, I use a conditional for choose if the client want the invoice. In this moment the plugin send me a invoice whatever option I choosen, yes or no. I want that only send a invoice if the cliente choice option “YES” for a invoice. Can i use a script or hook that i can add for don’t send me invoice.

    My idea is link this script to conditional through javascript, I don’t know if youy have better option or if possible from your panel.

    Thanks
    Christian

Viewing 6 replies - 1 through 6 (of 6 total)
  • Plugin Contributor kluver

    (@kluver)

    Hi @christianguevara,

    You can use the wpo_wcpdf_custom_attachment_condition filter do determine if a document should be attached to an email or not. You will need the field name (meta key) of your custom checkout field and its value when the customer selects ‘Yes’.

    So for instance when your field name is attach_pdf_invoice and the value is 1 when the customer wants an invoice your filter would look like this:

    add_filter( 'wpo_wcpdf_custom_attachment_condition', 'wpo_wcpdf_send_invoice_on_customer_request', 100, 4 );
    function wpo_wcpdf_send_invoice_on_customer_request( $condition, $order, $status, $template_type ) {
    	if ( $template_type == 'invoice' ) {
    		$condition = $order->get_meta( 'attach_pdf_invoice' ) == 1 ? true : false;
    	}
    }

    If you are having trouble finding the correct meta key and value for your custom field please read the following guide: Finding WooCommerce custom fields

    The above code snippet should be added to the functions.php of your child theme or via a plugin like Code Snippets. If you have never worked with code snippets or functions.php before please read this: How to use filters

    The above filter will only prevent automatically generating and attaching of the invoice to the emails. If you want to prevent all invoice creation, so also prevent it from being manually created, you will need the wpo_wcpdf_document_is_allowed filter. You can apply the same logic:

    add_filter( 'wpo_wcpdf_document_is_allowed', 'wpo_wcpdf_allow_invoice_on_customer_request', 10, 2 );
    function wpo_wcpdf_allow_invoice_on_customer_request( $allowed, $document ) {
    	if ( $document->type == 'invoice' && !empty( $order = $document->order ) ) {
    		$allowed = $order->get_meta( 'attach_pdf_invoice' ) == 1 ? true : false;
    	}
    	return $allowed;
    }

    hello, @kluver

    thanks for the script, I have a question about the script. it works but now the invoice in not attached to the email,i still selected that if i want an invoice.

    thanks you

    Plugin Contributor alexmigf

    (@alexmigf)

    Hi @henrypaul88

    Can you post your code snippet here?

    Thread Starter christianguevara

    (@christianguevara)

    Hi @kluver @alexmigf,

    Thanks for the fast aswer, Despite of your help i can’t resolve my problem,

    I try to put your code on my web and add the function that calls a code that you put in the post to call the invoice, but it doesn’t work.

    You can tell me what I’m doing wrong.`

    I put my code in case you can help me.

    This is html

    <p class="form-row  thwcfd-field-wrapper thwcfd-field-radio" id="billing_invoice_field" data-priority="50">
        <label for="billing_invoice_invoice_yes" class="">
        ?Quieres factura por la compra?&nbsp;
         <span class="optional">(opcional)</span>
        </label>
        <span class="woocommerce-input-wrapper"><input type="radio" class="input-radio " value="invoice_yes" name="billing_invoice" id="billing_invoice_invoice_yes" checked="checked">
        <label for="billing_invoice_invoice_yes" class="radio ">Sí</label>
        <input type="radio" class="input-radio " value="invoice_no" name="billing_invoice" id="billing_invoice_invoice_no">
        <label for="billing_invoice_invoice_no" class="radio ">No</label>
        </span>
        </p>
    
        <p class="form-row form-row-wide thwcfd-field-wrapper thwcfd-field-text" id="billing_company_field" data-priority="60">
            <label for="billing_company" class="screen-reader-text">
                Nombre de la empresa&nbsp;
                <span class="optional">(opcional)</span>
            </label><span class="woocommerce-input-wrapper">
                <input type="text" class="input-text " name="billing_company" id="billing_company" placeholder="Nombre de la empresa" value="" autocomplete="organization" style="display: none;">
            </span></p>

    I want to send the invoice only if the client check.

    <label for="billing_invoice_invoice_yes" class="radio ">Sí</label>

    In in case that the client check no, don`t send it.
    <label for="billing_invoice_invoice_no" class="radio ">No</label>

    I add a conditional to call label and put in your code

    add_filter( 'wpo_wcpdf_custom_attachment_condition', 'wpo_wcpdf_send_invoice_on_customer_request', 100, 4 );
    
    //Conditional
    
    if ('invoice_yes' == get_post_meta($post->ID, 'billing_invoice_field', true)) { 
      function wpo_wcpdf_send_invoice_on_customer_request( $condition, $order, $status, $template_type ) {
        if ( $template_type == 'invoice' ) {
          $condition = $order->get_meta( 'invoice_yes' ) == 1 ? true : false;
        }
      
      }
    
    }
    Thread Starter christianguevara

    (@christianguevara)

    Hello,
    @kluver @alexmigf

    I have managed to call the function when someone marks the check option, but my WordPress gives me a fatal error, I don’t know if I’m putting it in the right place.

    The code I put in the function.php

    <!DOCTYPE html>
    <html>
        <head>
            <title>JavaScript CheckBox</title>
            <meta charset="windows-1252">
            <meta name="viewport" content="width=device-width, initial-scale=1.0">
            <script src="https://code.jquery.com/jquery-3.3.1.js"></script>
            <script type="text/javascript" src="javascript.js"></script>
        </head>
        <body>
            <p class="form-row  thwcfd-field-wrapper thwcfd-field-radio" id="billing_invoice_field" data-priority="50">
                <label for="billing_invoice_invoice_yes" class="">?Quieres factura por la compra?&nbsp;
                    <span class="optional">(opcional)</span>
                </label>
                <span class="woocommerce-input-wrapper">
                    <input type="radio" class="input-radio " value="invoice_yes" name="billing_invoice" id="billing_invoice_invoice_yes">
                    <label for="billing_invoice_invoice_yes" class="radio ">Sí</label>
                    <input type="radio" class="input-radio " value="invoice_no" name="billing_invoice" id="billing_invoice_invoice_no">
                    <label for="billing_invoice_invoice_no" class="radio ">No</label>
                </span></p>
         
                 
        
            
        </body>
    </html>
    window.addEventListener('load', inicio);
    
    function inicio(){
    
    document.getElementById("billing_invoice_invoice_no").addEventListener("click", func1);
    
    }
        function func1()
        {
    /************Here would go the function that you have provided me**************/
    	}
    }

    }
    }`

    Thanks for help!

    Plugin Contributor alexmigf

    (@alexmigf)

    Hi @christianguevara

    This is falling into custom development, something we don’t provide as free support.

    First I would check the order meta data to see if the ratio is being saved.

    If that is true the rest should be straightforward.

    • This reply was modified 3 years, 5 months ago by alexmigf.
Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Script so that it doesn’t send the invoice if I call it’ is closed to new replies.