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
Related
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();
?>
This one for completed initial subscription payments and subscription renewals.
function payment_made($subscription){
// How do I get the Product ID from subscription? (Definitely need this)
}
add_action("woocommerce_subscription_payment_complete", "payment_made");
And this one for when a status is changed, so I can handle manual and system changes either manual overrides or failed/pending/active/whatever status based of payments or switches.
function status_update($subscription, $old_status, $new_status){
// How do I get the Product ID from subscription (Definitely need this)
}
add_action("woocommerce_subscription_status_updated", "status_updated");
To get the product id from the WC_Subscription Object, you will need to loop through order items (as you can have many) using the method get_items() like:
$order_items = $subscription->get_items();
// Loop through order items
foreach ( $order_items as $item_id => $item ) {
// Get the WC_Product_Subscription Object
$product = $item->get_product();
// To get the subscription variable product ID and simple subscription product ID
$product_id = $item->get_product_id();
// To get the variation subscription product ID
$variation_id = $item->get_variation_id();
// Or to get the simple subscription or the variation subscription product ID
$_product_id = $product->get_id();
}
Tested and works.
Related:
How to get the user ID from a WC_Subscription instance Object
How to get Order Details from a WC_Subscription instance Object
I saw this, WooCommerce - Get the product description by product_id
But I was hoping there was a shorter way to get the product by ID and get the description? Any idea?
I'm using WooCommerce 3.0 and above.
In your question, we don't know what do you mean by "GRAB"… It can be how to SET (or how to GET) the product description.
There is multiple ways to SET the product description.
1) From the product ID using Wordpress wp_update_post();
wp_update_post( array('ID' => $product_id, 'post_content' => 'This is the <strong>product description</strong> content.') );
2) From the WC_Product Object using set_description() method
// Get the WC_Product Object instance (optional if needed)
$product = wc_get_product( $product_id );
// Set the description
$product->set_description('This is the <strong>product description</strong> content.');
$product->save(); // Save product data
There is multiple ways to GET the product description (as explained in this thread).
1) From the product ID, using Wordpress get_post() function:
$product_description = get_post( $product_id )->post_content;
2) From the WC_Product Object using get_description() method
// Get the WC_Product Object instance (optional if needed)
$product = wc_get_product( $product_id );
// Get the product description
$description = $product->get_description();
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
I'm customizing my Woocommerce email templates now and right now I only have acess to the order number, I'd like to get the price and quantity for my order but I haven't managed to figure out how.
<?php
// Getting the order object "$order"
$order = wc_get_order( $order_id );
// Getting the items in the order
$order_items = $order->get_items();
// Iterating through each item in the order
foreach ($order_items as $item_id => $item_data) {
// Get the item quantity, something is wrong here..
$item_quantity = $order->get_item_meta($item_id, '_qty', true);
echo $item_quantity;
// Get the price, doesn't work either..
$item_total = $order->get_item_meta($item_id, '_line_total', true)
}
?>
The issue is that I'm unable to get the quantity and price which I can display in my order confirmation email that I'm customizing, I'm currently running woocommerce 3.2.5
As you have the $order object, you can get the product title this way:
<?php
// Loop through order items
foreach($order->get_items() as $items){
$product = $items->get_product(); // The product object
$product_name = $product->get_name(); // The product Name
$quantity = $items->get_quantity(); // The product Quantity
$line_total = $items->get_total(); // The line item total
// Display the product name
echo '<p>'.$product_name.'</p>';
// Display the product quantity
echo '<p>'.$quantity.'</p>';
// Display the product name
echo '<p>'.$line_total.'</p>';
// Get the raw output to check
echo '<pre>'; print_r(echo $items->get_data() ); '</pre>';
}
?>
Remember that an order can have many items (so different product names).
Related trhread: Get Order items and WC_Order_Item_Product in Woocommerce 3