Trigger action when order is placed (woocommerce) - wordpress

I want to update product price as shown on this example, and even when this works fine for me I need this to happen when any new order gets completed.
How can I trigger this to every order made? Is there any kind of event in Woocommerce for a new completed order, or similar?
Thank you.

Solved. This can be done with the woocommerce_thankyou action, as seen here:
add_action( 'woocommerce_thankyou', 'my_callback');
function my_callback( $order_get_id ) {
$order = wc_get_order( $order_get_id ); // Do whatever you need with the order ID
}
}

Related

WooCommerce Upsells Same Order as Admin

really hoping someone can help with this as I thought it would be far more simple!
Long story short, I have created a script that populates the upsells of WooCommerce products, all works great using the API and they are there. They show on the product page as expected but in a completely different order to how they were inputted in the admin area and I cannot seem to find a way for the order to follow admin?
function filter_woocommerce_upsells_orderby( $orderby ) {
return 'menu_order';
};
add_filter( 'woocommerce_upsells_orderby', 'filter_woocommerce_upsells_orderby', 10, 1 );
Above is the hook I have found but from the options I have found such as menu order / id / price etc, there is not simply an overide option to ignore the order and just take them as they are in admin!?
Please help!
I also encountered this problem. And i have an idea.
I checked all wordpress parameters about Order & Orderby.Link is https://developer.wordpress.org/reference/classes/wp_query/#order-orderby-parameters.
And i use the paramteters name "none",It can make your orderby not based on any sorting rules.In other words, it is sorted according to upsells. here is the code.
// ORDER BY
add_filter( 'woocommerce_upsells_orderby', 'filter_woocommerce_upsells_orderby', 10, 1 );
function filter_woocommerce_upsells_orderby( $orderby ){
return "none";
};
// ORDER
add_filter( 'woocommerce_upsells_order', 'filter_woocommerce_upsells_order', 10, 1 );
function filter_woocommerce_upsells_order(){
return 'asc'; // Default is 'desc';
};
But it is still chaotic, when adding any product to upsells, it is still random. Therefore I also used a plug-in "WooCommerce Drop/Drag For Upsells Cross-Sells", which allows you to drag your products in upsell at will.
If you have any question you can ask me.
Thanks.

Woocommerce Custom Order Action and Order Update WebHook problem

i'm trying to solve this issue for weeks, have tried everything.
I have a Custom Order Action in my order admin panel like this:
function manage_actions( $actions ) {
$actions['wc_my_custom_action'] = 'My Custom Action';
return $actions;
}
add_action( 'woocommerce_order_actions', 'manage_actions');
function custom_action_function( $order ) {
update_post_meta( $order->id, 'Custom_Meta', 'Value' );
}
add_action( 'woocommerce_order_action_my_custom_action', 'custom_action_function' );
This works fine, if i open an order and then click on the 'My Custom Action' in the actions menu the 'Custom_Meta' key value pair is added to the order.
Now i have an Order Update WebHook (using API v3) set up and unfortunately it triggers before the 'custom_action_function' function runs so i always get the payload of the order without the new meta tag. I have tried everything i can to add the meta data prior to the webhook being fired, does anyone have an idea on how to accomplish this? Thanks

WooCommerce, WP 4.9.7 update product attribute after order

I have a product what have attribute limited-edition-counter. When someone will buy this product I need to increment this attribute.
I tried to use add_action( 'woocommerce_order_status_completed', _my_function, 10, 1);
but it isn't called after order. It's only one trigger what I found to call after each order.
I use own plugin to extends WP.
Any hints how can I solve this problem?
Thanks in advance.
you can use woocommerce_thankyou hook so it will call everytime when someone order from the store.
add_action( 'woocommerce_thankyou', 'your_function' );
function your_function()
{
//access `limited-edition-counter` attribute here and increment it here
}

Hide and show Complete action shortcut in woocommerce

I searched a lot but didn't find any solution that work for me.
I am developing a plugin for wordpress that will work only with woocommerce. So i want that when my order status is in pending state i want to hide complete action shortcut on orders listing page.
In the above picture i want to hide middle button when my order is in pending state. I want to do it in plugin.
Any suggestion.
Thanks in advance.
Yes this can be done using 'woocommerce_admin_order_actions' filter and adding a function to a filter.
Code to be added in you plugin:
add_filter('woocommerce_admin_order_actions','wdm_verify_product_limitation',5,2);
function wdm_verify_product_limitation( $actions, $the_order ){
if ( $the_order->has_status( array( 'pending','on-hold') ) ) {
unset($actions['complete']);
}
return $actions;
}
Here i am checking if the order is in 'pending' or 'on-hold' state then don't show the Complete button for that order.(in your case remove 'on-hold' from the code )
Have tested the same. Do let me know if this works for you

how can I detect that option value has changed in wordpress?

I am working on a Wordpress plugin and I want to fire a different action based on each option value change. so how can I do that ?
Example:
$options = get_option('ACP_settings'); if $options['acp-select'] has changed from previous value than I want to fire different action based on selection
Please note that $option['acp-select'] is retrieved from select/option html form and has following values for selection:'book','air','SW','HW' and etc....
I hope I have posted my question clearly.
thanks for help
You can use wordpress hooks in the update_option() function.
In your case you can use updated_option or update_option_{$option} hooks.
The code for ACP_settings option would look like:
add_action( 'update_option_ACP_settings', function($old_value, $value, $option){
if( $old_value !== $value ){
// Your option was updated
// Run your code here
}
}, 10, 3 );

Resources