Update order status on form submission - php

I am trying to create a form that automatically updates order status once form has been submitted. The form is located at the order details page, so I would assume that current page ID is equal to orderID. When I try to do a form submission it's simply just stuck and nothing happens. I am asumming it's a problem with getting the orderID and therefore which order to update status on.
I've found the gform_after_submission hook and linked it to the form that is placed on the order details page (form ID 7). I've been trying to use global $wpdb; but not quite sure if thats the right thing to do.
add_action( 'gform_after_submission', 'set_post_content', 10, 2 );
function update_order_submission( $order_id ) {
global $wpdb;
//getting orderID
$order = wc_get_order( $order_id );
//changing order status
$order = array();
$order['ID'] = $order->ID;
$order['post_status'] = 'wc-completed';
//updating order
wp_update_post( $order );
}
I am expecting that once a form has been submitted then the order status of the current order ID (the page the form was submitted from) is going to be updated as orderstatus completed.

Replace your code with follows -
add_action( 'gform_after_submission', 'set_post_content', 10, 2 );
function update_order_submission( $entry, $form ) {
global $post;
$order_id = get_the_ID(); // getting orderID
$order = wc_get_order( $order_id );
if( $order ) {
//changing order status
$order->update_status( 'completed' );
}
}

I managed to solve it by getting the orderID from the URL by using $_GET["id"] to getting the url parameter. The code below in functions.php solves the task.
add_action( 'gform_after_submission_7', 'update_order_submission', 10, 2 );
function update_order_submission( $entry, $form ) {
global $post;
$order_id = $_GET["id"]; // getting orderID
$order = wc_get_order( $order_id );
if( $order ) {
//changing order status
$order->update_status( 'completed' );
}
}

Related

Get the order id on WooCommerce "Order received" Thankyou page

I will customize the thankyou page from my WoocCommerce shop.
For this I added a blanc thankyou.php into the WooCommerce checkout directory.
I tried this code
function get_order($order_id) {
echo $order_id;
}
add_action('woocommerce_thankyou', 'get_order');
But the variable $order_id is empty.
Is there somebody who knows how I get the order id on the thankyou page?
If Url is like www.example.com/checkout/order-received/1234/?key=wc_order_s5ou6md6nTZDds you can use the following to get the order id:
global $wp;
if ( isset($wp->query_vars['order-received']) ) {
$order_id = absint($wp->query_vars['order-received']); // The order ID
$order = wc_get_order( $order_id ); // The WC_Order object
}

setting custom order status in woocommerce

I have followed this tutorial and to add a custom order status "Awaiting Shipment":
My problem is I'm trying to update the status via a php function, but it stays sets on pending payment! So it is executing and changing the correct order but not with this new status.
My code:
$order = new WC_Order($order_id);
$order->update_status('Awaiting shipment', 'order_note');
I can set 'Awaiting Shipment' in the WordPress dashboard ok...
What am I doing wrong?
You need to set it using the slug awaiting-shipment instead, so your code will be:
$order = new WC_Order( $order_id );
$order->update_status('awaiting-shipment', 'order_note');
This time it will work…
Also 'order_note' is optional and should be replaced with a real explicit text as an order note should be.
To finish you also can use $order = wc_get_order( $order_id );
Reference: WC_Order update_status() method
Related thread: WooCommerce: Auto complete paid orders
Try this below
add_action( 'woocommerce_thankyou', 'my_custom_status_update' );
function my_custom_status_update( $order_id ) {
$order = new WC_Order( $order_id );
$order->update_status( 'awaiting-shipment' );
}

Get custom values of variations from order items in WooCommerce

