How to get sum of shipping and payment method fees in WooCommerce - php

I'm using WooCommerce for my e-commerce store and I'd like to know if there is some option, how to get a total price (fee) of shipping and total price (fee) of payment methods and their sum on Checkout page. (shipping fee + payment fee = result). I found out how to get a shipping fee without currency symbol ($), which is what I was looking for. But I can't find out the solution how to get payment fee (total), because sometimes when you use Cash-on-delivery tax, you need to get sum of all payment method fees. And so on I need to get sum of shipping fees and payment method fees.
For now I got something like the code, which is mentioned below, but it's not as good as I need.
<?php echo $shipping_price = WC()->session->get('cart_totals') €['shipping_total'];?>
<?php foreach ( WC()->cart->get_fees() as $fee ) : ?>,
<?php $checkout_fee = wc_cart_totals_fee_html( $fee );
echo $checkout_fee;
?>
<?php endforeach; ?>
With the code above I get both values, but one is without currency symbol and the second one is with currency symbol and also placed in foreach loop. So I can't get a sum of them.
If anybody could help, I'd be very grateful.
Thanks.

Instead you could use directly some related WC_Cart methods, to get what you expect, like:
<?php
$shipping_total = WC()->cart->get_shipping_total() + WC()->cart->get_shipping_tax();
$fees_total = WC()->cart->get_fee_total() + WC()->cart->get_fee_tax();
// Output formatted total
echo '<p>Total shipping and fees: ' . wc_price( $fees_total + $shipping_total ) . '</p>';
?>

Related

Get WooCommerce subtotal AFTER coupons

I'm using this code:
WC()->cart->subtotal;
to check how much money the customer has to spend until she gets free shipping. The free shipping amount considers coupon codes though so I need to subtract any discount from WC()->cart->subtotal; before displaying the total amount. Any idea?
I've tried to do something like this:
$current = WC()->cart->subtotal - WC()->cart->discounts;
But it isn't doing anything.
Appreciate your help!
If you want display the total amount (subtotal - discount) you can write this :
WC()->cart->total;
So, you can do this for example :
$amount_for_free_shipping= 49;
$cart = round(WC()->cart->total,2);
$remaining = $amount_for_free_shipping - $cart;

Recalculate Taxes in Woocommerce

I got a Problem. In Woocommerce I added a custom product type (abo), where the user can choose a area size (in square meters) and there is a price per square meter and a dosage information where it says e.g. "You need 0.165kg per squaremeter" (it´s a fertilizer).
So for example if the user chooses 100 square meters and the price per square meter is 2.30€ and the dosage is 0.165kg/sqm the total price of the abo is 37.95€ and at a tax rate of 10.7% the tax amount should be 4.06€.
My Problem now is, I have normal products in the shop as well, where the standard calculation of the price and tax etc. works perfectly fine and you can buy the abo and a normal product.
So my question is, how can I recalculate the tax amount if I got an abo in cart?
I tried a few things for testing so far:
Adding a custom fee (that isn´t what I´m looking for, cause the tax has to be the same on cart, checkout and receipt):
add_action( 'woocommerce_cart_calculate_fees','custom_tax_surcharge_for_swiss', 10, 1 );
function custom_tax_surcharge_for_swiss( $cart ) {
if(current_user_can('administrator')) :
$percent = 10.7;
// Calculation
$surcharge = ( $cart->cart_contents_total + $cart->shipping_total ) * $percent / 100;
// Add the fee (tax third argument disabled: false)
$cart->add_fee( __( 'TAX', 'woocommerce')." ($percent%)", $surcharge, false );
endif;
}
calc_tax function (I think this one is the one I need) :
/ define the woocommerce_calc_tax callback
function filter_woocommerce_calc_tax( $taxes, $price, $rates, $price_includes_tax, $suppress_rounding ) {
if(current_user_can('administrator')) :
foreach(WC()->cart->cart_contents as $item) :
if($item['data']->get_type() == 'abo') :
$qty = $item['quantity'];
$ppqm = str_replace(',', '.', get_post_meta($item['product_id'], 'abo_product_price_per_kg')[0]);
$kgpqm = str_replace(',', '.', get_post_meta($item['product_id'], 'abo_product_kg_qm')[0]);
$realQty = floatval($kgpqm) * floatval($qty);
$priceCustom = $priceCustom + $realQty * floatval($ppqm);
endif;
endforeach;
return $taxes;
};
// add the filter
add_filter( 'woocommerce_calc_tax', 'filter_woocommerce_calc_tax', 10, 5 );
With the above function I got the problem, that I don´t have the cart contents directly so it could be, that the order of the items isn´t the same as the Tax order which I get from the function.
I would also note, that the woocommerce_calc_tax fires twice. I had for debug reasons an output in the console and if I have 2 items in cart, I got 4 outputs.
Hope my Problem is clear and that anyone could help me with that problem.
Thanks in advance.
I would look into the tax class for WooCommerce. You can specific target a item with a different tax rate. Unless I'm missing something this should actually work how you need it to out of the box.
When checking out it will also separate the taxes.

Get separate Order's tax totals amounts with tax class

