• Boa noite a todos
    Estou usando Dokan com Woocomerce PDF invoices e packing slips, porém, n?o estou conseguindo inserir o campo CPF na fatura.
    E seria necessário para o lojista emitir a nota fiscal.
    Alguém sabe como resolver ?
    Valeu galera

    The page I need help with: [log in to see the link]

Viewing 9 replies - 16 through 24 (of 24 total)
  • filiperjbr

    (@filiperjbr)

    add_action ('wpo_wcpdf_before_billing_address', function ($template_type, $order) {
      if ($template_type == 'invoice') {
        $invoice = wcpdf_get_document ('invoice', $order);
        // if cnpj exists, show it
        if ( !empty($invoice-> get_custom_field ('billing_cnpj') ) {
          echo '<br>CNPJ: ' . $invoice-> get_custom_field ('billing_cnpj');
    
        // otherwise, show the cpf
        } else {
          echo '<br>CPF: '. $invoice-> get_custom_field ('billing_cpf');
        }
      }
    }, 10, 2);

    This code its work only INVOICE and Dont Work in Packing-slip

    • This reply was modified 3 years ago by filiperjbr.
    Plugin Contributor Yordan Soares

    (@yordansoares)

    @filiperjbr try with this code snippet instead:

    /**
     * Display CNPJ or CPF or after the billing address
     */
    add_action ('wpo_wcpdf_after_billing_address', function ( $document_type, $order ) {
    	// if CNPJ exists, show it
    	if ( $cnpj = $order->get_meta('billing_cnpj') ) {
    		echo 'CNPJ: ' . $cnpj;
    		// Otherwise, show the CPF (if exists)
    	} elseif ( $cpf = $order->get_meta('billing_cpf') ) {
    		echo 'CPF: ' . $cpf;
    	}	
    }, 10, 2);
    filiperjbr

    (@filiperjbr)

    dont work bro, dont get anything.

    Plugin Contributor Yordan Soares

    (@yordansoares)

    Make sure that your field are named billing_cnpj and billing_cpf too, or replace them with the actual respectively meta keys. However, I just update my code above changing the action hook from wpo_wcpdf_before_billing_address (that I took from your latest code) with wpo_wcpdf_after_billing_address, that could have more sense for these custom fields, so please use this latest version instead.

    filiperjbr

    (@filiperjbr)

    My field names are billing_cnpj and billing_cpf because in pdf invoice its work code, only and packing-slip no.

    Plugin Contributor Yordan Soares

    (@yordansoares)

    You’re right. The reason it’s because the packing slip shows the shipping address instead.

    Please replace my previous code snippet with this one:

    /**
     * Display CNPJ or CPF or after the billing address
     */
    add_action ('wpo_wcpdf_after_billing_address', function( $document_type, $order ) {
    	if ( $document_type == 'invoice' ){
    		echo get_cnpj_or_cpf( $order );
    	}	
    }, 10, 2);
    add_action ('wpo_wcpdf_after_shipping_address', function( $document_type, $order ) {
    	if ( $document_type == 'packing-slip' ){
    		echo get_cnpj_or_cpf( $order );
    	}
    }, 10, 2);
    function get_cnpj_or_cpf( $order ) {
    	// if CNPJ exists, get it
    	if ( $cnpj = $order->get_meta('billing_cnpj') ) {
    		$output = 'CNPJ: ' . $cnpj;
    		// Otherwise, get the CPF (if exists)
    	} elseif ( $cpf = $order->get_meta('billing_cpf') ) {
    		$output = 'CPF: ' . $cpf;
    	}
    	if ( ! empty( $output ) ) {
    		return sprintf( '<div>%s</div>', $output );
    	}	
    }
    filiperjbr

    (@filiperjbr)

    Thank you very much for your time, now I know the error was in the shipping_address, based on that I made the following modifications.
    Now it works on invoice and packing-slip. Thank you very much

    add_action ('wpo_wcpdf_after_shipping_address', function ($template_type, $order) {
      if ($template_type == 'packing-slip') {
        $invoice = wcpdf_get_document ('packing-slip', $order);
        // if cnpj exists, show it
        if ( !empty($invoice-> get_custom_field ('billing_cnpj') ) ) {
          echo '<br>CNPJ:' . $invoice-> get_custom_field ('billing_cnpj');
    
        // otherwise, show the cpf
        } else {
          echo '<br>CPF: '. $invoice-> get_custom_field ('billing_cpf');
        }
      }
    }, 10, 2);

    and

    add_action ('wpo_wcpdf_after_billing_address', function ($template_type, $order) {
      if ($template_type == 'invoice') {
        $invoice = wcpdf_get_document ('invoice', $order);
        // if cnpj exists, show it
        if ( !empty($invoice-> get_custom_field ('billing_cnpj') ) ) {
          echo '<br>CNPJ:' . $invoice-> get_custom_field ('billing_cnpj');
    
        // otherwise, show the cpf
        } else {
          echo '<br>CPF: '. $invoice-> get_custom_field ('billing_cpf');
        }
      }
    }, 10, 2);

    i′m using code snippet plugin.

    Plugin Contributor Yordan Soares

    (@yordansoares)

    That works too, but you’re calling the document object, in order to use the get_custom_field() that is not necessary (your code adds several extra database queries). That’s why I said previously that I improved the code snippet, because I use the WooCommerce get_meta() core method directly.

    filiperjbr

    (@filiperjbr)

    understood, and thank you so much to explain, but your last code, dont worked.

Viewing 9 replies - 16 through 24 (of 24 total)
  • The topic ‘CPF na Fatura’ is closed to new replies.