I am trying to get the variation values selected by customer at checkout from an order (by id or otherwise) to map to variables $storage and $tier which are used to build an API url.
I have tried a variety of methods to get the data onto the variables but the API url is failing, which leads me to believe I do not have the indented values on my variables.
My current code is as follows (extract):
add_action( 'woocommerce_order_status_processing', 'my_function' );
function my_function( $order_id ) {
$order = wc_get_order( $order_id );
$items = $order->get_items();
foreach ( $items as $key => $item ) {
$storage = get_post_meta( $key, 'attribute_addon-storage' );
$tier = get_post_meta( $key, 'attribute_subscription-type' );
Does anyone have any idea how I would grab the values of those two variables, addon-storage and subscription-type?
I can see the values I want to get in my database in the woocommerce_order_itemmeta table.
Can I get the values from there?
Edit:
So as per LoicTheAztec's advice, the right way to get the value of the data I see in the woocommerce_order_itemmeta table as $meta_key is:
add_action( 'woocommerce_order_status_processing', 'my_function', 10, 1 );
function my_function( $order_id ) {
$order = wc_get_order( $order_id );
$items = $order->get_items();
foreach ( $items as $key => $item ) {
$storage = wc_get_order_item_meta( $key, 'addon-storage', true );
$tier = wc_get_order_item_meta( $key, 'subscription-type', true );
$anyMetaValue = wc_get_order_item_meta( $key, '$meta_key', true );
// Then I can create the variable I need in the the API url with:
$package = "$tier$storage";
Updated: Your are confusing order post meta data and order item meta data, which are very different things and located in different database tables.
Also your code is incomplete and you will not get anything using get_post_meta() with the $key (which is the item ID)...
The get_post_meta( $order_id, 'meta_key', true ) function will look for Order post meta data (not related to order items) and use Order ID as argument.
The wc_get_order_item_meta( $item_id, 'meta_key', true ) function will look for Order item meta data realated and will use the Item ID as argument (the $key in your code)…
So this function should need to be used in the foreach loop, where you get the order items data, instead.
Then 2 ways:
You should check in your database for the last order ID you have (via phpMyAdmin) in wp_postmeta and wp_woocommerce_order_itemmeta tables, to see where is located the data…
Or you can use the following code (just for testing) that will output the order items raw data where your data is located.
This raw data will be output in the shop, archive and product pages, only visible for logged in admins. You will need to define an order ID in it.
Here is this testing function
add_action( 'woocommerce_before_main_content', 'my_testing_order_function' );
function my_testing_order_function() {
// Define an Order ID
$order_id = 724;
// Only for admin user role
if( ! current_user_can('edit_products')) return;
$order = wc_get_order( $order_id ); // The order object
foreach ( $order->get_items() as $item_id => $item ) {
// Order item meta data Raw output
echo "<pre>ORDER ITEM META DATA - (Item_id $item_id):"; print_r($item->get_data()); echo'</pre>';
echo "<pre>ORDER ITEM META META DATA - (Item_id $item_id):"; print_r($item->get_meta_data()); echo'</pre>';
}
}
Now you have everything needed to locate and get the data. The code below should normally work for you, allowing you to get the data from your variations:
add_action( 'woocommerce_order_status_processing', 'my_function', 10, 1 );
function my_function( $order_id ) {
$order = wc_get_order( $order_id );
foreach ( $order->get_items() as $key => $item ) {
// get the data
$storage = wc_get_order_item_meta( $item_id, 'attribute_addon-storage', true );
$tier = wc_get_order_item_meta( $item_id, 'attribute_subscription-type', true );
}
}

Add custom functionality on WooCommerce complete button

I've a situation to add some data to the database tables on order status completed button.
I can see the url in class-wc-admin-post-types.php
Can someone help me for any hook? Or how the admin-ajax.php works? I have to add status to some of mine custom database tables.
this code will fire a customer's order is set to completed..
add_action( 'woocommerce_order_status_completed', 'custom_task' );
function custom_task( $order_id ) {
// Only continue if have $order_id
if ( ! $order_id ) {
return;
}
// Get order
$order = wc_get_order( $order_id );
// Do your thing
}

Woocommerce custom stock update after successful order

I have a custom database table linked to my Woocommerce tables with some information like SKU and Stock. I want to add custom function to update this table after successful order (when Woocommerce updates product stock). I've tried to do something with this:
add_action( 'woocommerce_reduce_order_stock', 'wpet_testnote' );
function wpet_testnote() {
// Lets grab the order
$order = wc_get_order( $order_id );
$order->add_order_note( 'Stock Updated.' );
I've tried to testing my action with basic order note adding, but it's not working. Any ideas?
you forgot to submit the $order_id. this one works on my end:
add_action( 'woocommerce_reduce_order_stock', 'wpet_testnote' );
function wpet_testnote( $order_id ) {
$order = wc_get_order( $order_id );
$order->add_order_note( 'Stock Updated.' );
}

Categories