Get separate Order's tax totals amounts with tax class - php

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.

Related

Display Flat Rate Shipping Cost for Single Product Page on WooCommerce / Wordpress

I am trying to display the flat rate shipping cost for a product on its single product page.
From searching on Stack Overflow, I did find some solutions, but none worked quite as expected.
Currently, the Geo Location is being used, so I want to base the displayed shipping cost off of that location. Each product may have more than one shipping option, but I only want to display the flat rate fee for the individual product. E.g.
In the US - Product X - Shipping Cost $Y
In the UK - Product X - Shipping Cost £Z
Currently I have the following code:
function shipping_cost_display_1() {
$rates = WC()->session->get("shipping_for_package_0")['rates'];
if (! empty($rates) ) {
foreach ( $rates as $rate_key => $rate ) {
$method_id = $rate->method_id;
$instance_id = $rate->instance_id;
$rate_id = $rate_key;
$label = $rate->label;
$cost = $rate->cost;
$taxes_array = $rate->taxes;
$update_cost = ceil($cost *= (1 + display_tax_rate_on_single_product() / 100));
if ($label == 'Shipping cost') {
//'Shipping cost' is the Flat Rate
echo $label;
echo number_format($update_cost, 2);
}
}
}
}
It is displaying on the product page, but there are some issues. It will display the correct price on one page, but then if I go to another page the same price for the previous product will be displayed. If I add something to the cart then the price displayed on all product pages will change to that, so it seems to be related to the cart somehow.
Is there a way to get the direct flat rate for the individual product rather than have it related to the cart? Since Geo Location is already active, it should be possible to simply pull the flat rate for the product based on the current location.
I assume this current issue is happening somehow due to WC()->session , but any input would be appreciated.

Get the tax rate(s) from an order in WooCommerce 3

I'm displaying my products in the cart without any taxes because I'm adding them afterwords to the subtotal sum in the cart. The problem is that I can't find a way to get the tax rate used for an order later on the My Orders page. So I'm looking for a way to get the tax rate used during the checkout for an order. The rate can be different depending on the country.
What I have is:
global $order;
$order->get_the_used_tax_rate_somehow();
To get the tax rate(s) from an order you will have to get order "tax" item(s).
You will get WC_Order_Item_Tax protected Object(s) and you have to use the dedicated available methods.
Sample code:
// Get an instance of the WC_Order Object
$order = wc_get_order($order_id);
// Loop through order tax items
foreach( $order->get_items('tax') as $item ){
$name = $item->get_name(); // Get rate code name (item title)
$rate_code = $item->get_rate_code(); // Get rate code
$rate_label = $item->get_label(); // Get label
$rate_id = $item->get_rate_id(); // Get rate Id
$tax_total = $item->get_tax_total(); // Get tax total amount (for this rate)
$ship_total = $item->get_shipping_tax_total(); // Get shipping tax total amount (for this rate)
$is_compound = $item->is_compound(); // check if is compound (conditional)
$compound = $item->get_compound(); // Get compound
}
Note: An order can have multiple tax rates (items "tax").
You can also use some related WC_Abstract_Order methods on the WC_Order Object to get:
Get the tax location for the order: $order->get_tax_location() (array).
Get all tax classes for items in the order: $order->get_items_tax_classes() (array). It will display an empty value for the "Standard" tax class.

Show shipping costs WooCommerce

I am using the cart widget from WooCommerce -> mini-cart.php
I am adding products to it via AJAX and added some elements already to it myself. Standard it shows the subtotal price in the cart but I want to show the shipping costs after it and after that the total price (subtotal + shipping).
So the cart will look like this:
- Products in cart
- Subtotal
- Shipping costs
- Total price (shipping + subtotal)
I have added the total price with this code:
<p><?php echo WC()->cart->get_cart_total(); ?></p>
But this echo's the same price as the subtotal because the shipping isn't added to it. Can someone help me to add the shipping costs to it. Some hook or code.
To get and display the chosen shipping method label (and other related data, if needed) in cart page (or in checkout page):
foreach( WC()->session->get('shipping_for_package_0')['rates'] as $method_id => $rate ){
if( WC()->session->get('chosen_shipping_methods')[0] == $method_id ){
$rate_label = $rate->label; // The shipping method label name
$rate_cost_excl_tax = floatval($rate->cost); // The cost excluding tax
// The taxes cost
$rate_taxes = 0;
foreach ($rate->taxes as $rate_tax)
$rate_taxes += floatval($rate_tax);
// The cost including tax
$rate_cost_incl_tax = $rate_cost_excl_tax + $rate_taxes;
echo '<p class="shipping-total">
<strong class="label">'.$rate_label.': </strong>
<span class="totals">'. WC()->cart->get_cart_shipping_total() .'</span>
</p>';
break;
}
}

Magento get cart single item price incl. tax

