WooCommerce pass order data into woocommerce_checkout_update_user_meta - wordpress

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 );
}

Related

Custom field value set to permalink only on new post, But they change it every time it's updated

I want to set the permalink slug using the custom field value for the first save only, but it is not working.
The code below changes the slug not only for the first save, but also for every update.
function custom_slug_auto_setting( $slug, $post_ID, $post_status, $post_type, $post_parent, $original_slug ) {
return $_POST['custom_field_title'];
}
add_filter( 'wp_unique_post_slug', 'custom_slug_auto_setting', 10, 6 );
For the second and subsequent saves, I want to keep the slug set for the first save.
I tried using the filter hook for wp_insert_post to specify post_name only for the first save, but that didn't work well either.
Is there any good solution?
Thank you.
save_post or save_post_{$post->post_type} Fires once a post has been saved. The dynamic portion of the hook name, {$post->post_type}, refers to the post type slug.
We need to discard any updates autosave or revision actions. The WordPress autosave system fire every 60 seconds.
#See https://wordpress.org/support/article/revisions/
The $update parameter is supposed to determined whether this is an existing post being updated. It applies more specifically to a post autosave revision. The $update parameter will always be true when firing through wp_publish_post. But that isn't true for its usage in wp_insert_post (See the following wordpress stackexchange answer and comments for more details...).
#See https://wordpress.stackexchange.com/a/185991/190376
In our case, the wp_publish_post function publish a post by transitioning the post status.
#See https://developer.wordpress.org/reference/functions/wp_publish_post/
By additionally crosschecking the post status we can effectively determine whether it is indeed a non-existing post.
If you are calling a function such as wp_update_post that includes the save_post hook, your hooked function will create an infinite loop. To avoid this, unhook your function before calling the function you need, then re-hook it afterward.
#See https://developer.wordpress.org/reference/hooks/save_post/#avoiding-infinite-loops
save_post will fire on post Update (eg: Autosave) or Publish which is why we're runing into an infinite loop.
#See https://wordpress.stackexchange.com/a/19539/190376
<?php
add_action( 'save_post', 'wpso74121743', 10, 3 ); //can be replaced by save_post_{$post->post_type}
if ( ! function_exists( 'wpso74121743' ) ) {
function wpso74121743( $post_id, $post, $update ) {
if ( ! wp_is_post_autosave( $post_id ) && ! wp_is_post_revision( $post_id ) && ! $update && $post->post_status == 'auto-draft' ) {
$override_post_name = sanitize_title( get_post_custom_values( 'custom_field_title', $post_id ), get_the_title( $post_id ) );
add_action( 'save_post', 'wpso74121743', 10, 3 ); //can be replaced by save_post_{$post->post_type}
wp_update_post( array(
'ID' => $post_id,
'post_name' => $self,
) );
add_action( 'save_post', 'wpso74121743', 10, 3 ); //can be replaced by save_post_{$post->post_type}
};
};
};

Change Verbiage on WooCommerce Subscriptions Customer Facing

I'm looking to change the verbiage for the word "Suspend" to the word "Pause" on the customer-facing side of WooCommerce Subscriptions. I have the filter that pulls up the action buttons but I'm unsure of how to pull the specific action of "Suspend" and change it to the word "Pause".
Here is the filter.
apply_filters( 'wcs_view_subscription_actions', $actions, $subscription );
Any help would be much appreciated. If you need more information I'm happy to provide what I can.
You can use the wcs_view_subscription_actions filter hook. this way. check below code. code will go in your active theme functions.php file.
add_filter( 'wcs_view_subscription_actions', 'change_action_buttons_label', 10, 2 );
function change_action_buttons_label( $actions, $subscription ){
if( isset( $actions['suspend'] ) ){
$actions['suspend']['name'] = __( 'Pause', 'woocommerce-subscriptions' );
}
return $actions;
}

Automatically Update Order Status to Complete When Shipment Tracking Number Input - WooCommerce

