my php is pretty basic, also this is currently sandbox code which is why it is SO crude!
Basically, I have a loop that gets product details - Sku, Name and Tax Percentage.
After getting the tax value I have an if else to check if the Tax is equal to 20.
If it is, it outputs the product price ($vatprice). This works to this point.
What I want to do is add all the prices together and output outside the loop. So essentially add each occurrence of $vatprice together and output the total sum.
I'm not sure I'm going about this the right way?
foreach ($order->getAllItems() as $item) {
echo '<strong>Sku:</strong> ' . $item->getSku() . '<br/>';
echo '<strong>Name:</strong> ' . $item->getName() . '<br/>';
$taxpercent = $item->getData('tax_percent');
echo '<strong>VAT Percentage:</strong> ' . $taxpercent . '<br/>';
if ($taxpercent == 20) {
$vatprice = $item->getPrice();
echo $vatprice;
echo '<br/><br/>';
} else {
echo '<strong>0%</strong>';
echo '<br/><br/>';
}
}
My Answer:
>products as $product) {
$sku = $product['sku'];
$name = $product['name'];
$tax_percentage = $product['tax_percentage'];
if ($tax_percentage == 20) {
$vatprice = $product['price'];
// Output the product price
echo "Product Price: $vatprice
";
// Add the product price to a total sum variable
$total_sum += $vatprice;
}
}
// Output the total sum of all product prices with 20% tax
echo "Total Sum of Product Prices with 20% Tax: $total_sum";
Rate this post
3 of 5 based on 8909 votesComments