Auto complete order woocommerce [duplicate] - wordpress

This question already has answers here:
WooCommerce Order Status (Autocomplete orders)
(1 answer)
WooCommerce: Auto complete paid orders
(5 answers)
Closed 11 months ago.
I have a WordPress site that sells courses, and I want to make the woocommerce change the order status for the processing to complete.
I tried this
https://woocommerce.com/document/automatically-complete-orders/
But when the payment failed, it became complete
I want to make it complete only for the paid courses.
Thank you

The reference you picked is ok
https://woocommerce.com/document/automatically-complete-orders/
Just needed a small adjustment:
/**
* Auto Complete Processing WooCommerce orders on Thankyou Page
*/
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 );
if ( $order->has_status('processing') ) {
$order->update_status( 'completed' );
}
}
What we are doing with this adjustment is checking if the order has status processing before updating it to completed. This will avoid failed orders to be turned into completed.
Hope it works

Related

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

Why does the order get Processing status (when paying for product using a coupon)?

Please help figure it out. Some strange behavior in the payment process. If the buyer makes a purchase without using the coupon code, if the payment is successful, it is immediately transferred to the status: completed(it's important). But as soon as the buyer makes a purchase using the coupon. Even with successful payment, the order is transferred to the status: Processing.
I tried to use this code:
add_action( 'woocommerce_thankyou', 'custom_woocommerce_auto_complete_paid_order', 20 );
function custom_woocommerce_auto_complete_paid_order( $order_id ) {
if ( ! $order_id )
return;
$order = new WC_Order( $order_id );
if ( $order->has_status( 'processing' ) ) {
$order->update_status( 'completed' );
}else {
return;
}
}
But it doesn't work. Anyway, when paying for an item using a coupon, it first gets into the status Processing. Please, help me figure it out.

How to show user meta field in the Woocommerce Order admin page? [duplicate]

This question already has answers here:
How to get WooCommerce order details
(6 answers)
Get the customer ID from an order ID in WooCommerce
(2 answers)
How to save and display user meta from Woocommerce order admin
(1 answer)
Closed 2 years ago.
How to show user meta field of the user who placed the order in the Woocommerce Order admin page?
add_action( 'woocommerce_admin_order_data_after_billing_address', 'my_custom_checkout_field_display_admin_order_meta', 10, 1 );
function my_custom_checkout_field_display_admin_order_meta($order){
echo '<strong>Store number</strong>' . get_user_meta( $user_id, 'wpcf-store-number', true );
}
This is what I have so far. The user meta field is wpcf-store-number.
You need the user ID to retrieve user meta data, which is not something the action you are using provides you with. It only passes you the order object. So you will first have to check if the order was placed by a registered user and get the user from the order object. Then you can take the user ID and check for the existence of certain user meta and output it:
add_action( 'woocommerce_admin_order_data_after_billing_address', 'user_meta_after_admin_order_billing_address', 10, 1 );
function user_meta_after_admin_order_billing_address( $order ) {
if ( $user = $order->get_user() ) {
if ( $store_number = get_user_meta( $user->ID, 'wpcf-store-number', true ) ) {
printf( '<p><strong>Store number:</strong> %s</p>', $store_number );
}
}
}

Get Coupon code applied to the product in woocommerce [duplicate]

This question already has answers here:
WooCommerce Access orders that had discounts
(1 answer)
Get coupon data from WooCommerce orders
(2 answers)
Closed 3 years ago.
I am trying to retrieve the shop coupon code applied to a particular product. I used the below code but it did not work.
$WC_Cart = new WC_Cart();
$var = $WC_Cart->get_applied_coupons();
Can anyone please help me to get the solution. Thanks in advance!
I think this will solve your problem. I tested the code and it worked as:
echo order no
echo used coupons for this order no
run step 1 and step 2 for all orders
You may need to modify it.
//function to get orders - completed, pending and processing
function lets_get_all_orders()
{
$customer_orders = wc_get_orders( array(
'limit' => -1,
'status' => array('completed','pending','processing')
) );
return $customer_orders;
}
//function to get all used coupons in the orders
function lets_get_all_used()
{
$orders = lets_get_all_orders();
//traverse all users and echo coupon codes
foreach($orders as $order)
{
$order_discount = $order->discount_total;
$order_used_coupons = $order->get_used_coupons();
$order_id = $order->ID;
//check if any coupon is used in this order
if($order_discount>0)
{
echo "Order No: $order_id <br> Used Coupons:";
//display coupon code(s)
foreach($order_used_coupons as $order_used_coupon)
{
echo " - $order_used_coupon";
echo "<br>";
}
}
}
}
Please inform me if you need any help. Have a good day.

Auto-complete Paid Orders only for specific product IDs on Woocommerce

Is there any way I can auto complete orders only for specific product IDs on Woocommerce?
I used the code on this thread to auto complete orders.
I also read this thread but it excludes product ids from auto complete. And I am not able to make it work the other way around.
Since I have 20+ products in my shop and I want to use auto complete on only 2 of them, it would be great if I can specify the order ids which I want to auto complete.
Here is a way to autocomplete paid orders for specific product IDS:
add_action( 'woocommerce_payment_complete_order_status', 'wc_auto_complete_paid_order', 10, 3 );
function wc_auto_complete_paid_order( $status, $order_id, $order ) {
// Below the targeted product Ids
$targeted_ids = array(37, 53);
// Loop through order line items
foreach( $order->get_items() as $item ) {
if ( in_array( $item->get_product_id(), $targeted_ids ) || in_array( $item->get_variation_id(), $targeted_ids ) ) {
return 'completed';
}
}
return $status;
}
Code goes in functions.php file of the active child theme (or active theme).
WooCommerce: Auto complete paid orders
Exclude specific products on auto-complete orders process in Woocommerce

Resources