• Resolved poldacchio

    (@poldacchio)


    Hi, need your help again: I have this code you wrote me some days ago, this code adds a text in the invoice pdfs if the tax calculation equals to zero:

    add_action( ‘wpo_wcpdf_after_order_details’, ‘wpo_wcpdf_tax_exempt’, 10, 2 );
    function wpo_wcpdf_tax_exempt( $document_type, $order ) {
    // only in financial documents
    if ( !in_array( $document_type, array(‘invoice’, ‘proforma’, ‘credit-note’) ) ) {
    return;
    }

    // check if any tax was charged
    if ( $order->get_total_tax() == 0 && $order->get_total() > 0 ) {
    ?>
    <div class=”tax-free”>
    TEXT TEXT TEXT TEXT
    </div>
    <?php
    }
    }

    what can I do to make the code do the same thing but by selecting the type of tax and not whit the calculation, I better explain:
    if “zero tax” is selected -> html text 1
    if “reduced tax” i selected -> html text 2
    Thaanks again

Viewing 3 replies - 1 through 3 (of 3 total)
  • Plugin Contributor Ewout

    (@pomegranate)

    That’s significantly more difficult. You would need to get the tax lines and then loop through them:

    
    foreach ($order->get_items('tax') as $item_id => $tax) {
    	$rate = $tax->get_rate_percent();
    }
    

    With this rate (which is a float value) you can determine the appropriate value.
    Note that the get_rate_percent method was only recently introduced, with the release of WooCommerce 3.7 and this will not work on orders placed with older versions.

    Thread Starter poldacchio

    (@poldacchio)

    Sorry, I didn’t explain very well what I need… in woocommerce you can select the tax class you want to give to a product, for example the “Zero Rate” or the “Reduced Rate”, What I need is to modify the code you gave me to let it do this:

    if a product has the “zero rate” tax class -> add the “html text 1” at the end of the pdf invoice

    if a product has the “Reduced Rate” tax class -> add the “html text 2” at the end of the pdf invoice

    I don’t need to load the tax percent, but only the tax class
    I hope it is more clear now.. thanks again, I really need this code to solve a big problem

    Plugin Contributor Ewout

    (@pomegranate)

    Sure, you can loop over the order items too:

    
    foreach ($order->get_items() as $item_id => $item) {
    	if ( $product = $order->get_product_from_item( $item ) ) {
    		$tax_class = $product->get_tax_class();
    	}
    }
    

    Do keep in mind that product data is not immutable, meaning that when a product is ever deleted from your store, the invoices referring to this product will not see the tax class any longer.

    I hope you understand that we can’t do all your coding for you, this is a free plugin and we can only provide limited support. This is a the fourth topic you have opened in a short time asking for custom code. We can always point in the right direction, but 8f you need more help with coding your website I recommend finding a developer, codeable.io is a good resource for that.

    Good luck!

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘add text by tax’ is closed to new replies.