WooCommerce: Get order export with tax lines - php

I want to export all my orders with the tax lines separated by tax rate.
For example: An order could have the tax rates 7% and 19%. If the order has products with both tax rates, I only get one combined tax amount.
Let's say the order has two products for 100€ each. There would be the following taxes:
100€ at reduced rate: 7€
100€ at default rate: 19€
taxes combined: 26€
I'm using the following code (excerpt) to export order data:
$order_data = $order->get_data(); // The Order data
$order_date_created = $order_data['date_created']->date('d.m.Y'); // Example for oder creation date
After getting the data, I need to store them in an array:
$custom_data = array(
'custom_order_date_created' => $custom_order_date_created,
)
What I need is a fixed column (like the $order_date_created above) for every tax rate.
Like:
Column 1: tax rate - 7%
Column 2: tax amount - 7€
Column 3: tax rate - 19%
Column 4: tax amount - 19€
If there is no amount for a tax rate the column could be empty.
I know that there is an order property called tax_lines. But it's an array and I don't know how to use it. The property tax_lines has some own tax lines properties.
Does anyone has a clue how to get these values from an order?
EDIT: I found a snippet in another question:
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>';
}
It looks right, but I'm not sure how to use it with my code example and store it in my array.
EDIT 2: After some more digging I found the WC_Order_Item_Tax object which has the order tax items in it.
So I tried to use that data but it is an huge object and I'm not sure how to select the data and show it in the right column.
It looks like this (there is a lot more). And I need to select the tax by the rate_id for example:
[4543] => WC_Order_Item_Tax Object
(
[extra_data:protected] => Array
(
[rate_code] =>
[rate_id] => 0
[label] =>
[compound] =>
[tax_total] => 0
[shipping_tax_total] => 0
[rate_percent] =>
)
[data:protected] => Array
(
[order_id] => 11244
[name] =>
[rate_code] => DE-MWST.-1
[rate_id] => 1
[label] => MwSt.
[compound] => 1
[tax_total] => 7.54
[shipping_tax_total] => 0
[rate_percent] => 16
)
[cache_group:protected] => order-items
[meta_type:protected] => order_item
[object_type:protected] => order_item
[id:protected] => 4543
[changes:protected] => Array
(
)
[object_read:protected] => 1
[default_data:protected] => Array
(
[order_id] => 0
[name] =>
[rate_code] =>
[rate_id] => 0
[label] =>
[compound] =>
[tax_total] => 0
[shipping_tax_total] => 0
[rate_percent] =>
)

You can use it this way may be after this code line
$custom_data = array(
'custom_order_date_created' => $custom_order_date_created,
)
Then the foreach loop with tax rate items and using WC_Tax method get_rate_percent() to get the tax rate percentage as follows:
// Loop through order tax items
foreach ( $order->get_tax_totals() as $rate_code => $tax ) {
// Store it in the array
$custom_data['taxes'][] = array(
'tax_rate' => WC_Tax::get_rate_percent($tax->rate_id), // <=== the tax rate percentage
'tax_amount' => $tax->formatted_amount
);
}
Related:
How to get order tax details and rates in WooCommerce?
Get separate Order's tax totals amounts with tax class

Related

How can I get the cod costs in WooCommerce?

I've a problem. I'm currently writing a WordPress plugin for WooCommerce which reads out an order for a booking system.
The booking system has an own field for cod costs (costs on delivery / Nachnahmegbühr (deutsch)). The problem is that I can't find a method to read this cost from an order.
If I read out the fees from the order via $order->get_fees(), I can see the cod costs but this is not safe enough cause the fee name is always in the shop language which makes it nearly impossible to read out the cod costs this way (in an international plugin):
[data:protected] => Array
(
[order_id] => 24
[name] => Zahlungsgebühr
[tax_class] => 0
[tax_status] => taxable
[amount] => 2.16
[total] => 2.16
[total_tax] => 0.34
[taxes] => Array
(
[total] => Array
(
[6] => 0.344828
)
)
)
So is there any known way to get this kind of costs?

PHP: calculate popularity/frequency of an item sold from multimensional Array

I have an array extracted from my mysql that stores item_id, day that the item_id has been sold or not sold, and the price
my query is something like
select * from freq_history_sales
where freq_item_id = 123
and the output example is given below
[0] => Array
(
[freq_item_id] => 123 // item ID / sku number etc..
[freq_end] => 2016-05-06 // when the sale ended
[freq_status] => sold // if sold or not
[freq_price] => 175 // price sold
)
[1] => Array
(
[freq_item_id] => 123
[freq_end] => 2016-05-06
[freq_status] => notsold
[freq_price] => 225
)
[2] => Array
(
[freq_item_id] => 123
[freq_end] => 2016-05-10
[freq_status] => notsold
[freq_price] => 225
)
...etc...
I would like to record the time, sold/not sold on that specific day, and final price or items and then calculate a sort of frequency/popularity of this item
I am asking if this is something that can be done in PHP. I that is the case, how to transform that frequency in a sort of index 1 to 10 so that I can judge the popularity.

getting the highest value from an associative array in and if equal, get the highest with the lowest other value

Let me try and explain this as easy as possible. I have an array which contains the shipping fees of several products.
I have the value of
Item ID
Local Shipping
International Shipping
Additional Local Shipping
Additional International Shipping
To make it easier, I just need to know how to calculate the part of the local shipping. I can afterwards apply the same formula to the international shipping part.
The array may contain 1 or 5 or 100 products, each with their own different (or same) amounts, like this:
Array (
[0] => Array (
[item_id] => 788
[local_shipping] => 13.00
[intl_shipping] => 45.00
[addl_local_shipping] => 10.00
[addl_intl_shipping] => 20.00
)
[1] => Array (
[item_id] => 234
[local_shipping] => 23.00
[intl_shipping] => 5.00
[addl_local_shipping] => 1.00
[addl_intl_shipping] => 2.00
)
[2] => Array (
[item_id] => 543
[local_shipping] => 23.00
[intl_shipping] => 6.00
[addl_local_shipping] => 0.50
[addl_intl_shipping] => 5.00
)
)
So, what I'm trying to get is the array that contains the HIGHEST local_shipping value, which in that case would be both [1] and [2] with "23.00".
If there's only one unique highest value, I need it to return the "local_shipping" and the "addl_local_shipping" like
Local Shipping is 23.00 and additional local shipping is 1.00
Now, if there's 2 arrays with common highest values, I need it to return the one with the LOWEST "addl_local_shipping" value, which in that case is [2] like:
Local Shipping is 23.00 and additional local shipping is 0.50
Does that make sense?
I was able to get the highest "local_shipping" value out of the array with this:
$max = 0;
foreach( $my_array as $k => $v )
{
$max = max( array( $max, $v['local_shipping'] ) );
}
echo "Local Shipping is " . $max;
But I have no idea how to figure out how to print the related "addl_local_shipping" string AND sort out the problem if there are multiple ones with an equal high value.
You can use usort to sort the array based on local shipping descending and additional ascending, then take the values from the 1st result in the array:
usort($array, function($a, $b){
return $b['local_shipping'] - $a['local_shipping'] ?:
$a['addl_local_shipping'] - $b['addl_local_shipping'];
});
echo 'local shipping: ' . $array[0]['local_shipping'];
echo 'additional local shipping: ' . $array[0]['addl_local_shipping'];
However, as noted in comments, if you are getting this data from an sql query, it makes more sense to let the database do the work for you with an ORDER BY clause. The result will be the same - the 1st element in the array will have the data you require

Edit weight based shipping titles in OpenCart

I'm using weight based shipping but I don't want the weight information to be displayed in the checkout page. Right now the displayed information is "USA (Weight: 0.00kg) $24.30", but all I want to be displayed is "USA $24.30".
In controller/checkout/shipping I'm printing the $this->data['shipping_methods'] variable, getting the following array:
Array
(
[weight] => Array
(
[title] => Weight Based Shipping
[quote] => Array
(
[weight_11] => Array
(
[id] => weight.weight_11
[title] => USA (Weight: 0.00kg)
[cost] => 24.3
[tax_class_id] => 0
[text] => $24.30
)
)
[sort_order] => 1
[error] =>
)
)
What I need is to, for each $this->data['shipping_methods']['weight']['quote'], change the title to a substr to minus 16 characters, in order display only the country info (USA, in this case).
How can I do this?
Open up file catalog/model/shipping/weight.php, find this line:
'title' => $result['name'] . ' (' . $this->language->get('text_weight') . ' ' . $this->weight->format($weight, $this->config->get('config_weight_class_id')) . ')',
replace with
'title' => $result['name'],
done.
Best when applied via vQmod extension.

combining price and government taxes

I have several files that are provided.
I can convert and put them in an array, however, i cant figure out how to compare the 2 arrays:
1 is the product information and pricing
eg:
Array (
[0] => Array (
[Description] => description
[ArtID] => 1111
[PartID] => 1111
[Price] => 8.59
[Stock] => 24 [
)
[1] => Array (
[Description] => ....
the other contains all items that contain a certain type of government tax.
what i need to do is have array2, with the government tax, looped to match partid in array1, then have array1's price updated with old price + tax ..
the values in array2 don't contain all items (not all items are subject to the tax),
and it does not contain the same amount of fields (less info required)
any suggestions?
$ccnt = count($product_arr);
for($c=0;$c<$ccnt;$c++){
$price_original = $product_arr['price'];
$price = $product_arr['price'];
if(isset($gov_arr['tax'])){ $price = $price + ($price * $gov_arr['tax']); } // if percent
$product_arr[$c]['price'] = $price;
$product_arr[$c]['price_orig'] = $price_original;
}
Basically loop through the products and see if gov_arr['tax'] exist. If does then do math to get new value and then replace. I also saved the original price so if needed.

Categories