I want get the last order ID done after the placed order. I have used this for get the last order id.
global $wpdb;
$results = $wpdb->get_results( ' SELECT * FROM `wp_woocommerce_order_items`
ORDER BY `wp_woocommerce_order_items`.`order_item_id` DESC
LIMIT 1', OBJECT );
But apart from this, is there any other woocommerce function or hook to get the last order ID.
Thanks in advance.
If you just placed an order and nothing else then you can use ' $wpdb->insert_id'.. it will do the trick.
Related
I'm trying to make an alphabetical index with wordpress, so far I've managed to get the $letter parameter and make the query, but I can't sort the results by a specific category_name, here's my code:
global $wpdb;
$results = $wpdb->get_results(
"
SELECT * FROM $wpdb->posts
WHERE post_title LIKE '$letter%'
AND post_type = 'directory'
AND category_name = 'company';
"
);
Everything works just fine untill the query gets to the category_name, I checked the Wordpress Codex but I didn't get it very well.
I would appreciate any help, thanks!
I am trying to hook into a 3rd party application when a cart is checked out. Essentially i need to pass information about the order such as the products that are inside the order.
Everything i find points me towards the hook: woocommerce_new_order
When i use that hook i can get some information about the order but not everything.
add_action('woocommerce_new_order','order_check',10,1);
function order_check($order_id){
echo 'Order id is: '.$order_id;
$order = new WC_Order($order_id);
print_r($order);
echo '-----';
/** CHECK IF order has items */
$order_item = $order->get_items();
print_r($order_item);
exit;
}
For instance the above code sample will print the order array but when it call $order->get_items() nothing is returned.
At the time woocommerce_new_order fires, order items is not yet populated.
Instead, use the hook woocommerce_checkout_order_processed and you'll find that all items are then populated.
I can see inside class-wc-admin-cpt-shop_order.php there are some functions that are pulling together the order information for display in WooCommerce. However, I don't see anywhere where the date can be used ...
Because WooCommerce uses wp_posts to store the data, can I assume that the post_date field is the correct one to use?
Also, anyone know whether there is a function in WooCommerce to get this, or whether there is a way of getting the date to come out in class-wc-admin-cpt-shop_order.php.
// Get $order object from order ID
$order = wc_get_order( $order_id );
// Get Order Dates
$order->get_date_created();
$order->get_date_modified();
$order->get_date_completed();
$order->get_date_paid();
Source: https://businessbloomer.com/woocommerce-easily-get-order-info-total-items-etc-from-order-object/
Additionally
$order->get_date_created();
Is the "order date", which you can change within WooCommerce ("Edit Order")
You can use the WC_Order object, if you have the order ID:
$order = new WC_Order($order_id);
$order_date = $order->order_date;
Order properties should not be accessed directly. Best way is $order->get_date_completed()
I want to show the data by sorting in wordpress using a dropdown. So, how add sorting on comments by dropdown in wordpress ?
The function get_comments(); has an orderby argument that you can pass in order to change the order that they are shown:
$orderby
(string) (optional) Set the field used to sort comments.
Default: comment_date_gmt
$order
(string) (optional) How to sort $orderby. Valid values:
'ASC' - Ascending (lowest to highest).
'DESC' - Descending (highest to lowest).
Default: DESC
You should then be able to edit the HTML of the template file inside comments.php or single.php and do a POST to self with the value, changing the query.
My WordPress plugin has a table with a AUTO_INCREMENT primary key field called ID. When a new row is inserted into the table, I'd like to get the ID value of the insertion.
The feature is to using AJAX to post data to server to insert into DB. The new row ID is returned in the AJAX response to update client status. It is possible that multiple clients are posting data to server at the same time. So, I have to make sure that each AJAX request get the EXACT new row ID in response.
In PHP, there is a method called mysql_insert_id for this feature.But, it is valid for race condition only if the argument is link_identifier of last operation. My operation with database is on $wpdb. How to extract the link_identifier from $wpdb to make sure mysql_insert_id work? Is there any other way to get the last-inserted-row id from $wpdb?
Thanks.
Straight after the $wpdb->insert() that does the insert, do this:
$lastid = $wpdb->insert_id;
More information about how to do things the WordPress way can be found in the WordPress codex. The details above were found here on the wpdb class page
This is how I did it, in my code
...
global $wpdb;
$query = "INSERT INTO... VALUES(...)" ;
$wpdb->query(
$wpdb->prepare($query)
);
return $wpdb->insert_id;
...
More Class Variables
I needed to get the last id way after inserting it, so
$lastid = $wpdb->insert_id;
Was not an option.
Did the follow:
global $wpdb;
$id = $wpdb->get_var( 'SELECT id FROM ' . $wpdb->prefix . 'table' . ' ORDER BY id DESC LIMIT 1');
Get the last inserted row id in WP like this.
global $wpdb;
$order = ['product_name'=>'Computer', 'os_system'=>'Linux'];
$wpdb->insert('wp_product_orders', $order );
$last_inserted_id = $wpdb->insert_id;
Something like this should do it too :
$last = $wpdb->get_row("SHOW TABLE STATUS LIKE 'table_name'");
$lastid = $last->Auto_increment;
just like this :
global $wpdb;
$table_name='lorem_ipsum';
$results = $wpdb->get_results("SELECT * FROM $table_name ORDER BY ID DESC LIMIT 1");
print_r($results[0]->id);
simply your selecting all the rows then order them DESC by id , and displaying only the first
Putting the call to mysql_insert_id() inside a transaction, should do it:
mysql_query('BEGIN');
// Whatever code that does the insert here.
$id = mysql_insert_id();
mysql_query('COMMIT');
// Stuff with $id.