$order_id issue with a WooCommerce Order - woocommerce

Since I upgraded from WooCommerce 2.6 to 3 I have run into a problem whereby some custom code of mine does not run when an order is processed. I have been in touch with WooCommerce's help people without luck and I have been through many pages on Stackoverflow without seeing mention of the issue.
In my functions.php file I have this code that does an update to the database of a custom field (a website address) ... and it works 100% (both in WC 2.6 & WC 3):
add_action( 'woocommerce_checkout_update_order_meta', 'my_custom_checkout_field_update_order_meta' );
function my_custom_checkout_field_update_order_meta( $order_id ) {
if ( ! empty( $_POST['website_address'] ) ) {
update_post_meta( $order_id, 'Website Address', sanitize_text_field( $_POST['website_address'] ) );
}
}
The fact that this code works every time suggests to me that the value of $order_id must exist at this step.
But if, just before the successful code runs, I have a piece of code such as :
IF ($order_id) {
// Email me ...
}
... then the email never fires.
In version 2.6 I had no issue with this and the email would fire off and include the value of $order_id.
I have even experimented with leaving out the IF statement for 'IF ($order_id)' so as to test if the email script is working fine and it is.
So my question is, how come I cannot access the value of $order_id yet the code immediately after, that uses $order_id, works fine?
Any suggestions / insights welcome.
Thanks.

Try updating this line
add_action( 'woocommerce_checkout_update_order_meta', 'my_custom_checkout_field_update_order_meta', 10, 1 );
You should mention parameters that you expect while adding hook. It should work for you.

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.

Woocommerce DISABLE autocomplete € 0 orders

I wish i could just use Uncle Google but he serve me only what i DON'T want :D
I have a free Product on my website. But I want to check every order manualy, because it's only for company accounts and not for "customers".
But every order will set the Status to approved - or what ever in english language- and the order is complete.
The user has automaticaly acces to "restricted area" and that's what I don't want. I want to check every order manualy and pick every spam account.
I can imagine, this is going to work with only one simple function but I can't get it. It's only one free Product, other Products are for moneeeeey.
It would be great if somebody has the same issue and can help me :)
Thank you
Place the following function in your active theme functions.php. Check all default statuses here and change on-hold to what you want - https://woocommerce.wp-a2z.org/oik_api/wc_get_order_statuses/
function change_free_order_status( $order_id ) {
if ( ! $order_id ) {return;}
$order = wc_get_order( $order_id );
if($order->get_total() <= 0):
$order->update_status( 'on-hold' ); // Change to what you need
endif;
}
add_action('woocommerce_thankyou','change_free_order_status');

WooCommerce woocommerce_new_order not firing when creating an order from the REST API

I have the following code snippet:
add_action('woocommerce_new_order', 'foo_function', 10);
If I create a new order from the admin panel, this fires just fine.
However, creating it via the REST API will not fire the function.
Why is that?
Update
I've tried using the following:
woocommerce_rest_insert_shop_object – this one doesn't fire at all.
wp_insert_post and save_post – they do fire, but don't contain the line items... (on the first run, it's an empty list, and on the second run (where the $update flag is true) there is an item but that item has no data (like product id or quantity).
add_action( "woocommerce_rest_insert_shop_order_object", 'your_prefix_on_insert_rest_api', 10, 3 );
function your_prefix_on_insert_rest_api( $object, $request, $is_creating ) {
if ( ! $is_creating ) {
return;
}
$order_id = $object->get_id();
$wc_order = new WC_Order( $order_id );
do_action( 'woocommerce_new_order', $order_id, $wc_order );
}
You have tried woocommerce_rest_insert_shop_object,
but the hook is woocommerce_rest_insert_shop_order_object
Please use this hook woocommerce_process_shop_order_meta instead of woocommerce_new_order As far as I can determine, woocommerce_new_order is only fired when an order is processed through the checkout. In a lot of instances, staff were creating orders in the wp-admin and assigning them straight to processing or completed, which meant that hook wasn't doing what I thought it would. Using woocommerce_process_shop_order_meta will solve it for you.
On v2 there is the woocommerce_rest_insert_{$this->post_type}_object filter
So the correct hook for what you need is woocommerce_rest_insert_shop_order_object

WooCommerce Eway plugin not creating customer token for new customer

