I want to export only the coupon totals from items with reduced tax rate (the totals you can see in the image below).
With the totals of these items I do the same. A great answer helped me to export only the totals of order items with reduced tax rate. For that I'm using the following code:
// gets the total of the order items by tax class
function get_total_order_items_by_tax_class( $order_id, $tax_class = 'reduced-rate' ) {
$order = wc_get_order( $order_id );
// initializes the total of the order items
$total = 0;
foreach( $order->get_items() as $item_id => $order_item ) {
// if the product tax class is equal to "$tax_class"
if ( $tax_class == $order_item['tax_class'] ) {
// sum the total
$total += $order_item['total'];
}
}
return $total;
}
I tried something similar and added the line (found here):
$order->get_discount_total();
to the snippet. But this exports the whole discount for every item of every tax class.
I also tried the following code from this answer:
foreach( $order->get_coupon_codes() as $coupon_code ) {
// Get the WC_Coupon object
$coupon = new WC_Coupon($coupon_code);
$discount_type = $coupon->get_discount_type(); // Get coupon discount type
$coupon_amount = $coupon->get_amount(); // Get coupon amount
}
But it's also the discount for the whole order.
Is there any way to get the discount totals only for items with reduced tax rates?
I believe that there must be a way because the order shows these totals below every item.
But I couldn't find a way to get these discounts.
I saw that $order
only contains the coupon total and the coupon tax. Not per line item. And it seems that $order_item
doesn't contain any discounts. Only something line WC_Coupon_Data_Store_CPT
.
My Answer:
To get the discount total only for items with a specific tax class, you can modify the `get_total_order_items_by_tax_class` function to include the discount amount for items with the specified tax class. Here's an updated version of the function:
php
// gets the total discount of the order items by tax class
function get_discount_total_order_items_by_tax_class( $order_id, $tax_class = 'reduced-rate' ) {
$order = wc_get_order( $order_id );
// initializes the total discount of the order items
$total_discount = 0;
foreach( $order->get_items() as $item_id => $order_item ) {
// if the product tax class is equal to "$tax_class"
if ( $tax_class == $order_item->get_tax_class() ) {
// get the discount amount for the item
$item_discount = $order_item->get_discount_total();
// sum the total discount
$total_discount += $item_discount;
}
}
return $total_discount;
}
You can then call this function with the order ID and the tax class you want to filter by to get the total discount amount for items with that tax class. For example:
php
$order_id = 123; // replace with your order ID
$tax_class = 'reduced-rate';
$discount_total = get_discount_total_order_items_by_tax_class( $order_id, $tax_class );
echo 'Total discount for items with tax class '.$tax_class.': '.$discount_total;
This updated function should now correctly calculate the total discount amount for items with the specified tax class.
Rate this post
5 of 5 based on 5974 votesComments