A few days ago my customer gave me some information to change in his online shop, and one of them was change the VAT of shipping tax, because in Spain (i don’t know at the rest) works diferent for food products.
i was able to change it in few sites but i’m not able to change it in order table. Researching i found that I have to modify a wordpress function, specifically a woocommerce function called wp_price (location: woocommerce/includes/wc-formatting-functions.php) which gives me a value.

<?php 
                    // Get items from the order
                    $line_items = $order->get_items( apply_filters( 'woocommerce_admin_order_item_types', 'line_item' ) );

                    $import4 = 0; //total value for products with 4%
                    $import10 = 0; //total value for products with 10%
                    $import21 = 0; //total value for products with 21%

                    foreach ( $line_items as $item_id => $item2 ) {
                        if($item2["tax_class"] == "superreduit"){ // TAX NAME FOR 4%
                            $import4 += $item2["line_total"];

                        }else if($item2["tax_class"] == "reduit"){ // TAX NAME FOR 10%
                            $import10 += $item2["line_total"];

                        }else if($item2["tax_class"] == ""){ // TAX FOR THE REST (21%)
                            $import21 += $item2["line_total"];
                        }
                    }

                    $importTotal = $import10+$import21+$import4; //

                    // HERE I ESTIMATE THE RATE FOR EACH VAT
                    $proporcio4 = $import4/$importTotal;
                    $proporcio10 = $import10/$importTotal;
                    $proporcio21 = $import21/$importTotal;

                    // GET THE SHIPPING TOTAL AS A BASE
                    $base = $order->get_total_shipping();

                    // GET THE ORDER TAXES AND THEN I CALCULATE A VAT
                    $order_taxes = $order->get_taxes();
                    foreach ( $order_taxes as $tax_item ) {
                        if($tax_item['tax_amount'] != ''){
                            if($tax_item['name'] == "ES-21%-1"){
                                $taxa += ($base*$proporcio21)*0.21;
                            }else if($tax_item['name'] == "ES-10%-1"){
                                $taxa += ($base*$proporcio10)*0.1;
                            }else if($tax_item['name'] == "ES-4%-1"){
                                $taxa += ($base*$proporcio4)*0.04;
                            }
                        }
                    }
                     ?>  

That’s my own code which works correctlly.
The problem is that in the function i could’t use ” $order_taxes = $order->get_taxes();” neither “$line_items = $order->get_items( apply_filters( ‘woocommerce_admin_order_item_types’, ‘line_item’ ) );
“, basically i couldn’t use $order.
I tried to do the same with WC()->cart, WC()->order, and things like that without results.
The function is the following:

function wc_price( $price, $args = array() ) {
extract( apply_filters( 'wc_price_args', wp_parse_args( $args, array(
    'ex_tax_label'       => false,
    'currency'           => '',
    'decimal_separator'  => wc_get_price_decimal_separator(),
    'thousand_separator' => wc_get_price_thousand_separator(),
    'decimals'           => wc_get_price_decimals(),
    'price_format'       => get_woocommerce_price_format()
) ) ) );

$negative        = $price < 0;
$price           = apply_filters( 'raw_woocommerce_price', floatval( $negative ? $price * -1 : $price ) );
$price           = apply_filters( 'formatted_woocommerce_price', number_format( $price, $decimals, $decimal_separator, $thousand_separator ), $price, $decimals, $decimal_separator, $thousand_separator );

if ( apply_filters( 'woocommerce_price_trim_zeros', false ) && $decimals > 0 ) {
    $price = wc_trim_zeros( $price );
}

$formatted_price = ( $negative ? '-' : '' ) . sprintf( $price_format, '<span class="woocommerce-Price-currencySymbol">' . get_woocommerce_currency_symbol( $currency ) . '</span>', $price );
$return          = '<span class="woocommerce-Price-amount amount">' . $formatted_price . '</span>';

if ( $ex_tax_label && wc_tax_enabled() ) {
    $return .= ' <small class="woocommerce-Price-taxLabel tax_label">' . WC()->countries->ex_tax_or_vat() . '</small>';
}

return apply_filters( 'wc_price', $return, $price, $args );

}

I have to modify $price or $formatted_price.
Thank you so much for any help.

Read more here: I have to edit wc_price function


Solution:

If you know the solution of this issue, please leave us a reply in Comment section, to update the question.


Wordpress related questions and answers: