Created
May 14, 2021 08:21
-
-
Save wpweb101/876db597e8ed0b79a0bcee5b1e7e540e to your computer and use it in GitHub Desktop.
WooCommerce PDF Vouchers - Redeem Voucher on Cart Total - Code Snippet for German tax rule
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
* Modify woocommerce tax rule for german | |
* Display the orginal tax without applying discount | |
*/ | |
function woo_vou_german_woocommerce_calc_tax( $tax_totals, $cart ) { | |
$count = 1; | |
foreach ( $cart->cart_contents as $key => $item) { | |
$tax_totals['TAX-'.$count]->amount = $item['line_subtotal_tax']; | |
$count++; | |
} | |
return $tax_totals; | |
}; | |
// add the filter for modify woocommerce tax rule for german | |
add_filter( 'woocommerce_cart_tax_totals', 'woo_vou_german_woocommerce_calc_tax', 999, 2 ); | |
// Filter to display Tax if order total zero | |
add_filter( 'woocommerce_cart_hide_zero_taxes', '__return_false' ); | |
add_filter( 'woocommerce_order_hide_zero_taxes', '__return_false' ); | |
/* | |
* Modify woocommerce tax rule for german | |
* Display the orginal tax without applying discount for all the order pages | |
*/ | |
function woo_vou_german_woocommerce_order_tax( $tax_totals, $order){ | |
$tax_totals = array(); | |
$tax_custom_array = array(); | |
$count = 1; | |
foreach ( $order->get_items() as $item_id => $item ) { | |
$tax_custom_array[$count] = $item->get_subtotal_tax(); | |
} | |
$count = 1; | |
foreach ( $order->get_items( 'tax' ) as $key => $tax ) { | |
$code = $tax->get_rate_code(); | |
if ( ! isset( $tax_totals[ $code ] ) ) { | |
$tax_totals[ $code ] = new stdClass(); | |
$tax_totals[ $code ]->amount = 0; | |
} | |
$tax_totals[ $code ]->id = $key; | |
$tax_totals[ $code ]->rate_id = $tax->get_rate_id(); | |
$tax_totals[ $code ]->is_compound = $tax->is_compound(); | |
$tax_totals[ $code ]->label = $tax->get_label(); | |
$tax_totals[ $code ]->amount += (float) $tax_custom_array[$count] + (float) $tax->get_shipping_tax_total(); | |
$tax_totals[ $code ]->formatted_amount = wc_price( $tax_totals[ $code ]->amount, array( 'currency' => $order->get_currency() ) ); | |
} | |
if ( apply_filters( 'woocommerce_order_hide_zero_taxes', true ) ) { | |
$amounts = array_filter( wp_list_pluck( $tax_totals, 'amount' ) ); | |
$tax_totals = array_intersect_key( $tax_totals, $amounts ); | |
} | |
return $tax_totals; | |
} | |
// add the filter for modify woocommerce tax rule for german | |
add_filter( 'woocommerce_order_get_tax_totals', 'woo_vou_german_woocommerce_order_tax', 999, 2 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment