How to update Order meta data, from a WooCommerce Action - woocommerce

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

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

Advanced Custom Fields repeater delete_post_meta hooks passing the wrong meta

I'm trying to run some additional functions when updating and deleting ACF custom post meta, ACF version 4.3.8. The ACF field type is a repeater with several rows. When I delete one of these rows, I'm getting the wrong $meta_key passed to my hook:
<?php
class My_Consultant_Save_Post {
function __construct() {
add_action( 'delete_post_meta', array ( $this, 'delete_consultant_meta_connections'), 10, 4 );
}
public function delete_consultant_meta_connections( $meta_id, $post_id, $meta_key, $_meta_value ) {
echo 'post_id';
var_dump($post_id);
echo 'meta_key';
var_dump($meta_key);
echo 'current value of meta, before deleting';
$current = get_post_meta($post_id, $meta_key);
var_dump($current);
die;
}
}
$my_consultant_update = new My_Consultant_Save_Post();
see delete_postmeta function, and the action delete_post_meta
actually it appears to be passing the $meta_key of the last row entry I have in the field group's repeater field, and definitely not one that I deleted from the front-end post editor's acf remove row button.
Any ideas why the hook is not returning the meta_key of the actual meta I tried to delete?

Woocommerce remove admin order item item meta

Im adding a custom item meta to every item with woocommerce_add_order_item_meta action.
I dont need to show this custom meta in the Order Detail, because it's an arry stringy that im using to print a pdf.
How can i remove this meta custom item? Is there some action to do it?
Thanks
I understand its a bit old question but I am answering for some other users who will have same issue in future.
If you want your order item meta to not display in admin order details page than you should append underscore (_) at the start of your meta name.
Example:
_custom_order_meta
The underscore trick no longer works. In Woo 3.x there is a hidden meta array:
add_filter('woocommerce_hidden_order_itemmeta',
array($this, 'hidden_order_itemmeta'), 50);
function hidden_order_itemmeta($args) {
$args[] = 'my_hidden_meta';
return $args;
}
It sounds like you need to keep it in order to print the PDF. If you override the order-details.php template you can possibly change:
$item_meta = new WC_Order_Item_Meta( $item['item_meta'], $_product );
to
$array = $item['item_meta'];
if( isset( $array['your_pdf_array_key'] ) ){ unset( $array['your_pdf_array_key'] ); }
$item_meta = new WC_Order_Item_Meta( $array, $_product );
EDIT
The wc_add_order_item_meta() function has 4 parameters as seen in the code:
function wc_add_order_item_meta( $item_id, $meta_key, $meta_value, $unique = false ) {
return add_metadata( 'order_item', $item_id, $meta_key, $meta_value, $unique );
}
If you choose a $meta_key with a preceding underscore, the meta will be automatically hidden from view on the checkout/order-received page, the My Order's list of the My account area, as well as in the admin's order overview page.
Therefore, I would suggest making your woocommerce_add_order_item_meta callback function look something like the following:
add_action( 'woocommerce_add_order_item_meta', '25979024_add_order_item_meta', 10, 3 );
function 25979024_add_order_item_meta( $order_item_id, $cart_item, $cart_item_key ) {
wc_add_order_item_meta( $order_item_id, '_pdf_something', 'hide this stuff' );
}

Display custom meta box to the admin sidebar not working

I created a custom meta box to display some content inside the post display in the admin area and want it to show in the sidebar instead of below the wysiwyg editor. I added "side" to the context but nothing happens! I've been playing with this for many hours and haven't had any luck.
This is my code:
function add_custom_meta_box() {
add_meta_box (
'custom_meta_box',
'Custom Meta Box Title',
'show_custom_meta_box',
'post',
'side',
'high'
);
}
add_action('add_meta_boxes', 'add_custom_meta_box');
function show_custom_meta_box() {
// here i have all the code
}
Adapted from this Q&A, the following will force the custom meta box into the second position in the side column.
Check the comments and note the caveat for admin_init.
It only works if the user hasn't rearranged the positions himself. When registering a new user, the position is set for him, as the hooks admin_init and user_register are attached to the same callback function.
// This fires at **every** page load, a better hook must be found
add_action( 'admin_init', 'set_user_metaboxes_so_14183498' );
// This fires when a new user is created
add_action( 'user_register', 'set_user_metaboxes_so_14183498' );
function set_user_metaboxes_so_14183498( $user_id = null )
{
// This is the metakey we will need to update
$meta_key = 'meta-box-order_post';
// So this can be used without hooking into user_register
if( !$user_id )
$user_id = get_current_user_id();
// Set the default order if it has not been set yet by the user.
// These are WP handles, PLUS our custom meta box handle
if ( ! get_user_meta( $user_id, $meta_key, true ) )
{
$meta_value = array(
'side' => 'submitdiv,custom_meta_box,formatdiv,postimagediv',
'normal' => 'postcustom,commentsdiv,slugdiv,revisionsdiv',
'advanced' => '',
);
update_user_meta( $user_id, $meta_key, $meta_value );
}
}

Automatically insert WordPress custom field

I want to add a new custom field with a meta key and meta value to my posts.
Currently the only way it is added to a post is if I go into the post and click Update.
I have lots of posts and essentially want this custom field to be added to all posts automatically with an assigned meta value.
This meta value is different for each post.
I found this helpful: http://www.catswhocode.com/blog/wordpress-how-to-insert-data-programmatically
function add_custom_field_automatically($post_ID) {
global $wpdb;
if(!wp_is_post_revision($post_ID)) {
add_post_meta($post_ID, 'field-name', 'custom value', true);
}
}
add_action('publish_page', 'add_custom_field_automatically');
add_action('publish_post', 'add_custom_field_automatically');
This will add a 'custom value' to the 'field-name' in the $post_ID.
I think the right method is using the 'save_post'hook,such as:
function cwp_add_custom_post_meta($post_id, $post){
global $wpdb;
$post_cat_id=get_the_terms( $post_id, 'category' );
$post_cat_id=cwp_object_to_array($post_cat_id);
$post_cat_id=$post_cat_id['0'] ["term_id"];
$display_voting = get_tax_meta($post_cat_id,'cwp_display_voting');
if(!wp_is_post_revision($post_id))
update_post_meta($post_id,'display_voting', $display_voting);
}
add_action( 'save_post', 'cwp_add_custom_post_meta', 10, 2 );

Resources