• Resolved Jason Wong

    (@eljkmw)


    G’day Ewout,

    I just changed the tax rate from 6% to 0%, and I noticed that the old orders PDF invoice show the changed rate.

    By the way, I added the following snippet …

    // Change 'tax' to 'GST (0%)'
    add_filter( 'woocommerce_countries_tax_or_vat', function() {
    	return __( 'GST (0%)', 'woocommerce' );
    });

    Fortunately, when I edit/view the old orders via admin > WooCommerce >> Orders, the tax section still shows the old tax, as ‘GST (6%)’. However, when I click on the Create PDF > PDF Tax Invoice button, the generated PDF shows the changed rate, as ‘GST (0%)’.

    How do I make the generated PDF Tax Invoice get the old tax rate?

    Please advise. Thank you in advance.

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

    (@pomegranate)

    Hi Jason,
    The free Simple template does not show the tax rates by default, how did you add them?

    Thread Starter Jason Wong

    (@eljkmw)

    Hi Ewout,
    I’m using your free plugin version 2.1.3 with WooCommerce version 3.2.6.
    The tax rate (per order, and not per item) is showing on my custom template from my active theme’s “/woocommerce/pdf/custom” folder.

    In the “invoice.php” file, the section that adds the tax rate is,

    <td class="no-borders" colspan="2">
    	<table class="totals">
    		<tfoot>
    			<?php foreach( $this->get_woocommerce_totals() as $key => $total ) : ?>
    			<tr class="<?php echo $key; ?>">
    				<td class="no-borders"></td>
    				<th class="description"><?php echo $total['label']; ?></th>
    				<td class="price"><span class="totals-price"><?php echo $total['value']; ?></span></td>
    			</tr>
    			<?php endforeach; ?>
    		</tfoot>
    	</table>
    </td>

    The get_woocommerce_totals() function does display the tax rate.

    Plugin Contributor Ewout

    (@pomegranate)

    I see, that means that another plugin (or filter) is hooking into the woocommerce totals and adding the rate. It may very well be that filter you are using. Can you try disabling that to see if it resolves the issue?

    Thread Starter Jason Wong

    (@eljkmw)

    If the woocommerce_countries_tax_or_vat filter is disabled, then it reverts to “tax”.

    Thread Starter Jason Wong

    (@eljkmw)

    Hi Ewout,

    In the get_woocommerce_totals() function, I found the following lines of code

    if ( 'itemized' == get_option( 'woocommerce_tax_total_display' ) ) {
    	foreach ( $this->order->get_tax_totals() as $code => $tax ) {
    		$tax_amount         = $tax->formatted_amount;
    		$tax_string_array[] = sprintf( '%s %s', $tax_amount, $tax->label );
    	}
    } else {
    	$tax_string_array[] = sprintf( '%s %s', wc_price( $this->order->get_total_tax() - $this->order->get_total_tax_refunded(), array( 'currency' => WCX_Order::get_prop( $this->order, 'currency' ) ) ), WC()->countries->tax_or_vat() );
    }

    Since my setup isn’t displaying tax for each item (not itemized), it executes the ELSE statement. Hence, it displays the current tax rate via WC()->countries->tax_or_vat(). I’m trying to figure out how to get an existing order’s tax rate instead when the [PDF Tax Invoice] button is clicked.

    Thread Starter Jason Wong

    (@eljkmw)

    Hi Ewout,

    I managed to develop a solution to my problem, by inserting a custom wpo_wcpdf_raw_order_totals filter into template-functions.php
    The following snippet will change the tax label in the PDF Invoice according to the order’s creation date. If it was created before 01 June 2018, then it sets to “GST (6%)”. Else, it sets to “GST (0%)”.

    /* Rename tax label of PDF Invoice
    =============================================================== */
    add_filter( 'wpo_wcpdf_raw_order_totals', 'wpo_wcpdf_raw_order_totals_change_tax_label', 10, 2 );
    function wpo_wcpdf_raw_order_totals_change_tax_label( $item_totals, $order ) {
    	// get order details
    	$order_data = $order->get_data();
    	
    	// get order date_created data as date('Y-m-d') format
    	$order_date = $order_data['date_created']->date('Y-m-d');
    
    	// get name tax set by the woocommerce_countries_tax_or_vat filter
    	$gst_name = WC()->countries->tax_or_vat();
    	
    	// if order date_created is before 01 June 2018, set GST string to GST (6%)
    	if ( $order_date < date('Y-m-d', strtotime('01 June 2018')) ) {
    		$gst_string = 'GST (6%)';
    	} else {
    		$gst_string = $gst_name;
    	}
    
    	$item_totals['tax']['label'] = $gst_string;
    	
    	return $item_totals;
    }

    I needed this because whenever a customer re-downloads the PDF Invoice, those orders before 01 June 2018 will display the wrong GST percentage. This solution is custom fix for me, which I hope can be helpful to others.

    Plugin Contributor Ewout

    (@pomegranate)

    Hello Jason,
    The code you are referring to in the PDF invoice plugin with WC()->countries->tax_or_vat() is actually only applied for refunded orders. The same method is also applied by WooCommerce itself in the emails, see:
    https://github.com/woocommerce/woocommerce/blob/3.4.1/includes/class-wc-order.php#L172

    I recommend to use either the woocommerce_get_formatted_order_total filter or woocommerce_get_order_item_totals so that this is not only fixed in the PDF invoice, but also in other places where WooCommerce displays this.

    Thread Starter Jason Wong

    (@eljkmw)

    Hi Ewout,

    I thought of using the “woocommerce_get_order_item_totals” filter at first, but it was complicated and it didn’t fully solve my problem. Eventually, I found that the “wpo_wcpdf_raw_order_totals” filter was the way forward.

    I noticed your plugin doesn’t generate a REFUND PDF Invoice whenever any order is cancelled for a refund. I could probably add a condition to “invoice.php”, which can make the switch.

    Plugin Contributor Ewout

    (@pomegranate)

    Hi Jason, for refunds, we offer Credit Notes (negative invoices) in the Professional extension, you may want to look at that to save yourself some headaches as the refunds are separate orders internally.

Viewing 9 replies - 1 through 9 (of 9 total)
  • The topic ‘Changed tax rate, but old orders PDF invoice show changed rate’ is closed to new replies.