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.
Related
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.
I need to make certain changes to the file order-details-item.php Product prices are formed in custom meta fields. So in my case the value is: $qty = $item->get_quantity(); is incorrect. It is always the same.
To solve the problem, I can use the simplest arephmetic operation. Divide the total order price by the product price. For example, if a customer ordered 10 kilograms of apples at a cost of 14.5 per kilogram the total cost will be 145. This means that in order to correctly display the quantity I need 145/10.
$price_weight_array = $product_attr['_mia_cup_price_weight'];
$price_unit_array = $product_attr['_mia_cup_price_unit'];
$sale_price_array = $product_attr['_mia_cup_sale_price_unit'];
$price_weight = $price_weight_array[0];
$price_unit = $price_unit_array[0];
$sale_price = $sale_price_array[0];
$prod_item_total = $order->get_formatted_line_subtotal();
custom_quantity = $prod_item_total / $price_weight
And here is the problem $order->get_formatted_line_subtotal(); returns me a number with currency symbol. And I cannot use it in arithmetic operation. How can I remove this symbol?
Your code is incomplete and you are not using the right way… Also get_formatted_line_subtotal() method requires a mandatory argument in order to work and as you know displays the formatted order item subtotal (with currency symbol)…
Based on How to get WooCommerce order details and Get Order items and WC_Order_Item_Product in WooCommerce 3, you need first to get order items and you will use your code in a foreach loop.
To get the non formatted and non rounded order item subtotal you will use instead get_line_subtotal() method as follows:
$order = wc_get_order($order_id); // (optional - if needed) get the WC_Order object
// Loop through order items
foreach( $order->get_items() as $item_id => $item ) {
$product = $item->get_product(); // get the WC_Product Object
// Your other code (missing from your question) Here …
$price_weight = reset($product_attr['_mia_cup_price_weight']);
$price_unit = reset($product_attr['_mia_cup_price_unit']);
$sale_price_unit = reset($product_attr['_mia_cup_sale_price_unit']);
// Get the non formatted order item subtotal (and not rounded)
$item_subtotal = $order->get_line_subtotal( $item, $order->get_prices_include_tax(), false );
$custom_quantity = $item_subtotal / $price_weight;
}
It should better work
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.
I need to change the item price in a woocommerce order but everything I found is to changing the price in the cart but this is not what I need because I need to change after the checkout process.
Does somebody can give me a clue on how to do that?
You need to use the new CRUD setters methods introduced with Woocommerce 3:
For order object you will use WC_Order methods,
For order "line item" you will use WC_Order_Item_Product methods,
For both of them you could be also use some WC_Data methods like save()…
Here is a working basic example with a static price and a static order ID:
$order_id = 809; // Static order Id (can be removed to get a dynamic order ID from $order_id variable)
$order = wc_get_order( $order_id ); // The WC_Order object instance
// Loop through Order items ("line_item" type)
foreach( $order->get_items() as $item_id => $item ){
$new_product_price = 50; // A static replacement product price
$product_quantity = (int) $item->get_quantity(); // product Quantity
// The new line item price
$new_line_item_price = $new_product_price * $product_quantity;
// Set the new price
$item->set_subtotal( $new_line_item_price );
$item->set_total( $new_line_item_price );
// Make new taxes calculations
$item->calculate_taxes();
$item->save(); // Save line item data
}
// Make the calculations for the order and SAVE
$order->calculate_totals();
Then you will have to replace the static price by your submitted new price in your custom page, which is not so simple, as you will need to target the correct $item_id…
Thank you very much, I spent 4 hours looking for how to change the quantity of the product in the order and based on your code (I rewrote the necessary part) I finally got it! that's if someone needs to change the quantity product in the order `
$order = wc_get_order( $_POST['orderID'] );
foreach( $order->get_items() as $item_id => $item ){
$product = $item->get_product();
$product_price = (int) $product->get_price(); // A static replacement product price
$new_quantity = (int) $_POST['productQty'] // product Quantity
// The new line item price
$new_line_item_price = $product_price * $new_quantity;
// Set the new price
$item->set_quantity($_POST['orderQty']);
$item->set_subtotal( $new_line_item_price );
$item->set_total( $new_line_item_price );
// Make new taxes calculations
$item->calculate_taxes();
$item->save(); // Save line item data
}
// Make the calculations for the order and SAVE
$order->calculate_totals();`
#LoicTheAztec
Completing an automated woo-commerce Payment by manually inputting an identifier
The shopper goes online, creates an account , adds a payment method, and fills their cart .
We hold the amount plus 15% when they check out.
woocommerce sends order details to the delivery team that takes the gig .
They go to the store and shop
After checking out at the physical store, the new invoice total is uploaded to woo-commerce via the shopping app .
This manually entered amount will be the IDENTIFIER in the stripe that TRIGGERS the order completion
how can i programmatically get the shipping tax from an order increment_id in magento?
I try this script, but seems not to work.
$order = Mage::getModel('sales/order')->loadByIncrementId("increment_id");
$taxRefunded = $order->getTaxRefunded();
Thanks
You can retrieve tax information from order with following code, returning an array.
You can parse and use this array to get the shipping tax ammount.
$order = Mage::getModel('sales/order')->getCollection()->addAttributeToFilter('increment_id', 100000166)->getFirstItem();
$taxRefunded = $order->getFullTaxInfo();
var_dump($taxRefunded);
E.g.: to retrieve tax percent:
$tax_percent = $taxRefunded[0]['percent'];