Change Order item prices in Woocommerce 3 - php

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

Related

Woocommerce update stock by ID after payment

I created a custom product type for WooCommerce. With this product type it is possible to connect an other product that is exist by ID.
WooCommerce reduce the stock quantity automatically if an order is placed and the payment is successful.
For example I added a product with ID 4082 to the cart with a quantity from 3.
After place this order WooCommerce updated the stock from product 4082 with -3.
Ok back to my custom product type. As I said it is possible to connect another product by ID.
For example I connect product 4082 with product ID 10988.
If a customer add product 4082 to the cart and placed the order I want reduce the stock quantity from product ID 10988 and not from 4082.
<?php
add_action('woocommerce_checkout_order_processed', 'stocktest');
function stocktest($order_id){
$order = wc_get_order( $order_id );
$order_item = $order->get_items();
foreach( $order_item as $product ) {
//for the topic I programmed the IDs hardcoded
if($product->ID == '4082'){
wc_update_product_stock( 10998, $product['qty'], 'decrease', '' );
}
}
}
?>
I tried the code above and the stock from ID 10998 is correctly decreased but also the stock from ID 4082 is decreased.
Do I use the wrong hook? And how can I make the function correctly?
Hope somebody can help me with this.
Thanks a lot
Does wc_update_product_stock( 4082, $product['qty'], 'increase', '' ); not work?
That way, the stock should be adjusted for the 4082 product.
Also, there might be a potential issue with your snippet. The $product variable refers to the product object of 4082 product, not the 10998 product.
Maybe try something like this?
<?php
add_action('woocommerce_checkout_order_processed', 'stocktest');
function stocktest($order_id){
$order = wc_get_order( $order_id );
$order_item = $order->get_items();
foreach( $order_item as $product ) {
//for the topic I programmed the IDs hardcoded
if($product->ID == '4082'){
$connected_qty = get_post_meta( 10988, '_stock', true );
wc_update_product_stock( 10998, $connected_qty, 'decrease', '' );
wc_update_product_stock( 4082, $product['qty'], 'increase', '' );
}
}
}
?>
Instead of using the custom field query, you could use the WC_Product class:
$connected_product = wc_get_product(10998);
$connected_qty = $connected_product->get_stock_quantity();
I did some research on your question. WooCommerce called a lot of functions to update the stock af order / payment. If an order is placed the items become an status reduce_stock yes or no.
If is yes go further and update the stock of this item. After this WooCommerce created the e-mails and ordernotes.
All this functions worked together please be careful with changing the code in the file wc-stock-functions.php
If you want change and try something do this for example on a local testserver.
This are some functions in the file
<?php
wc_maybe_reduce_stock_levels();
wc_maybe_increase_stock_levels();
wc_reduce_stock_levels();
wc_trigger_stock_change_notifications();
?>

Get non formatted value from $order->get_formatted_line_subtotal() in WooCommerce

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

Get the product object from sku and update the price in WooCommerce

How can I update a product by product_id in my functions file?
I tried using the below code but to no success:
$_pf = new WC_Product_Factory();
$product_id = wc_get_product_id_by_sku( $sku );
$_product = $_pf->get_product($product_id);
$_product->set_price('225');
Since WooCommerce 3, new WC_Product_Factory() with get_product() method is deprecated and replaced simply by the function wc_get_product().
To update price, you have to update the price and the regular price (or the price and the sale price)...
Also the save() method is necessary to grab the data at the end.
So to get the WC_Product Object from an existing product SKU and to use on it any available method, do the following:
$new_price = '225'; // New price
$_product_id = wc_get_product_id_by_sku( $sku );
if ( $_product_id > 0 ) {
// Get an instance of the WC_Product Object
$_product = wc_get_product( $_product_id );
$_product->set_regular_price($new_price); // Set the regular price
$_product->set_price($new_price); // Set the price
$_product->save(); // Save to database and sync
} else {
// Display an error (invalid Sku
printf('Invalid sku "%s"… Can not update price.', $sku);
}
Tested and works.

