Update address fields relating to order via rest api in WooCommerce - 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.

Related

How to link WooCommerce guest orders to customer account after registration

Our scenario is:
The guest user enters the site and places one or more orders without the need to register. And after a while, he decides to register on the site.
Now how to link guest orders to customer account after registeration?
I use the following code, but this code only works for users who have already registered but did not log in at the time of purchase. Any advice?
//assign user in guest order
add_action( 'woocommerce_new_order', 'action_woocommerce_new_order', 10, 1 );
function action_woocommerce_new_order( $order_id ) {
$order = new WC_Order($order_id);
$user = $order->get_user();
if( !$user ){
//guest order
$userdata = get_user_by( 'email', $order->get_billing_email() );
if(isset( $userdata->ID )){
//registered
update_post_meta($order_id, '_customer_user', $userdata->ID );
}else{
//Guest
}
}
}
You can use the woocommerce_created_customer action hook and the wc_update_new_customer_past_orders() function
So you get:
function action_woocommerce_created_customer( $customer_id, $new_customer_data, $password_generated ) {
// Link past orders to this newly created customer
wc_update_new_customer_past_orders( $customer_id );
}
add_action( 'woocommerce_created_customer', 'action_woocommerce_created_customer', 10, 3 );

WooCommerce tag returning customer

In our store, during checkout for every customer we create account automatically if it doesn’t already exist with same email. Now I’m looking for a way to somehow display label tag or little notice on orders list if it comes from already existing customer (returning) because these orders are handled a little differently.
Does anybody have an idea how to do that?
Thanks in advance
this snippet adds a column to orders list page in woocommerce backend and checks if customer is returning or not
add_filter( 'manage_shop_order_posts_columns', 'shalior_wc_set_custom_edit_post_columns',99,1 );
function shalior_wc_set_custom_edit_post_columns($columns) {
$columns['is-returning'] = __( 'Is returning?', 'your_text_domain' );
return $columns;
}
add_action( 'manage_shop_order_posts_custom_column' , 'shalior_wc_is_returning', 99, 2 );
function shalior_wc_is_returning( $column, $post_id ) {
switch ( $column ) {
case 'is-returning':
$order = new WC_Order( $post_id );
$user_id = $order->get_user_id();
$orders_count = wc_get_customer_order_count( $user_id );
echo $orders_count > 1 ? "Yes" : "NO" ;
break;
}
}

Woocommerce Sending Duplicate Admin Notifications

I implemented this code by #LoicTheAztec to set the default status for all COD orders to pending:
add_action( 'woocommerce_thankyou', 'woocommerce_thankyou_change_order_status', 10, 1 );
function woocommerce_thankyou_change_order_status( $order_id ){
if( ! $order_id ) return;
$order = wc_get_order( $order_id );
if( $order->get_status() == 'processing' ) $order->update_status( 'pending' );
}
After adding the code (as a plugin), Woocommerce started sending duplicate email notifications of new orders. Essentially, one notification is sent when the order is created (rightly so), but the same exact notification is again then sent when the order is marked as completed.
How can I stop this?
You are generating another notification since you are updating the order status after it has already been set by the default functionality in woocommerce. Can you try the below code:
add_filter( 'woocommerce_cod_process_payment_order_status', 'default_cod_payment_order_status', 10 );
function default_cod_payment_order_status( $order_status ) {
return 'pending';
}

Set every new order with woocommerce on hold

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.

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