Wyświetlanie aktualnej stawki podatkowej na podstronie produktu WooCommerce

Poniższy kod wyświetli aktualną stawkę podatkową na podstronie produktu. Za pomocą kodu można również wyświetlić aktualną klasę podatkową produktu – get_tax_class().

blank
add_action( 'woocommerce_single_product_summary', 'display_tax_rate_on_single_product', 15 );
function display_tax_rate_on_single_product() {
    global $product; // The current WC_Product Object instance

    // Get an instance of the WC_Tax object
    $tax_obj = new WC_Tax();
    
    // Get the tax data from customer location and product tax class
    $tax_rates_data = $tax_obj->find_rates( array(
        'country'   => WC()->customer->get_shipping_country() ? WC()->customer->get_shipping_country() : WC()->customer->get_billing_country(),
        'state'     => WC()->customer->get_shipping_state() ? WC()->customer->get_shipping_state() : WC()->customer->get_billing_state(),
        'city'      => WC()->customer->get_shipping_city() ? WC()->customer->get_shipping_city() : WC()->customer->get_billing_city(),
        'postcode'  => WC()->customer->get_shipping_city() ? WC()->customer->get_shipping_city() : WC()->customer->get_billing_city(),
        'tax_class' => $product->get_tax_class()
    ) );
    
    // Finally we get the tax rate (percentage number) and display it:
    if( ! empty($tax_rates_data) ) {
        $tax_rate = reset($tax_rates_data)['rate'];
    
        // The display
        printf( '<span class="tax-rate">' . __("Cena zawiera %s VAT", "woocommerce") . '</span>',  $tax_rate . '%' );
    }
}

Dodatkowy kod, który wyświetla VAT-marża:


if($product->get_tax_class() == 'zero-rate'){
	echo '<span class="tax-rate-new">' . __("Final price, according to §25a UStG this article is sold on margin scheme.", "woocommerce") . '</span>';
}else{
	if( ! empty($tax_rates_data) ) {
		$tax_rate = reset($tax_rates_data)['rate']; 
		printf( '<span class="tax-rate-new">' . __("Gross Price including tax.", "woocommerce") . '</span>',  $tax_rate . '%' );
	}else{ 
		printf( '<span class="tax-rate-new">' . __("Final price.", "woocommerce") . '</span>');
	}
}