Set every new order with woocommerce on hold - woocommerce

For a specific woocommerce project, I need to set every new order on hold.
It needs to go through that before processing payment.
Do you guys know a hook to do that? I tried many different things, that didn't work.

add_action( 'woocommerce_thankyou', 'custom_woocommerce_auto_complete_order' );
function custom_woocommerce_auto_complete_order( $order_id ) {
global $woocommerce;
if ( !$order_id )
return;
$order = new WC_Order( $order_id );
$order->update_status( 'on-hold' );
}
This is the standard way of doing it. Not sure if it still works with 2.2, but you didn't specify your WooCommerce version.

Related

Add text in woocommerce_thankyou if order status is complete

I need to add a custom text that is displayed on the thank you page, if the order has been processed and has a completed status.
I am using the following code in the "woocommerce_order_details_before_order_table" hook and it works perfectly, but when I use it in the "woocommerce_thankyou" hook it generates errors.
add_action( 'woocommerce_thankyou', 'complete_custom_text' );
function complete_custom_text( $order ) {
$status = $order->get_status();
if(($status === 'completed')){
echo '<p><b>Text (Custom) 5:</b> Perfect.</p>';
}
}
If someone could help me I would appreciate it.
You are receiving order_id in that hook, use $order = wc_get_order( $order_id ); to get the order before reading the status.

Update address fields relating to order via rest api in WooCommerce

This code change order first and last name after order via rest api. I want to echo the billing first name where it says "asd". olso i need to other meta related to billing information.
add_action( 'woocommerce_payment_complete', 'digger_checkout_save_user_meta');
function digger_checkout_save_user_meta( $order_id ) {
//$order = wc_get_order( $order_id );
// $user_id = $order->get_user_id();
update_post_meta($order_id, '_billing_first_name', 'asd' );
update_post_meta($order_id, '_billing_last_name','bsd' );
}
i try this update_post_meta($order_id, '_billing_first_name', 'billing_first_name' ); but it doesnt work :) thx a lot for your replay.

WooCommerce: updating order

I am trying to run some code everytime after the order is updated using 'woocommerce_update_order' action hook as you can see below:
add_action( 'woocommerce_update_order', 'update_order' );
After some time spent on developing 'update_order' function I realized that it is not called only once as I supposed but it is called multiple times.
So for testing I rewrite 'update order' function as you can see below:
function update_order( $order_id ) {
$order = wc_get_order( $order_id );
$current_count = get_post_meta( $order_id, '_edit_sale_count', true );
$new_count = 1;
if( $current_count ) $new_count = $current_count + 1;
$order->update_meta_data( '_edit_sale_count', $new_count );
$order->save();
}
After updating a single order and checking database, I realized that function 'update_order' is called 1500 times...
So my question is why is this the case? And how I can prevent multiple times execution on single update and force it to run once only?
Thanks

Cannot get Woocommerce order by ID

I have created a function that is supposed to update certain meta fields once an order has been placed with Woocommerce.
Although I can confirm that the function is fired, I cannot seem to be able to load the order by the ID. Here is my code so far:
add_action('woocommerce_new_order', 'wc_update_bib_options');
function wc_update_bib_options($order_id) {
$order = wc_get_order( $order_id );
$items = $order->get_items();
foreach ($items as $item) {
// Some stuff...
}
}
I can confirm that $order_id is passed and is an integer. Also, if I enter an ID of another (previous) order, the code works just fine...
I also tried using
$order = new WC_Order( $order_id );
But I get the same results...
Can anyone help me out?

WooCommerce pass order data into woocommerce_checkout_update_user_meta

I'm trying to add user_meta upon checkout in WooCommerce. Currently I'm using woocommerce_checkout_update_user_meta action to update user meta. I'd like to pass some of the order data itself in as well specifically I'd like to make the order id the value of the meta like so...
function woocommerce_add_my_user_meta( $user_id ) {
global $woocommerce;
update_user_meta( $user_id, 'purchased', ''.$order->ID.'' );
}
add_action('woocommerce_checkout_update_user_meta', 'woocommerce_add_my_user_meta');
This however is not working. It's just adding a blank purchased user meta.
Any help greatly appreciated.
You could use the woocommerce_checkout_update_order_meta hook
// Use hook after checkout
add_action( 'woocommerce_checkout_update_order_meta', 'do-additional-stuff-on-checkout', 10, 2 );
// Things you want to be done when hook is called
function do-additional-stuff-on-checkout( $order_id, $post_values ) {
// get user
$current_user = wp_get_current_user();
// update
update_user_meta( $current_user->ID, 'purchased', $order_id );
}

Resources