How to update WooCommerce order item quantity

I need to update the order item meta in a woocommerce oder on checkout page or while woocommerce creates the order.
I'm using the plugin visual product configurator and it is not passing the right quantity of some items of the order to woocommerce order meta, especially when I use multiple variations on the same product.
Is there a hook for me to use to update the item quantity for a certain order item and how can I use it?
The plugin returns me an array with all the cart information and I can only check if an item of the order appears multiple times - if yes I need to change the quantity of that item to that number in the woocommerce order/database.
I was thinking of adding the following hook to my functions.php
add_action('woocommerce_checkout_create_order', 'change_qty', 1,1);
function change_qty($item_qty){
foreach($item_qty as $qty) {
$qty['product_id'] = $id;
$qty['qty'] = $new_qty
$order->update_meta_data('quantity', $new_qty, $id)
}
}
Whereas $item_qty is be an multi-dimensional array containing the item_ids and adjusted quantities.
Another problem I'm facing is that I dont know when I need to call that function because I get the array from the plugin on the checkout page, but I think WooCommerce has not yet created an order at that moment?
The result should be an adjusted item quantity in the woocommerce order summary in the backend.
To update the order item quantity, you can use WC_Order_Item_Product set_quantity() method.
The correct hook to update order items (line items) is woocommerce_checkout_create_order_line_item action hook, that is triggered during order creation, before data is saved to databased.
add_action('woocommerce_checkout_create_order_line_item', 'change_order_line_item_quantity', 10, 4 );
function change_order_line_item_quantity( $item, $cart_item_key, $cart_item, $order ) {
// Your code goes below
// Get order item quantity
$quantity = $item->get_quantity();
$new_qty = $quantity + 2;
// Update order item quantity
$item->set_quantity( $new_qty );
}
The function arguments (variables) are defined and usable:
$item is the WC_Order_Item_Product Object (not saved yet to database)
$cart_item_key is the related cart item key
$cart_item is the related cart item data
$order is the WC_Order Object (not saved yet to database)
Related:
Get Order items and WC_Order_Item_Product in Woocommerce 3
WC_Order_Item_Product Class and methods API Documentation
WC_Checkout and woocommerce_checkout_create_order_line_item action hook located in the create_order_line_items() method
This can help you (we hook into payment completed notification from the payment provider). If you want to update the _qty just after the order was created, I can change my function. But for now I would update it only when the payment was successful.:
/**
* Update order item qty after payment successful
*/
add_filter( 'woocommerce_payment_complete_order_status', 'update_order_item_qty', 10, 2 );
function update_order_item_qty( $order_status, $order_id ) {
//Get the order and items
$order = new WC_Order( $order_id );
$items = $order->get_items();
//New qty
$new_qty = 0;
foreach ( $items as $item_id => $item_data ) {
update_meta_data( '_qty', $new_qty, $item_id );
}
}
Please try if this is what you'r looking for.

Get the Product ID from email templates in WooCommerce 3

In Woocommerce I am trying to find out how to get the product id for a completed order inside the Customer completed order email template to store it as a PHP variable. This way I will be able to insert it into an external database.
I already tried $product->get_id(); but it does not work.
How can get the Product ID in WooCommerce 3+ from email templates?
You need to loop through Order items… Normally the WC_Order object is defined through the variable $order mostly defined everywhere in email templates.
So the code to get the product ID will be:
// Loop through order items
foreach( $order->get_items() as $item_id => $item ){
// Get the product ID
$product_id = $item->get_product_id();
// Get an instance of the WC_Product object
$product = $item->get_product();
// Get product title (name)
$product_name = $product->get_title(); // or $product->get_name();
// Get product price
$product_price = $product->get_price();
}
See this related thread: Get Order items and WC_Order_Item_Product in Woocommerce 3

Categories