combining price and government taxes - php

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.

Related

WooCommerce: Get order export with tax lines

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

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

Add extra cost per array base on key value per producto

everyone, I have the following problem I need to solve...
I'm building a shopping cart, everything was working fine until I decide to add some extras, so when you buy a product it let you add some extra items for an extra cost, so let say
Shoes $20.00 if you want to add an extra "coluor" it will cost you $10 extra...
so if you have only 1 item(one pair of shoes) it works fine, the problem is when you add an extra pair of shoes for a total of 2 items in your cart
Sport Shoes $20 + 10 per colour
Formal Shoes $30
so with that we can see that the total to be paid is only $60.00, but it shows $70.00 so why is that? what I have seen is that the next product get the extra cost from the first item, and if the next item has an extra, it get added plus the extras from the first extras of the first product... as you can see is kind of confusing.. so here is my code.
First, you need to understand how my array is build so here it is:
Normal Array for a single item:
Array
(
[cart] => Array
(
[2] => Array
(
[10] => Array
(
[quantity] => 1
[extra1] =>
[extra2] =>
)
)
)
)
So my session is that, the first ID[2] is the products ID, the second[10] is the Stock ID, the quantity and the extras... that works just fine... the problem is with the second.
Array
(
[cart] => Array
(
[2] => Array
(
[10] => Array
(
[quantity] => 1
[extra1] => Green +$10
[extra2] => Red +$10
)
)
[1] => Array
(
[7] => Array
(
[quantity] => 1
[extra1] => 0
[extra2] => 0
)
)
)
)
so that's what the session looks like, I'm able to manipulate that array in any way I need, such as, update quantity, add extras, unset product or stock and so on... also the prices with discounts are display and sum or subtracted correctly the problem are the extras... which I don't understand why I have such a hard time to manipulate...
the code that I'm using to display the information is as follow:
$s_pq = $_SESSION['cart'];
$ext1 = 0;
$ext2 = 0;
foreach ($s_pq as $kes => $values){
$qidq = $_SESSION['cart'][$kes];
foreach ($qidq as $sks => $svs ){
$ex_ex1 = $svs['extra1'];
$ex_ex2 = $svs['extra2'];
if ($ex_ex1 != '' && $ex_ex1 > 0 ) {
$extt1 += 10;
} else {$extt1 = 0;}
if ($ex_ex2 != '' && $ex_ex2 > 0 ) {
$extt2 += 10;
} else {$extt2 = 0;}
}
}
That little code is the same structure that I use to display the rest of the information:
$s_pq = $_SESSION['cart'];
foreach ($s_pq as $kes => $values){
// display product name and extra data related to that product ID
// DB Query to extract the info a few if's and echos..
$qidq = $_SESSION['cart'][$kes];
foreach ($qidq as $sks => $svs ){
// Another few DB queries to fetch information for the stock for each item
}
}
is a very simple code to work with the array from my session and is working fine, but the problem remains... so in the first code the idea is to see if the key [extra1] or [extra2] are different from " " if so it means that it has information other than " " which can be a single character but not empty and not space or zero's, if that is the case then add extra $10 if not then 0...
in another statement I use those variables $extt2 and $extt1 to add the values to the final cost
$final = round($db_price, 2) * $num_items_total + $extt1 + $extt2;
echo '$ '.$final;
with that code I display the total cost for each item in the cart.
so that is all I have for the cart session it will have nothing else but that...
last but not least here is an array for multiple size with some extras...
Array
(
[cart] => Array
(
[2] => Array
(
[10] => Array
(
[quantity] => 1
[extra1] => asdasd
[extra2] => asdasd
)
[12] => Array
(
[quantity] => 1
[extra1] =>
[extra2] =>
)
)
[1] => Array
(
[7] => Array
(
[quantity] => 1
[extra1] => as
[extra2] => asda
)
)
)
)
This is telling you that:
Name | Descr | Total
| Size 10 |
Item [2] | quantity 1 |
$20 | Extra 1 +10 | $60.00 <-- Good total
| Extra 2 +10 |
|--------------------|
| Size 12 |
| quantity 1 |
-----------------------------------------
Item [1] | quantity 1 |
$20 | Extra 1 +10 | $60.00 <-- Wrong total
| Extra 2 +10 | it should be $40.00
-----------------------------------------
Total $120.00
Thank you for taking the time, I appreciate any help you can provide!
F I X E D
I went through all my files line by line, and what i have discover was shocking! how could be so stupid!!! what happen was that I have 4 loops [loop [loop vars [loop [loop]]]] and that was the problem with the extras, the first 2 loops already show the information I need, I didn't see them because the file is damm long and I forgot about them, and the second loops I though I was outside the first 2 loops but I wasn't! and that was the problem, so I had to remove the other 2 loops and work with the first 2 loops and is working flawlessly; !! omg, 2 day!!, two day with that!! Thank you all!
So in other words, you can not use loops inside loops for the same purpose, basically was I was creating was a flood inside a loop, and for that in my case my variables never got the change to reset and that was because, the first loop was pulling the information when it was done it jump to the next key, but the loop inside was never finish so my vars never reset and that was the problem...
Again, thank you all.
Hmm its always the little things that kills code so I'll start there. I noticed that $extt1 and $extt2 are never reset in the foreach loop. This may be the source of the incrementing extra cost error. They are initialized before that loop but never reset between items.
Are you setting a default value for '$extt1' & '$extt2'? I see that you are setting:
$ext1 = 0;
$ext2 = 0;
But then you are setting:
$extt1 += 10;
And then you are totaling up all of the '$extt1' variables, but not resetting them for each loop.
So, the first time that you use it, it should be set to zero and then if it matches conditions, it will modify it. The next time you use it, since you're not setting it to zero first, '$extt1' still equals 10.
Move
$ext1 = 0;
$ext2 = 0;
inside your foreach loop
$s_pq = $_SESSION['cart'];
foreach ($s_pq as $kes => $values){
$ext1 = 0;
$ext2 = 0;
$qidq = $_SESSION['cart'][$kes];
foreach ($qidq as $sks => $svs ){
$ex_ex1 = $svs['extra1'];
$ex_ex2 = $svs['extra2'];
if ($ex_ex1 != '' && $ex_ex1 > 0 ) {
$extt1 += 10;
} else {$extt1 = 0;}
if ($ex_ex2 != '' && $ex_ex2 > 0 ) {
$extt2 += 10;
} else {$extt2 = 0;}
}
}

Get Number and Name From Array PHP

I've created an array that contains the elements of an order:
Array ( [0] => M8 [1] => M8 [2] => U- )
(it gets the first 2 chars from the title in the foreach loop - its all i need)
I need to know how many M8's come up in this array.
basically if they buy 2 or more M8's then they get a free item. But if they buy 1 M8 and a U- they get nothing.
There will also be other products too:
Array ( [0] => M8 [1] => M8 [2] => U- [3] => D8)
if they buy an M8 and a D8 they get a free item as well
so its not just based off quantity its based off how many items they buy and the product ID (m8,d8 ect..)
a free item with any d8 and m8 purchase, or free item with any 2 d8 or and 2 m8
thanks.
There's a function for that:
http://us3.php.net/manual/en/function.array-count-values.php
Should return an array like array('M8' => 2, 'U-' => 1, etc. etc.)
Edit: For example.
<?php
$counts = array_count_values($input);
if(isset($counts['M8']) && $counts['M8'] >= 2)
{
//Do special things when 2 M8's are present.
}

Categories