I'm making personal a PDF invoice plugin for woocommerce.
Some of our products has tax with Reduced rate (%8) or Standart rate (%18). For example, Product A = Reduced rate (%8), Product B = Standart rate (%18). I can get total of tax amount easily but I want to print with sperate tax total amounts with tax class.
How to get total Reduced rate tax amount of an order? Also Standart rate.
How can I echo them separately?
The Tax class is not registered anywhere in the order data… It's registered in each product and optionally for shipping.
You can simply use the dedicated WC_Abstract_Order method get_tax_totals() (as Woocommerce uses for separated tax rows) and you will have the tax label percentage that is set in each tax line settings.
The Rate code $rate_code is made up of COUNTRY-STATE-NAME-Priority. For example: GB-VAT-1 or US-AL-TAX-1.
The code to display separated tax rows:
// Get the WC_Order instance Object from the Order ID (if needed)
$order = wc_get_order($order_id);
// Output the tax rows in a table
echo '<table>';
foreach ( $order->get_tax_totals() as $rate_code => $tax ) {
$tax_rate_id = $tax->rate_id;
$tax_label = $tax->label;
$tax_amount = $tax->amount;
$tax_f_amount = $tax->formatted_amount;
$compound = $tax->is_compound;
echo '<tr><td>' . $tax_label . ': </td><td>' . $tax_f_amount . '</td></tr>';
}
echo '</table>';
If you want to display something like Reduced rate (%8) or Standart rate (%18), you will have to customize the "Tax name" in the Tax settings for each tax line in each Tax rate (But it will be displayed everywhere and not only in your custom PDF plugin).
Additionally, the Tax class is just for settings purpose and for admin view.

How can i get order_total without taxes and shipping cost ? On woocommerce

i want put an pixel for tracking my orders for affiliate.
I must get my total order after discount, so without Tax and Shipping cost.
I've make something like this but it's display 0 .
<?php echo $woocommerce->cart->get_total_ex_tax(); ?>
It's maybe because it's display currency symbol.
This it the cart total without tax and shipping.
$cart_value = number_format( (float) $order->get_total() - $order->get_total_tax() - $order->get_total_shipping() - $order->get_shipping_tax(), wc_get_price_decimals(), '.', '' );
Vdadmax's answer is almost correct. If tax is applied on shipping, then it's deducted twice in his case (the total shipping cost including tax is deducted and after that, shipping sales tax is deducted again), leaving you with a final total that is too low.
This gives you the correct total with all sales tax and shipping deducted:
$cart_value = number_format( (float) $order->get_total() - $order->get_total_tax() - $order->get_total_shipping(), wc_get_price_decimals(), '.', '' );
I can't comment yet, hence the reason why I am adding this as an answer instead.
This helps me to get the total amount without tax.
WC()->cart->subtotal_ex_tax
Have you tried?
$cart_value = $order->get_total_tax() - $order->get_total();
get_woocommerce_totals()['cart_subtotal']['value']

Woocommerce price after discount

I am not expert on WooCommerce, i cant find how to display price that calculate on it discount from coupon.
This code, display price after coupon, but also with shipping price - what i dont want, i want to display price after discount, before shipping and tax:
$woocommerce->cart->get_cart_total();
This code display the finnal price without discount and without shipping:
$woocommerce->cart->get_cart_subtotal();
So i tried something like that to get price after discount:
<?php
$totalp = $woocommerce->cart->get_cart_subtotal();
$totaldisc = $woocommerce->cart->get_total_discount();
$resultp = $totalp - $totaldisc;
echo ($resultp);
?>
But it show "0" number.
So, i guess there is simple function for what i want (i want to get total cart price after discount, before shipping), but i cant find this function.
Answer: i was need to use int's or floats without symbols, and the best way was to check WooCommerce docs here:
Woo - Docs , and to search for this variable and use it without () (not function). now it work :)
<?php
$first_number = $woocommerce->cart->subtotal;
$second_number = $woocommerce->cart->discount_total;
$sum_total = $first_number - $second_number;
print ($sum_total);
?>
Since you have the currency in the prices, you'll need to remove them.
I couldn't find WooCommerce code to just get the subtotal & discount without currency symbol, so let's just remove them manually:
function remove_currency($price) {
return (double) preg_replace('/[^0-9\.]+/', '', $price);
}
$totalp = remove_currency($woocommerce->cart->get_cart_subtotal());
$totaldisc = remove_currency($woocommerce->cart->get_total_discount());
$resultp = $totalp - $totaldisc;
echo $resultp;
This post is a bit old but I would like to inform anyone viewing that Woocommerce 3.x now uses:
WC()->cart
or you can use just
$cart
as long as you have the name_of_my_function( $cart ){} parameter callable.
Everything above is fine... I'm sure yet in hopes someone is not aware that $woocommerce global is no long in use.
There is also a way to loop foreach through the cart items (objects) to pick your price sub-totals using the cart_object.
foreach( $cart_obj->get_cart() as $key=>$value )
{
$priced = $value['subtotal'];
$new_price = $priced - ($predefined_referral_discount * 1);
$value['data']->set_price( $new_price );
}
Remember: name_of_my_function( $cart_obj ){} for this one.
Happy Coding

Categories