I'm trying to figure out the best way to automatically update the order status in WooCommerce when the tracking information is inputted via the official WooCommerce Shipment Tracking plugin. I found the doc from Woo on how to automatically complete orders, but I only want this to execute when the tracking number is added. Any help would be greatly appreciated!
Below is the code from Woo:
/**
* Auto Complete all WooCommerce orders.
*/
add_action( 'woocommerce_thankyou', 'custom_woocommerce_auto_complete_order' );
function custom_woocommerce_auto_complete_order( $order_id ) {
if ( ! $order_id ) {
return;
}
$order = wc_get_order( $order_id );
$order->update_status( 'completed' );
}
And here are the meta references from the plugin:
The Shipping Tracking plugin stores the tracking information in the order meta with the meta key _wc_shipment_tracking_items. It’s an array with the following structure:
tracking_provider — String of predefined provider
custom_tracking_provider — String of custom provider
custom_tracking_link — String of custom tracking URL tracking_number
String of tracking number date_shipped — Timestamp of shipment date
As you said you are using the "WooCommerce Shipment Tracking plugin." but in that plugin, I didn't find any filters or hooks that will help to update status when the tracking number is added. but I found that they use update_post_meta() to update the tracking code so you can use the update_postmeta action hook to update order status.
Try the below code. code will go in your active theme functions.php file.
function update_order_status_when_shipment_tracking_input( $meta_id, $object_id, $meta_key, $meta_value ){
if( $meta_key == '_wc_shipment_tracking_items' ){
error_log('update_order_status_when_shipment_tracking_input');
$order = wc_get_order( $object_id );
if( $order ){
$order->update_status( 'completed' );
}
}
}
add_action( 'update_postmeta', 'update_order_status_when_shipment_tracking_input', 10, 4 );

How to update Order meta data, from a WooCommerce Action

I'm trying to add some custom meta_data to a WooCommerce Order, by running a Order action.
Here is my code:
function custom_add_order_actions( $actions ){
global $theorder;
$actions['my_custom_action'] = 'My custom action';
return $actions;
}
add_action( 'woocommerce_order_actions', 'custom_add_order_actions' );
function custom_add_single_action( $order ){
// Non of these change anything on the order
$order->set_billing_first_name( 'A new test name' );
$order->update_post_meta( 'a_test_field', 'Test field value' );
update_post_meta( $order->get_id(), 'a_test_field', 'Some other value' );
// $order->save(); // I even tried adding this as well, but it doesn't change anything.
}
add_action( 'woocommerce_order_action_my_custom_action', 'custom_add_single_action' );
How do I change the order (or specifically, post_meta fields for an order) from inside an action?
A example
Imagine that I add a post_meta field, with the field name (key): a_test_field.
It's currently an ACF-field, but it's the same for regular WordPress custom fields.
If I change the value of the field and press 'Update', then the value changes:
So far so good. Now the value of the field is 'Foobar'.
What's wierd is that even if I do this:
add_action( 'woocommerce_order_action_my_custom_action', 'custom_add_single_action' );
function custom_add_single_action( $order ){
update_post_meta( $order->get_id(), 'a_test_field', 'A new value' );
die(); // This die is vital, to make the change in the database.
}
Then I can see the value change in the database to 'A new value'.
But if I just do this:
add_action( 'woocommerce_order_action_my_custom_action', 'custom_add_single_action' );
function custom_add_single_action( $order ){
$order->update_post_meta( 'a_test_field', 'A new value' );
// No die(); here...
}
Then the value remains 'Foobar' in the database.
Sorry but the following lightly revisited code works (Selecting the action and click on the button arrow):
add_action( 'woocommerce_order_actions', 'add_custom_order_action' );
function add_custom_order_action( $actions ){
$actions['my_custom_action'] = __('My custom action', 'WooCommerce');
return $actions;
}
add_action( 'woocommerce_order_action_my_custom_action', 'triggered_custom_order_action' );
function triggered_custom_order_action( $order ){
$order->update_meta_data( '_test_1_custom_field', 'AAFFBB9977' );
$order->save();
update_post_meta( $order->get_id(), '_test_2_custom_field', 'Some other value' );
}
Code goes in functions.php file of the active child theme (or active theme). Tested and works.
Note: Order actions are mostly used for some other things than what you are trying to do.
Now when using a meta box with an input field (as you are showing), on submit, you should save that field value using the action hook save_post_shop_order like in those related threads:
Metabox with multi checkbox in WooCommerce admin single orders
Dynamic custom order numbers based on payment method
Metabox with multiple custom fields for WooCommerce admin order pages

wooCommerce call function in checkout page

I have a function in my themes functions.php file which displays some information about the product. On the checkout page below the billing address I want to out put the information there.
Here is my function in the themes functions.php
function wc_checkout_description_so_1( $other_data, $cart_item )
{
$post_data = get_post( $cart_item['product_id'] );
echo '<div>HTML OUTPUT HERE</div>';
}
I have tried to use add_filter to below the billing address but doesnt not work:
add_filter( 'woocommerce_before_checkout_shipping_form', 'wc_checkout_description_so_1', 10, 2 );
All I need is the output below the shipping information and the above I think should work?
Thanks
J
woocommerce_after_checkout_shipping_form might be more appropriate for displaying something after the shipping address. Either way, the only variable that is passed to the woocommerce_after_checkout_shipping_form hook is the $checkout variable. You can var_dump that variable to see what it is available.
add_action( 'woocommerce_after_checkout_shipping_form', 'wc_checkout_description_so_1' );
function wc_checkout_description_so_1( $checkout )
{
var_dump( $checkout );
}

Resources