Auto change order status from pending to completed in Woocommerce - wordpress

I am trying to change paid order status pending to completed.
CODE:
function wc_autocomplete_paid_orders( $order_status, $order_id ) {
$order = wc_get_order( $order_id );
if ( $order_status == 'pending' ) {
return 'completed';
}
return $order_status;
} ```
THank you for the help.

You should not do this programatically as this is done from the payment providers that your WooCommerce is configured to use whet a successful order payment has been issued.
If in case you use a payment provider in sandbox mode or test mode that does not do this and you need to test the completed order status, you can do change this manually from the WordPress admin panel:
WordPress Admin / WooCommerce / Orders - edit a PENDING oder and change it manually to COMPLETE.

Related

Is there a reason the "Emails" tab in WooCommerce Settings breaks after the use of custom code?

I have used the following custom code.
add_filter( 'woocommerce_email_recipient_new_order', 'custom_wc_email_recipient_new_order', 10, 2 );
function custom_wc_email_recipient_new_order( $recipient, $order ) {
// Get the user ID of the order's creator
$user_id = $order->get_user_id();
// Get the user's role
$user = get_userdata( $user_id );
$user_role = $user->roles[0];
// Only send the email to the admin email if the customer has the specified user role
if ( $user_role == 'role1' ) {
return $recipient .= ', admin#website.com';
}
// Return the original recipient for all other user roles
return $recipient;
}
The code works perfectly fined and does what it is required to do, however, once the code has been used once (so once an order has been placed) if I try to access the "Emails" tab in "WooCommerce > Settings", I get a fatal error on the website and all I see is the below image.
Is there a reason for this, and if so, a way I can fix it?
EDIT - Added part of the error log:
[20-Dec-2022 17:12:00 UTC] WordPress database error Unknown column 'wp_postmeta.post_id' in 'where clause' for query SELECT meta_id FROM wp_8hkq051x71_postmeta,
(SELECT DISTINCT post_id FROM wp_8hkq051x71_postmeta
WHERE (meta_key = '_billing_country' OR meta_key='_shipping_country') AND meta_value='UA')
AS states_in_country
WHERE (meta_key='_billing_state' OR meta_key='_shipping_state')
AND meta_value='CV'
AND wp_postmeta.post_id = states_in_country.post_id
LIMIT 100 made by do_action_ref_array('action_scheduler_run_queue'), WP_Hook->do_action, WP_Hook->apply_filters, ActionScheduler_QueueRunner->run, ActionScheduler_QueueRunner->do_batch, ActionScheduler_Abstract_QueueRunner->process_action, ActionScheduler_Action->execute, do_action_ref_array('woocommerce_run_update_callback'), WP_Hook->do_action, WP_Hook->apply_filters, WC_Install::run_update_callback, wc_update_721_adjust_ukraine_states, Automattic\WooCommerce\Database\Migrations\MigrationHelper::migrate_country_states, Automattic\WooCommerce\Database\Migrations\MigrationHelper::migrate_country_states_for_orders
I have many lines like the one above, the only part that seems to change on each line is the:
AND meta_value='CV'
The CV changes to CH, CK, KS, etc.
Second Edit - Code Fix
The initial problem with the code was a WooCommerce bug. With that bug solved, I still couldn't modify email recipients in the WooCommerce Emails tab in the Settings. To be able to modify recipients there you need to use the following modified code.
add_filter( 'woocommerce_email_recipient_new_order', 'custom_wc_email_recipient_new_order', 10, 2 );
function custom_wc_email_recipient_new_order( $recipient, $order ) {
if ( $order ) {
// Get the user ID of the order's creator
$user_id = $order->get_user_id();
// Get the user's role
$user = get_userdata( $user_id );
$user_role = $user->roles[0];
// Only send the email to the admin email if the customer has the specified user role
if ( $user_role == 'role1' ) {
return $recipient .= ', admin#website.com';
}
// Return the original recipient for all other user roles
return $recipient;
}
return $recipient;
}
Thanks for posting the error log, now it all makes sense. It's a database error, it says wp_postmeta.post_id is missing. This happens because you are using a custom database prefix wp_8hkq051x71 and some code that generated this SQL wasn't using $wpdb->postmeta or $wpdb->prefix. Instead, there was a hardcoded wp_postmeta value and that table really doesn't exists in your database.
But how that happened?
WooCommerce team made some changes to country states, in this case Ukrainian states and they made a bug inside the update script.
They already fixed it 19 hours ago: https://github.com/woocommerce/woocommerce/commit/6a1a7d7e15f488064f872020d42b7a58a2980c38
So just update WooCommerce to the latest version and the bug will disappear.
Also, I would highly recommend you to use only stable releases of WooCommerce instead of latest dev versions.
Current stable version is 7.1.1 and current dev version with included fix for this issue is 7.2.1 (https://github.com/woocommerce/woocommerce/releases/tag/7.2.1)
The problem is not related to your custom_wc_email_recipient_new_order at all. It's just a coincidence that you noticed this bug after you added your change.

How to trigger custom order status woocommerce email only for specific conditions

Hi there I have added a custom order status called "payment instructions" and I have also added a custom email template for this status.
Now I want that this email template should be only be triggered when two conditions are true.
Cond-1: If the order status is "on-hold".
Cond-2: If the payment method chosen by the customer is " Credit Card".
I have added the following code in my functions.php but it doesn't seem to work.
// Trigger "Payment Instructions" notification for Credit card orders
add_action('woocommerce_order_status_payment-instructions', 'email_payment_instructions_notification_for_credit_card', 2, 20 );
function email_payment_instructions_notification_for_credit_card( $order_id, $order ) {
if( $order->get_payment_method_title() == 'CREDIT CARD (15% ADDITIONAL FEE)' && $order->get_status == 'on-hold' )
WC()->mailer()->get_emails()['WC_Email_Customer_order_status_email_payment_instructions']->trigger( $order_id );
}
How can I make it work? or Should I use any other code for this?

Set Woocommerce new orders to "On hold" and send "on hold" email [duplicate]

This question already has answers here:
Change COD default order status to "On Hold" instead of "Processing" in Woocommerce
(3 answers)
Change default WooCommerce order status to processing for cheque and bacs payments
(2 answers)
Closed last year.
I would like to ask.
Is there any way I can automatically make every new Woocommerce order’s status on hold?
Paypal and Visa card payments is getting automatically “processing” status but I don’t want that. I would like all my new orders getting order status “On Hold” and getting on hold email
I have found the following code which I add it on function child theme and is working fine but there is a small problem.
The customer still getting proccessing status email instead of getting “On hold” email.
/**
* Auto On hold all WooCommerce orders.
*/
function custom_woocommerce_auto_complete_order( $order_id ) {
if ( ! $order_id ) {
return;
}
$order = wc_get_order( $order_id );
if( 'processing'== $order->get_status() ) {
$order->update_status( 'on-hold' );
}
}
add_action( 'woocommerce_thankyou', 'custom_woocommerce_auto_complete_order' );
Is there anyway that I can edit this code and customer get the “On hold” email instead of processing email?
Please let me know if someone can help me.
Thank You!

Trigger woocommerce_payment_complete when creating order manually

I have a hook to woocommerce_payment_complete, in which I send the order to the distributor. This is working fine.
Now, since I'm also selling through 3rd party marketplace, sometimes I want to create an order form the admin panel, and I expect the woocommerce_payment_complete hook to be triggered by setting the order status to 'Processing' but it's not.
Is there any way to trigger this hook by creating an order manually?
Thanks
You can use the following to set 'processing' for admin orders. action_woocommerce_process_shop_order_meta is used to detect the order update.
// define the woocommerce_admin_order_actions_end callback
function action_woocommerce_admin_order_actions_end( $order_id ) {
global $woocommerce;
if (!$order_id)
return;
$order = new WC_Order($order_id);
$order_status = $order->get_status();
if ($order_status != "failed") {
$order->update_status('processing');
}
};
// add the action
add_action( 'action_woocommerce_process_shop_order_meta', 'action_woocommerce_admin_order_actions_end', 10, 1 );

How to update user meta field after a action is completed

I tried to update user meta field after the checkout payment is successful. I tried keeping the following code in thankyou.php
update_user_meta($user->ID, '_uw_balance', $updated_balance);
Actually i'm using a wallet plugin called "User Wallet Credit System" and using the user wallet balance to get deducted in the total order amount. I'm able to deduct the wallet amount in order total but unable to update the wallet after the successful payment.
I'm using woocommerce for the checkout and everything. I need to how should i update the wallet amount after the transaction is processed.
Use woocommerce_thankyou being called after successful checkout.
you can play with order id,
add_action('woocommerce_thankyou', 'update_e_wallet', 11, 1);
function update_e_wallet($order_id) {
//do something...
}
also you can get user by order id
$order = new WC_Order( $order_id );
$user_id = $order->user_id;

Resources