5

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?

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
Sergio Ríos
  • 71
  • 1
  • 1
  • 3
  • 1
    When exactly you need to do that and how (or what is the context)… Actually your question is just unclear. – LoicTheAztec May 03 '18 at 21:07
  • Hi, sorry about that, the idea is when a client makes an order the delivery guy go to buy the products but the price is variable so, the guy needs to add a general product with an specific price, so the moment is after the client make the order and before get paid (cash on delivery) because of that I build a page where the current order is showed and there is where I need to fill the price in a text field and add the general product with this specific price. – Sergio Ríos May 05 '18 at 16:29
  • I have answered finally… Some feed back on it will be really appreciated. Thanks. – LoicTheAztec May 20 '18 at 12:48

3 Answers3

12

You need to use the new CRUD setters methods introduced with Woocommerce 3:

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

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
  • Thank you for this! It saved the day for me. I was trying to use a custom donation form with the name your price plugin. Because of the custom form the donation price and the total were coming out different when someone donated more than the minimum donation. This helped me change the price of the donation and reflect it correctly on the receipt they were emailed. Thank you!!! – GDT Aug 02 '19 at 12:02
  • Thanks, this works for me as well very good. I would only like to ask hot exclude to calcualte the subtotal to only change product price and nothing else ? – Aljaz May 26 '20 at 21:09
  • Wondering is possible set the total include tax? – Rach Chen May 07 '21 at 03:29
1

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();`
0

@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
Jermain
  • 1
  • 1