Woocommerce custom stock update after successful order - php

I have a custom database table linked to my Woocommerce tables with some information like SKU and Stock. I want to add custom function to update this table after successful order (when Woocommerce updates product stock). I've tried to do something with this:
add_action( 'woocommerce_reduce_order_stock', 'wpet_testnote' );
function wpet_testnote() {
// Lets grab the order
$order = wc_get_order( $order_id );
$order->add_order_note( 'Stock Updated.' );
I've tried to testing my action with basic order note adding, but it's not working. Any ideas?

you forgot to submit the $order_id. this one works on my end:
add_action( 'woocommerce_reduce_order_stock', 'wpet_testnote' );
function wpet_testnote( $order_id ) {
$order = wc_get_order( $order_id );
$order->add_order_note( 'Stock Updated.' );
}

Related

Custom post type data and order item data in WooCommerce

I am using a plugin, WooCommerce Custom Post Type Manager, along with WooCommerce that enables the use of custom post types as products. This is working pretty well for the most part, but I want to be able to control inventory and I am able to see the problem. In the database under the order_item_meta table, the product_id = 0. Because the product_id is empty, there is no way to update the stock on a completed purchase.
I know that WooCommerce made some changes to where it searched if a post_type was a 'product' and if not, certain things failed. I am wondering if there is a filter hook or another way to add the product id with a custom function at checkout?
This is the function I am trying to create to control a custom type of inventory, we are selling event "seats". And no, even the regular "stock" does not work, probably because of the same reason.
function update_course_seats( $order_id ){
$order = wc_get_order( $order_id );
$items = $order->get_items();
foreach ( $items as $item ) {
$product_id = $item->get_product_id(); // this returns 0
if($product_id != 0) {
wc_update_order_item_meta( $item, '_seats', 10 ); // example number
}
}
//add_action( 'woocommerce_payment_complete', 'update_course_seats');
First you should use woocommerce_add_cart_item_data action hook, to store your Custom Post Type (CTP) Id on add to cart… But for that you will need to display, on your CTP pages, inside the add to cart form, a hidden field with the CTP Id like:
<input type="hidden" name="ctpost_id" value="<?php echo get_the_id(); ?>">
Now you can add this hooked function, that will add as custom cart item data your CTP Id:
add_filter( 'woocommerce_add_cart_item_data', 'add_custom_cart_item_data', 10, 3 );
function add_custom_cart_item_data( $cart_item_data, $product_id, $variation_id ){
if( isset( $_POST['ctpost_id'] ) ) {
$cart_item_data['ctpost_id'] = wc_clean( $_POST['ctpost_id'] );
}
return $cart_item_data;
}
The Second hook to be used is woocommerce_checkout_create_order_line_item action hook, to add custom order item meta data (or to make changes to order item data). This hook is triggered before payment gateways process during order creation.
You can use WC_data add_meta_data() method to save your CTP Id as a custom CTP ID like:
add_action( 'woocommerce_checkout_create_order_line_item', 'save_cpt_id_to_order_item_data', 10, 4 );
function save_cpt_id_to_order_item_data( $item, $cart_item_key, $cart_item, $order ){
if( isset($cart_item['ctpost_id']) && $cart_item['ctpost_id'] > 0 ) {
// Add the custom CTP post ID
$item->add_meta_data('_ctpost_id', $cart_item['ctpost_id'] );
}
// And here for example you add seats as custom cart item data
if( isset($cart_item['quantity']) ) {
$item->add_meta_data( 'Seats', $cart_item['quantity'], true );
}
}
When the order will get paid, you will be able to update everything required, as you will have your CTP post ID as custom order item data.
Then finally, you can use woocommerce_payment_complete as follow:
add_action( 'woocommerce_payment_complete', 'action_payment_complete_callback', 10, 1 );
function action_payment_complete_callback( $order_id ){
$order = wc_get_order();
// Loop through order items
foreach ( $order->get_items() as $item_id => $item ) {
$ctp_id = $item->get_meta('_ctpost_id'); // Get the CTP post ID
// Your code goes here
}
}
Or woocommerce_order_status_changed hooks like:
add_action( 'woocommerce_order_status_changed', 'action_order_status_changed_callback', 10, 4 );
function action_order_status_changed_callback( $order_id, $status_from, $status_to, $order ){
if( in_array( $status_to, ['processing','completed'] ) ) {
// Loop through order items
foreach ( $order->get_items() as $item_id => $item ) {
$ctp_id = $item->get_meta('_ctpost_id'); // Get the CTP post ID
// Your code goes here
}
}
}
Related: Get Order items and WC_Order_Item_Product in Woocommerce 3.

setting custom order status in woocommerce

I have followed this tutorial and to add a custom order status "Awaiting Shipment":
My problem is I'm trying to update the status via a php function, but it stays sets on pending payment! So it is executing and changing the correct order but not with this new status.
My code:
$order = new WC_Order($order_id);
$order->update_status('Awaiting shipment', 'order_note');
I can set 'Awaiting Shipment' in the WordPress dashboard ok...
What am I doing wrong?
You need to set it using the slug awaiting-shipment instead, so your code will be:
$order = new WC_Order( $order_id );
$order->update_status('awaiting-shipment', 'order_note');
This time it will work…
Also 'order_note' is optional and should be replaced with a real explicit text as an order note should be.
To finish you also can use $order = wc_get_order( $order_id );
Reference: WC_Order update_status() method
Related thread: WooCommerce: Auto complete paid orders
Try this below
add_action( 'woocommerce_thankyou', 'my_custom_status_update' );
function my_custom_status_update( $order_id ) {
$order = new WC_Order( $order_id );
$order->update_status( 'awaiting-shipment' );
}

Change orders status made with cheque payment method to "processing" status

I need to make WooCommerce push payments made by check into the "processing" status rather than the "on hold" status. I tried the snippet below however it doesn't seem to have an effect.
Here is my code:
add_filter( 'woocommerce_payment_complete_order_status', 'sf_wc_autocomplete_paid_orders' );
function sf_wc_autocomplete_paid_orders( $order_status, $order_id ) {
$order = wc_get_order( $order_id );
if ($order->status == 'on-hold') {
return 'processing';
}
return $order_status;
}
How can I achieve this?
Thanks.
Here is the function you are looking at hooked in woocommerce_thankyou hook:
add_action( 'woocommerce_thankyou', 'cheque_payment_method_order_status_to_processing', 10, 1 );
function cheque_payment_method_order_status_to_processing( $order_id ) {
if ( ! $order_id )
return;
$order = wc_get_order( $order_id );
// Updating order status to processing for orders delivered with Cheque payment methods.
if ( get_post_meta($order->id, '_payment_method', true) == 'cheque' )
$order->update_status( 'processing' );
}
This code goes in function.php file of your active child theme (or theme) or also in any plugin file.
This is tested and works.
Related thread: WooCommerce: Auto complete paid Orders (depending on Payment methods)
I didn't want to use the Thank You filter in case the order still got set to On Hold at a previous step, before changing it to my desired status in the filter (a custom status in my case, or Processing in yours). So I used the filter in the Cheque gateway:
add_filter( 'woocommerce_cheque_process_payment_order_status', 'myplugin_change_order_to_agent_processing', 10, 1 );
function myplugin_change_order_to_agent_processing($status){
return 'agent-processing';
}
I hope this helps someone else out there to know that there is another option.
Previous answer from LoicTheAztec is outdated and gives error about accessing object fields directly on the order object.
Correct code should be
add_action( 'woocommerce_thankyou', 'cheque_payment_method_order_status_to_processing', 10, 1 );
function cheque_payment_method_order_status_to_processing( $order_id ) {
if ( ! $order_id )
return;
$order = wc_get_order( $order_id );
// Updating order status to processing for orders delivered with Cheque payment methods.
if ( get_post_meta($order->get_id(), '_payment_method', true) == 'cheque' )
$order->update_status( 'processing' );
}

Add custom functionality on WooCommerce complete button

I've a situation to add some data to the database tables on order status completed button.
I can see the url in class-wc-admin-post-types.php
Can someone help me for any hook? Or how the admin-ajax.php works? I have to add status to some of mine custom database tables.
this code will fire a customer's order is set to completed..
add_action( 'woocommerce_order_status_completed', 'custom_task' );
function custom_task( $order_id ) {
// Only continue if have $order_id
if ( ! $order_id ) {
return;
}
// Get order
$order = wc_get_order( $order_id );
// Do your thing
}

Woocommerce - Call custom function after payment is completed

I am using Woocommerce for some project and i need to send the order id to some remote site when the payment is made. I am not finding the accurate hook to do this. Can anyone help me to find what's the correct hook to perform certain action after order is completed.
Here is what i have tried
add_action( 'woocommerce_thankyou', 'woo_remote_order' );
function woo_remote_order( $order_id ) {
// Lets grab the order
$order = new WC_Order( $order_id );
//Some action to make sure its working.
wp_mail( 'sagarseth9#example.com',' Woocommmerce Order ID is '.$order_id , 'Woocommerce order' );
}
Not sure which is the proper hook to perform this action. I am using paypal payment gateway for payment and orders successfully passes through.
looks like you need add accepted_args on last parameters,
Try this :
add_action( 'woocommerce_thankyou', 'your_func', 10, 1 );
function your_func($order_id) {
$order = new WC_Order( $order_id );
/* Do Something with order ID */
}
Maybe try one of the following.
woocommerce_checkout_order_processed
woocommerce_new_order
add_action( 'woocommerce_subscription_payment_complete', 'YourFunction', 1, 2);
function YourFunction ($order_id)
{
$order = new WC_Order( $order_id );
wp_mail( 'sagarseth9#example.com',' Woocommmerce Order ID is '.$order_id , 'Woocommerce order' );
}
The add_action call must be placed at the very beginning of your plugin, if using wordpress, or if a theme, in functions.php.

Categories