I have setup a fresh docker container with Wordpress 5.0.3 and the latest WC and WC Eway plugin (WooCommerce eWAY Gateway).
Created a store with some products, hooked up my Eway sandbox environment, enabled Save Cards (which would enable the token) and created an order.
After checking the post_meta in my DB for the order, I didn't see a _eway_token_customer_id field. While being logged in as a customer, I tried again and with the new order I still do not get a token.
The reason for this tests is that I got this strange behaviour in my real, new website, where the first order with a NEW customer, doesn't result in a token.
However, when I create a second order whilst being logged in, I do get a _eway_token_customer_id value within the order_meta.
It is imperative for me to get that token with the first order, because after that I will auto renew the product using the tokenp ayment option.
Debugging this issue is hell, and I find it very disconcerting that on my fresh WP installation I get no token at all.
Is there anyone that has a bright idea?
**update
After some digging around in the Eway Plugin, I found out that the first time I do an order, the function request_access_code() from the class WC_Gateway_EWAY is checking if there is a token in the database for this user.
The function body:
protected function request_access_code( $order ) {
$token_payment = $this->get_token_customer_id( $order );
if ( $token_payment && 'new' === $token_payment ) {
$result = json_decode( $this->get_api()->request_access_code( $order, 'TokenPayment', 'Recurring' ) );
} elseif ( 0 === $order->get_total() && 'shop_subscription' === ( version_compare( WC_VERSION, '3.0', '<' ) ? $order->order_type : $order->get_type() ) ) {
$result = json_decode( $this->get_api()->request_access_code( $order, 'CreateTokenCustomer', 'Recurring' ) );
} else {
$result = json_decode( $this->get_api()->request_access_code( $order ) );
}
if ( isset( $result->Errors ) && ! is_null( $result->Errors ) ) {
throw new Exception( $this->response_message_lookup( $result->Errors ) );
}
return $result;
}
The function handles three possible outcomes:
1) new customer: results in calling `$this->get_api()->request_access_code( $order, 'TokenPayment', 'Recurring' )` <-- this is the one we are after!
2) shop_subscription: calls `$this->get_api()->request_access_code( $order, 'CreateTokenCustomer', 'Recurring' )`
3) else..: calls `$this->get_api()->request_access_code( $order )`
What is happening during debugging, is that the $token_payment variable has the value of an empty string for a new customer, instead of new.
So I will attempt to fix this, either via a filter/action hook, or figure out why this is happening.
When I forced the function the always use the first if block, I got my token. :)
**Update 2:
I tested with an existing user account, created a new order.
When I look in the post_meta table:
Voila, the new value is present.
However, when I am not logged in and I create an account, the new value is not added and that is where it goes wrong.
A temp fix would be to use a hook and add the new value to the order so that when get_token_customer_id is called it retrieves a new value and not an empty string.
I think this is a bug, since this value should be added. It explains why the second transactions get the token but not the first.
If only Woocommerce Eway plugin had a git repo.... I could flag an issue or fork it.
***Solution without hack
Added this to my plugin (or functions.php if you like):
add_action( 'woocommerce_checkout_order_processed', function( $order_id, $posted_data, $order ) {
update_post_meta( $order_id, '_eway_token_customer_id', 'new' );
}, 10, 3);
This will add the new value when you checkout with a non-existent user.
The token was added nicely after adding my creditcard details.
The matter of the fact stays that the plugin still has a bug, which you can work around.

Bulk restore revision on all posts on wordpress

Is there a wordpress plugin to restore revisions on multiple posts at once based on date or position?
I have a hacked site in which every post was changed. To make all content clean I would need to restore all posts to a wherever revision was online 5 or 6 days ago.
Thanks in advance!
Honestly, with a hacked site - your best bet is to restore a Database/MySQL backup from a time when it was "clean", or just reverting them manually.
That said, WordPress does let you retrieve revisions using wp_get_post_revisions().
NOTE: This is the plural function, the singular function wp_get_post_revision() get a revision by its ID. Unless you know what the ID of the revision you want is, you don't want this function.The plural function gets all revisions of a post.
Now, before you continue reading - BACK UP YOUR SITE.
Did you back it up?
Make sure you HAVE A BACKUP.
Also, make sure it's a backup that works.
Now, that you have a backup...
Take a look at this function that I came up with, it uses wp_get_post_revisions() in conjunction with wp_update_post(). Again, PLEASE make sure you back up before running this function. It's going to mess with posts and post content, and if it stalls/dies, it could get messy. Use at your own risk.
First, it makes sure you WANT to run this function by visiting https://yoursite.com/?restore_revisions=so_50959583_restore_revisions. It needs that query string to run.
Then it makes sure the current user has admin level privileges.
Then it checks to see if this function has run before, so it doesn't run every time.
Now it runs a standard WP_Query to grab all posts. Note I've ommitted other post types, you can add those in if you want.
Inside the loop, it checks to see if a revision exists, and if so it uses wp_update_post to update the post to the revised post_content.
If it fails here, it will die with a message to prevent setting the flag we checked in the beginning.
Provided it runs smoothly, it sets the so_50959583_restore_revisions_flag option flag so it won't ever run again.
At this point, you should remove the code.
NOTE you could remove the option flag, and set this up as a custom plugin and run the function off of register_activation_hook.
So, here's the beast, best used for illustrative purposes, use it at your own risk.
add_action( 'init', 'so_50959583_restore_revisions' );
function so_50959583_restore_revisions(){
if( isset( $_GET['restore_revisions'] ) && $_GET['restore_revisions'] == 'so_50959583_restore_revisions' ){
if( current_user_can( 'manage_options' ) ){
if( !get_option( 'so_50959583_restore_revisions_flag' ) ){
// This hasn't run before, run it now.
$args = array(
'post_type' => 'post', // Limit to just posts
'posts_per_page' => -1
);
$revision_query = new WP_Query( $args );
if( $revision_query->have_posts() ){
while( $revision_query->have_posts() ){
$revision_query->the_post();
if( $last_revision = array_shift( wp_get_post_revisions( $post->ID ) ) ){
// At least one revision existed.
$reverted_post = array(
'ID' => get_the_ID(), // Update current post in query
'post_content' => $last_revision->post_content // set content to previous content
);
// Update the existing post
if( !wp_update_post( $reverted_post ) ){
wp_die( 'Something went wrong' );
}
}
}
wp_reset_postdata();
}
// Whew, completed!
update_option( 'so_50959583_restore_revisions_flag', true );
}
}
}
}
I would like to stress, again, that your best bet is to keep backups of your database using one of the many available (and ofttimes FREE) plugins, to prevent issues like this from occurring.
Last, if you only have like 20-30 posts, you should just do it by hand. And if you have like 10,000 posts you may need to have this code do it in "chunks" or it may timeout.
Best of luck

Resources