I have a pretty weird issue, I hope someone can help me with this.
Here are the major config settings that influence my problem:
Catalog prices in admin panel are shown including tax
Catalog prices in frontend are shown including tax
Items in shopping cart are shown excluding tax (so it's displayed separately near the subtotal).
Everything is working fine so far. The problem comes in a custom ajax mini cart module. I grab the collection of items from the cart, but, since I'm getting the price from the shopping cart item, I get it without tax.
Here is some code to exemplify what I mean. I will assume a 20% tax and a product that has the admin price (including tax) set to 120$, an option that costs 60$ (also including tax). Excluding tax these would be 100$ and 50$. I want to get the price + option + tax => 180$
$quote = Mage::getSingleton('checkout/session')->getQuote();
$items = $quote->getAllVisibleItems();
foreach ($items as $item) {
echo $item->getPrice(); // 150$ - price excluding tax
echo $item->getPriceInclTax(); // 150$ - price excluding tax
echo $item->getProduct()->getPrice(); // 120$ price including tax, BUT without the customer selected options.
}
PS: The custom option I am talking about is user selected, for example an install checkbox that adds +50$ to the price of the product.
- Get products id, name, price, quantity, etc. present in your cart.
- Get number of items in cart and total quantity in cart.
- Get base total price and grand total price of items in cart.
Get all items information in cart
// $items = Mage::getModel('checkout/cart')->getQuote()->getAllItems();
$items = Mage::getSingleton('checkout/session')->getQuote()->getAllItems();
foreach($items as $item) {
echo 'ID: '.$item->getProductId().'<br />';
echo 'Name: '.$item->getName().'<br />';
echo 'Sku: '.$item->getSku().'<br />';
echo 'Quantity: '.$item->getQty().'<br />';
echo 'Price: '.$item->getPrice().'<br />';
echo "<br />";
}
Get total items and total quantity in cart
$totalItems = Mage::getModel('checkout/cart')->getQuote()->getItemsCount();
$totalQuantity = Mage::getModel('checkout/cart')->getQuote()->getItemsQty();
Get subtotal and grand total price of cart
$subTotal = Mage::getModel('checkout/cart')->getQuote()->getSubtotal();
$grandTotal = Mage::getModel('checkout/cart')->getQuote()->getGrandTotal();
Have you tried:
$product->getFinalPrice();
// or this?
$product->getPriceModel()->getFinalPrice($qty, $product);
what is the ouput of $item->getOptions()?
Have you tried $item->getData('price')?
How do you apply your custom options? What is the ouput of $item->debug()? Maybe you can find what you need there.
Regards
Simon
I didn't find a solution to my exact problem, but I changed the settings to mimic this exact functionality, and the problem I encountered was no longer there.
First of all, I removed all the taxes on the site, and told magento all the prices are excluding tax (even though they are including tax).
The tax reduction is now made through a promotion applied on a custom group, so for
$tax = 20; // percent
I add a reduction of
(1 - (1 / ($tax / 100 + 1)))*100
// for 20% tax => 16.6667% reduction
// for 24% tax => 19.3548% reduction
with 4 decimals (that's as much as magento accepts). It may have an error of 1 cent from time to time - so if this is not an issue, go for it!
Now the prices all over the website will be shown exactly for the product (because the promotion is applied per cart, not per product).
You can try This :
$grandTotal = $this->helper('checkout/cart')->getQuote()->getGrandTotal();
echo $text .= $this->__(' Total: %s', $this->helper('core')->formatPrice($grandTotal, false));
show the quantity of a cart in my header
if ($parentBlock = $this->getParentBlock()) {
$count = $this->helper('checkout/cart')->getSummaryCount();
if( $count == 1 ) {
echo $text = $this->__('My Cart (%s item)', $count);
} elseif( $count > 0 ) {
echo $text = $this->__('My Cart (%s items)', $count);
} else {
echo $text = $this->__('My Cart (0 items)');
}
}
show the total price of a cart in my header
$grandTotal = $this->helper('checkout/cart')->getQuote()->getGrandTotal();
echo $text .= $this->__(' Total: %s', $this->helper('core')->formatPrice($grandTotal, false));

Magento calculates the grandtotal twice after calling collectTotals() in Ajax

I am trying to apply Shipping Amount after Discounts in Magento checkout
I'm using UPS free shipping by price, so if you total after discount is under the minimum amount for free shipping it will not grant you free shipping.
Magento calculate free shipping before discount so I calculated on my local ups.php file the correct amount after discount and applied it to the code:
$coupon_code=Mage::getSingleton('checkout/cart')->getQuote()->getCouponCode();
Mage::getSingleton("checkout/session")->setData("coupon_code",$coupon_code);
Mage::getSingleton('checkout/session')->getQuote()->setCouponCode($coupon_code)->setTotalsCollectedFlag(false)->collectTotals();
//Mage::getSingleton('checkout/cart')->getQuote()->setCouponCode($coupon_code)->collectTotals()->save();
$totals =Mage::getSingleton('checkout/session')->getQuote()->getTotals(); //Total object
$subtotal = $totals["subtotal"]->getValue(); //Subtotal value
if(isset($totals['discount']) && $totals['discount']->getValue()) {
$discount = $totals['discount']->getValue(); //Discount value if applied
} else {
$discount = '';
}
$r->setValue($request->getPackageValueWithDiscount( ) - $giftCardPrice);
if($discount!='') $r->setValueWithDiscount($subtotal-abs($discount));
else $r->setValueWithDiscount($request->getPackageValueWithDiscount( )- $giftCardPrice);
This works my problem now that in every Ajax call for shipping Magento calculates the grandtotal of the cart twice
What can I do?

Categories