i know my question will not be answered, but i will ask.
i want subtotal to be calculated without considering sale price, that is, to consider the regular price, i did a lot of searching on the net, but i didn't get an answer.
i think i need to change the calculation method, but i don't know where to start.
echo wc_price( $product->get_regular_price() * $item['quantity'] );
it is not used on the order viewing page because if the order is today and the price changes tomorrow, the new price will be displayed, not the price at the time of placing the order.
If you want to access price at the time of order was placed,
you will have to access Order object for that.
// Get and Loop Over Order Items
foreach ( $order->get_items() as $item_id => $item ) {
$subtotal = $item->get_subtotal();
}
Use this links for reference,
https://www.businessbloomer.com/woocommerce-easily-get-order-info-total-items-etc-from-order-object/
How to get WooCommerce order details
Related
I am currently using this solution in order to save the stock status of items at the time they are purchased,
add_action('woocommerce_checkout_create_order_line_item', 'save_stock_status_order_item_meta', 10, 4 );
function save_stock_status_order_item_meta( $item, $cart_item_key, $values, $order ) {
$item->update_meta_data( '_stock_status', $values['data']->get_stock_status() );
}
however this has a problem:
If an item has 1 item in stock and has backorder on, a user orders 2 items (1 is missing) the save_stock_status_order_item_meta will save the metadata as "instock" incorrectly, instead of as "backorder" which is what the product really is at the time of the order considering the user ordere 2 of them. This leads it to inform users that the product is instock incorrectly when in fact the quantity selected would be out of stock.
So this seems to not be correctly tackling the initial problem of customers receiving emails saying that the product is still in stock, when in fact it is not.
Thank you in advance for the attention and advice.
I would like to briefly give you some information about our problem here: We need to correct all our Woocommerce orders from 2021 and now we are facing a big challenge. Since we have changed the tax class on the products, we can't just have the order recalculated but need to re-add the entire order line item to the order and remove the old line item.
We have now found a workaround that works:
Edit corresponding purchase order
Set purchase order from "completed" to "on hold
Update/Save
Now the order can be edited:
Check which coupon has been redeemed
Remember the product ID of the order
Delete product completely
Add new product via product ID
If voucher was used Add voucher code
Update/Save
This is exactly the process we use to correct our orders as needed. From this we have programmed a bot that goes through this process in wp-admin. Unfortunately, with tens of thousands of orders, this takes months....
Now I have come across Hooks and Functions and believe there is a simpler solution here.
For example I found this thread: How Can I remove a particular product from an completed order in woocommerce?
foreach ($order->get_items() as $item_id => $item) {
if ($item_id == 3) {
wc_delete_order_item($item_id);
}
}
Here it is explained how to remove an order-item from the order.
We would need a Function that:
Opens an order
Saves the order_item_id to itself
Deletes the order_item
adds the order_item again
checks if there was a coupon
if yes: delete old voucher & add new voucher
Does anyone have any ideas or can help us with this?
Thanks a lot!
I need to add in order metadata the amount of the discount for each product individually. For example, There are ten products in the order. The user has applied a coupon discount to the products. The total amount of the discount was calculated (if there are products on_sale, they were not taken into discount calculate).
But in the end, we got a total discount. And I need to split this discount for all the products that received it and even write this value into order item metadata.
Why don't I even have a sample code? I just don't know where to start. The only thing I think is to take the total discount, divide it by the number of products that can have a discount (these are all products in the order except on_sale), And add the resulting number to the product meta. But there is a problem, I'm not sure if this is the right solution. Could you please share any advice on how to solve this?
You can start with woocommerce_checkout_create_order_line_item action hook where you will be able to set custom order item meta data. This hook has 4 arguments:
$item the order item (object),
$cart_item_key the cart item key (string),
$values the cart item (array),
$order the order (object).
So you can get from the cart item $values the discounted line item totals using:
$line_discount = $value['line_subtotal'] - $value['line_total'];
$line_discount_tax = $value['line_subtotal_tax'] - $value['line_tax'];
Then for example all together to save the line discount total as custom order item meta data:
// Save Line item discount as custom order item meta data
add_action('woocommerce_checkout_create_order_line_item', 'action_checkout_create_order_line_item', 10, 4 );
function action_checkout_create_order_line_item( $item, $cart_item_key, $values, $order ) {
$line_discount = $value['line_subtotal'] - $value['line_total'];
$line_discount_tax = $value['line_subtotal_tax'] - $value['line_tax'];
$item->update_meta_data( '_line_discount', $line_discount + $line_discount_tax );
}
Code goes in functions.php file of the active child theme (or active theme). Tested and works.
Then you can get it from an order item object $item using the WC_Data method get_meta() like:
$line_discount = $item->get_meta('_line_discount');
Related: Get Order items and WC_Order_Item_Product in WooCommerce 3
I have a plugin which applies discounts based on the category of the product. I'm using the filters below to add text to the order table in the cart:
add_filter('woocommerce_cart_product_subtotal', array($this,'change_product_subtotal'), 10, 4);
add_filter( 'woocommerce_cart_product_price', array($this,'change_product_price'), 10, 2);
add_filter( 'woocommerce_cart_item_name', array($this,'change_product_name'), 10, 2);
And this action to change the total price of the order/cart.
add_action( 'woocommerce_after_calculate_totals', array($this,'ca_change_cart_total'), 10, 1 );
The total price of the order pulls through to the checkout, as do the modified product fields. However when the order has been placed, the filters no longer have any effect (however the total order price is correct). My question is: what are the hooks I need to use to add the text present in the cart, to the placed order screen OR what is an alternative approach that will achieve the same outcome.I have provided screenshots showing the stages of the order. Thanks for any contributions.
For anyone having the same issue. I found an alternative method to using filters. Upon order creation loop through all the items in the order and change the totals. This has the advantage of effectively changing the unit price of the each product so in the WooCommerce admin area, if you are needing to issue a refund, the correct unit price is displayed. I have shown my code below, obviously replace 999 with the relevant total price for each product (ie unit price * quantity) using whatever custom logic your use case requires.
add_action('woocommerce_checkout_create_order', 'on_checkout_create_order', 20, 2);
function on_checkout_create_order( $order, $data ) {
foreach( $order->get_items() as $item_id => $line_item ){
$order->items[$item_id]->set_subtotal(999);
$order->items[$item_id]->set_total(999);
}
}
I am currently developing a webshop where i needed to do a separate price function. So far, with the help of woocommerce hooks, i had managed to manipulate the price in both the shopping cart and the checkout, this works without any problems at all. Hooks i have used woocommerce_cart_item_price, woocommerce_cart_item_subtotals, woocommerce_cart_subtotal and woocommerce_cart_total.
Now we come to my problem that i need to solve in the very near future. The price from my custom function is not included in the woocommerce order. So, is there a hook to manipulate the product prices in the order before the woocommerce creates the order?
I have looked at https://docs.woocommerce.com/wc-apidocs/hook-docs.html
but with no success.
Where does Woocommerce get the price from when it creates the order? The _price meta field, woocommerce_get_price hook, the cart or something else. I would be very grateful if someone could explain this to me. I find that woocommerce is not very consistent with where it's getting the price from.
Please ask questions if you don't understand my problem or my relative poor English. Thanks in advance.
I've use the woocommerce_get_price hook, when you change it, that changed price will be used for the cart to calculate the total (price * qty).
After order is placed, WooCommerce calculates the product based price on the total and qty, if you change one of the 2 values (total or qty) it will change the product price.
In other words, the price is dynamic after order has been created.
Edit:
Added method to change price
function change_price( $cart ) {
// Exit function if price is changed at backend
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
foreach ( $cart->get_cart() as $key => $item ) {
$item['data']->set_price( $custom_price );
}
}
add_action( 'woocommerce_before_calculate_totals', 'change_price', 10, 1);