2

I'm using a woocommerce plugin (Woocommerce pdf invoice & packing slip), I have been trying to make this happen, I was close to achieving it but it occurred some bugs when there is tax included.

So I’m looking to display a “Subtotal after discount” so our accountant can easily see the real subtotal include discount but without tax.

Here is the screenshot for the one I’m currently having:

enter image description here

So the ‘Subtotal Discounted’ section displays incorrect number when there is tax including in this order.

Here is my code so far:

/**
* Show discounted subtotal in pdf
*/
add_filter( 'wpo_wcpdf_woocommerce_totals', 'add_discounted_subtotal_to_pdf_invoices', 10, 2 );
function add_discounted_subtotal_to_pdf_invoices( $totals, $order ) {
    // Get ‘subtotal’ raw amount value
    $subtotal = strip_tags($totals'cart_subtotal']['value']);
    $subtotal = (float) preg_replace('/[^0-9.]+/', '', $subtotal);

    // Get ‘discount’ raw amount value
    $discount = strip_tags($totals['discount']['value']);
    $discount = (float) preg_replace('/[^0-9.]+/', '', $discount);

    $new_totals = array();
    // Loop through totals lines
    foreach( $totals as $key => $values ){
        $new_totals[$key] = $totals[$key];
        // Inset new calculated 'Subtotal discounted' after total discount
        if( $key == 'discount' && $discount != 0 && !empty($discount) ){
            $new_totals['subtotal_discounted'] = array(
                'label' => __('Subtotal Discounted', 'wpo_wcpdf'),
                'value' => wc_price($subtotal – $discount)
            );
        }
    }
    return $new_totals;
}

Is there any way to fix it?

Any help will be appreciated.

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399

0 Answers0