I'm using WooCommerce 2.0 and I would like to retrieve and display the ordered product name in the myaccount.php page, alongside the order number.
So if this is the default display in myaccount.php:
ORDER DATE STATUS TOTAL ACTIONS LICENSING
#521 August 19, 2014 Completed $99.99 for 1 item VIEW
I'd like to change it to:
ORDER DATE STATUS TOTAL ACTIONS LICENSING
#521-ProductName August 19, 2014 Completed $99.99 for 1 item VIEW
Can anyone offer any suggestions on how to retrieve the order product name? I am confused on how to do this.
Thanks!
You can do it after making some changes in your my-orders.php
you need my-orders.php. Place a copy in your theme folder to make it update proof.
//Add the following code in the customer_order loop
foreach($order->get_items() as $item) {
$product_name = $item['name'];
}
<?php echo $product_name;?> //echo product name
$args = array(
'post_type' => 'shop_order',
'post_status' => 'publish',
'meta_key' => '_customer_user',
'posts_per_page' => '-1'
);
$my_query = new WP_Query($args);
$customer_orders = $my_query->posts;
foreach ($customer_orders as $customer_order) {
$order = new WC_Order();
$order->populate($customer_order);
$orderdata = (array) $order;
// $orderdata Array will have Information. for e.g Shippin firstname, Lastname, Address ... and MUCH more.... Just enjoy!
}
And in order to get more details of the order you can use the below code.
Assuming $post->ID is the id of the product you want to display the orders containing, this is what you need:
$products = array();
foreach (get_posts('post_type=shop_order&numberposts=-1&post_status=publish') as $order) {
$order = new WC_Order($order->ID);
foreach($order->get_items('line_item') as $item) {
$product_id = (!empty($item['variation_id'])) ? $item['variation_id'] : $item['product_id'];
$products[] = $product_id;
}
if (in_array($post->ID,$products)) {
echo 'Status: '.$order->order_status;
echo '<br>Date : '.$order->order_date;
echo '<br>Email : '.$order->billing_email;
}
}
Related
I created the attached loop to be able to view all the products purchased by users. However, no duplicate products are shown. How can I change everything to show duplicate products too? Anyone big to help me? Thank you in advance.
<?php
$user_id = get_current_user_id();
$current_user = wp_get_current_user();
$customer_email = $current_user->email;
$args = array(
'posts_per_page' => -1,
'post_type' => 'product',
);
$loop = new WP_Query($args);
if ($loop->have_posts()) : while($loop->have_posts()) : $loop->the_post();
if(wc_customer_bought_product($customer_email, $user_id, get_the_ID())) {
global $product;
$id = $product->get_id();
?>
I would suggest to start from the Order.
Get all orders from User
Loop through all orders from this User and create an array of all Products
Then flatten the array and you can play with the product.
It's also possible to get different data in the array. Check the link, in the section of the code // Get and Loop Over Order Items
Something like this. Flattening gives a nice clean array of product ids. Including duplicates. Tested on the most recent version of WooCommerce.
Note that you can also adjust the arguments for wc_get_orders to only include paid statuses. Check here for details on that.
$orders = wc_get_orders(array(
'customer_id' => get_current_user_id(),
));
// should be an array of objects
foreach($orders as $order) :
// loop through the orders
foreach ( $order->get_items() as $item_id => $item ) :
$ids[] = $item->get_product_id();
endforeach;
// add all ids in a new loop
$AllIds[] = $ids;
endforeach;
// flattens the array nicely.
$product_ids = array_merge(...array_values(($AllIds)));
I am trying to produce a plain output of order data. First step is a WP_QUery (perhaps) so I write this code;
$args = array (
'post_type' =>'shop_order',
'posts_per_page' => -1,
'post_status' => 'any',
//'p' => $post_id,
);
$order_query = new WP_Query( $args );
while ( $order_query->have_posts() ) :
$order_query->the_post();
echo the_ID();
echo ' : ';
the_title();
echo '<br/><br/>';
endwhile;
It obliging products a list of all orders, if I set the 'p' => $post_id where $post_id is a valid post ID, the query returns nothing.
Any idea why, hive mind?
Alternatively is there a Woocommerce way of producing a plain page with a layout like;
Order ID: 836
Order Status: ....
I assumed a WP_Query would be the obvious way but it is appearing like getting woocommerce order data is anything but straightforward.
Update 2
To get the order data for one order, you don't need WP_query. You can use directly:
$order = wc_get_order( $order_id );
$order->id; // order ID
$order->post_title; // order Title
$order->post_status; // order Status
// getting order items
foreach($order->get_items() as $item_id => $item_values){
// Getting the product ID
$product_id = $item_values['product_id'];
// .../...
}
Update 1
You should try this, as with array_keys( wc_get_order_statuses() you will get all order statuses and with 'numberposts' => -1, all existing orders.
Here is an alternative way (without WP_query or you can use thoses args in the WP_query array):
$customer_orders = get_posts( array(
'numberposts' => -1,
'post_type' => 'shop_order',
'post_status' => array_keys( wc_get_order_statuses() )
) );
// Going through each current customer orders
foreach ( $customer_orders as $customer_order ) {
// Getting Order ID, title and status
$order_id = $customer_order->ID;
$order_title = $customer_order->post_title;
$order_status = $customer_order->post_status;
// Displaying Order ID, title and status
echo '<p>Order ID : ' . $order_id . '<br>';
echo 'Order title: ' . $order_title . '<br>';
echo 'Order status: ' . $order_status . '<br>';
// Getting an instance of the order object
$order = wc_get_order( $order_id );
// Going through each current customer order items
foreach($order->get_items() as $item_id => $item_values){
// Getting the product ID
$product_id = $item_values['product_id'];
// displaying the product ID
echo '<p>Product ID: '.$product_id.'</p>';
}
}
The strict answer to this question is change 'p'=>$post_id' to 'post__in' => array($post_id)
...but the real answer, should anyone be treading this path, is that parsing the email is so much easier, woocommerce has done most of the work for you. There are other pratfalls along the way; 1. The post is protected and the order notes are in the excerpt so can't be displayed ( I could move them to meta but...) 2. The order items are within comments and have to be looped then parsed to produce meaningful output, so I might as well parse the email direct.
I need to get a user's done purchases in the last month by USER ID in Woocommerce.
Users have levels (Gold, Silver):
Gold members can purchase 4 items each month;
Silver members can buy 1 item per month.
I need to check this before adding an item to the cart. I don't want to use a plugin for just this feature (which could not found, BTW).
Is that possible?
How can I achieve this?
Thanks
It's possible to get the total items count bought by the current customer in the past 30 days.
Here is the code of this function based on this answer:
function current_customer_month_count( $user_id=null ) {
if ( empty($user_id) ){
$user_id = get_current_user_id();
}
// Date calculations to limit the query
$today_year = date( 'Y' );
$today_month = date( 'm' );
$day = date( 'd' );
if ($today_month == '01') {
$month = '12';
$year = $today_year - 1;
} else{
$month = $today_month - 1;
$month = sprintf("%02d", $month);
$year = $today_year - 1;
}
// ORDERS FOR LAST 30 DAYS (Time calculations)
$now = strtotime('now');
// Set the gap time (here 30 days)
$gap_days = 30;
$gap_days_in_seconds = 60*60*24*$gap_days;
$gap_time = $now - $gap_days_in_seconds;
// The query arguments
$args = array(
// WC orders post type
'post_type' => 'shop_order',
// Only orders with status "completed" (others common status: 'wc-on-hold' or 'wc-processing')
'post_status' => 'wc-completed',
// all posts
'numberposts' => -1,
// for current user id
'meta_key' => '_customer_user',
'meta_value' => $user_id,
'date_query' => array(
//orders published on last 30 days
'relation' => 'OR',
array(
'year' => $today_year,
'month' => $today_month,
),
array(
'year' => $year,
'month' => $month,
),
),
);
// Get all customer orders
$customer_orders = get_posts( $args );
$count = 0;
if (!empty($customer_orders)) {
$customer_orders_date = array();
// Going through each current customer orders
foreach ( $customer_orders as $customer_order ){
// Conveting order dates in seconds
$customer_order_date = strtotime($customer_order->post_date);
// Only past 30 days orders
if ( $customer_order_date > $gap_time ) {
$customer_order_date;
$order = new WC_Order( $customer_order->ID );
$order_items = $order->get_items();
// Going through each current customer items in the order
foreach ( $order_items as $order_item ){
$count++;
}
}
}
return $count;
}
}
This code goes in function.php file of your active child theme (or theme) or also in any plugin file.
The function accept an optional user_id argument (if needed). For example for a user_id with 56 value, you will use the function, this way:
// For user ID: "56".
$number_of_items_in_last_month = current_customer_month_count('56');
The function will get the current user ID in $user_id = get_current_user_id();, if you dont set an argument user_id value, this way:
// For current logged user.
$number_of_items_in_last_month = current_customer_month_count();
You can use this function in a conditional if statement to hide or replace the add-to-cart button through somme WooCommerce hooks or related templates.
You could get helped in that task, asking a new question including this code, and providing more details about how you want to do it.
This code is tested and works.
References: Checking if customer has already bought something in WooCommerce
So I have done a bunch of looking around the web and couldn't find a solution for this...
Basically what I am trying to do is display a product loop of all the products the user has purchased in the store just like displaying normal products.
If you still don't understand maybe this will help you get what I mean..
Here is the example product loop on the WooCommerce documentation...
<ul class="products">
<?php
$args = array(
'post_type' => 'product',
'posts_per_page' => 12
);
$loop = new WP_Query( $args );
if ( $loop->have_posts() ) {
while ( $loop->have_posts() ) : $loop->the_post();
woocommerce_get_template_part( 'content', 'product' );
endwhile;
} else {
echo __( 'No products found' );
}
wp_reset_postdata();
?>
</ul><!--/.products-->
So what if I wanted to display basically this same exact product loop however filter it out so that it only displays products that the user has already purchased.
I honestly do not know where to go with this one and I am sure there are others that have done research on this in the past so maybe this will help out a bunch of people!
Thanks in advance!
There are at least two different approaches you can take to solve this problem.
The first is to get the product from each post, and then get the product ID from each product and then use an if statement to filter using wc_customer_bought_product or woocommerce_customer_bought_product (if you are using old WooCommerece).
The second is to pass the correct arguments to filter the WP_Query to only include orders purchased by a user and then filter products only in those orders. More information on the second approach is available at Get All User Orders and Products bought by user in WooCommerce based shop (archive.org).
An example of the first approach is something like
<!-- code started -->
<ul class="products">
<?php
$user_id = get_current_user_id();
$current_user= wp_get_current_user();
$customer_email = $current_user->email;
$args = array(
'post_type' => 'product',
'posts_per_page' => 12
);
$loop = new WP_Query( $args );
if ( $loop->have_posts() ) {
while ( $loop->have_posts() ) : $loop->the_post(); $_product = get_product( $loop->post->ID );
if (wc_customer_bought_product($customer_email, $user_id,$_product->id)){
woocommerce_get_template_part( 'content', 'product' );
}
endwhile;
} else {
echo __( 'No products found' );
}
wp_reset_postdata();
?>
</ul><!--/.products-->
Kudos to Appleman1234 for providing two answers, both of which will work.
ApppleMan1234's first answer that he provided an example for is to loop through all products and then filter them by calling wc_customer_bought_product(). This certainly will work. If you have n products then you are going to make n+1 database queries.
His second suggestion is a link to a post written by Brajesh Singh who, on June 2, 2013, published a solution on fusedpress.com. The original post is no longer available. I found a cached copy at Google.
Brajesh Singh's solution queries the user's orders, then queries the order details, and last queries the product id in the order item's metadata. This solution then is always only 3 queries. Unless your shop only has 1 or 2 products, this solution is far better.
Here is a slightly edited version of Brajesh Singh's code.
/**
* Get all Products Successfully Ordered by the user
* #return bool|array false if no products otherwise array of product ids
*/
function so28362162_get_all_products_ordered_by_user() {
$orders = so28362162_get_all_user_orders(get_current_user_id(), 'completed');
if(empty($orders)) {
return false;
}
$order_list = '(' . join(',', $orders) . ')';//let us make a list for query
//so, we have all the orders made by this user that were completed.
//we need to find the products in these orders and make sure they are downloadable.
global $wpdb;
$query_select_order_items = "SELECT order_item_id as id FROM {$wpdb->prefix}woocommerce_order_items WHERE order_id IN {$order_list}";
$query_select_product_ids = "SELECT meta_value as product_id FROM {$wpdb->prefix}woocommerce_order_itemmeta WHERE meta_key=%s AND order_item_id IN ($query_select_order_items)";
$products = $wpdb->get_col($wpdb->prepare($query_select_product_ids, '_product_id'));
return $products;
}
/**
* Returns all the orders made by the user
* #param int $user_id
* #param string $status (completed|processing|canceled|on-hold etc)
* #return array of order ids
*/
function so28362162_get_all_user_orders($user_id, $status = 'completed') {
if(!$user_id) {
return false;
}
$args = array(
'numberposts' => -1,
'meta_key' => '_customer_user',
'meta_value' => $user_id,
'post_type' => 'shop_order',
'post_status' => 'publish',
'tax_query' => array(
array(
'taxonomy' => 'shop_order_status',
'field' => 'slug',
'terms' => $status
)
)
);
$posts = get_posts($args);
//get the post ids as order ids
return wp_list_pluck($posts, 'ID');
}
Combining that with a product loop from the question, plus a non-deprecated wc_get_template_part() and an addition of posts_per_page=-1 gives us
<ul class="products">
<?php
$args = array(
'post_type' => 'product',
'post__in' => so28362162_get_all_products_ordered_by_user(),
'posts_per_page' => -1
);
$loop = new WP_Query($args);
if($loop->have_posts()) {
while($loop->have_posts()) : $loop->the_post();
wc_get_template_part('content', 'product');
endwhile;
}
else {
echo __('No products found');
}
wp_reset_postdata();
?>
</ul><!--/.products-->
Not sure if this helps you out at all, but there is a plugin developed by WooThemes to support purchase history.
What I am trying to do is to be able to search by order item SKU or ID in the WooCommerce Orders Admin page.
What I have found/done till now, but with no success is the following at functions.php file.
add_filter( 'woocommerce_shop_order_search_fields', 'woocommerce_shop_order_search_sku' );
function woocommerce_shop_order_search_sku( $search_fields ) {
$args = array( 'post_type' => 'shop_order' );
$orders = new WP_Query( $args );
if ( $orders->have_posts() ) {
while( $orders->have_posts() ) {
$post = $orders->the_post();
$order_id = get_the_ID();
$order = new WC_Order( $order_id );
$items = $order->get_items();
foreach( $items as $item ) {
$search_order_item_sku = wp_get_post_terms( $item['product_id'], 'search_sku' );
foreach( $search_order_item_sku as $search_sku ) {
add_post_meta( $order_id, "_search_sku", $search_sku->sku );
}
}
}
};
$search_fields[] = '_search_sku';
return $search_fields;
}
I suppose the issue is the value of $search_sku at the line with the add_post_meta.
I have also tried it with get_sku(), $item['sku'] with no luck.
You have the right idea about saving extra metadata to the order. As jbby and helgatheviking suggest, there is no built-in postmeta for product_id or sku available by default in the woocommerce orders api. Your methodology for accessing and saving the metadata wasn't quite right, however. wp_get_post_terms will access custom taxonomy information, not metadata (use get_post_meta for that). You will be able to do what you were trying to do with this filter:
add_filter( 'woocommerce_shop_order_search_fields', function ($search_fields ) {
$posts = get_posts(array('post_type' => 'shop_order'));
foreach ($posts as $post) {
$order_id = $post->ID;
$order = new WC_Order($order_id);
$items = $order->get_items();
foreach($items as $item) {
$product_id = $item['product_id'];
$search_sku = get_post_meta($product_id, "_sku", true);
add_post_meta($order_id, "_product_sku", $search_sku);
add_post_meta($order_id, "_product_id", $product_id);
}
}
return array_merge($search_fields, array('_product_sku', '_product_id'));
});
Strictly speaking you should probably move the calls to add_post_meta into a hook that runs when the order is originally saved to the database--this will prevent unnecessary legwork whenever you search through order.
#blacksquare, #jibby, #helgatheviking you are the men! This is the code that works, due to your help.
//Search by product SKU in Admin Woocommerce Orders
add_filter( 'woocommerce_shop_order_search_fields', function ($search_fields ) {
$posts = get_posts(array('post_type' => 'shop_order'));
foreach ($posts as $post) {
$order_id = $post->ID;
$order = new WC_Order($order_id);
$items = $order->get_items();
foreach($items as $item) {
$product_id = $item['product_id'];
$search_sku = get_post_meta($product_id, "_sku", true);
add_post_meta($order_id, "_product_sku", $search_sku);
}
}
return array_merge($search_fields, array('_product_sku'));
});
While #Nikos and #blacksquare 's answers work, new post metas are added to every order on every search. If you have 100 orders and make 10 searches, there will be at least 100*10 = 1000 _product_sku entries in the wp_postmeta table. If some orders contain multiple products, there will be even more.
As #blacksquare suggested, add_post_meta should be called when the order is saved. That said, if the site is small and backend search performance isn't too much of a concern, the following code would work without creating redundant _product_sku entries.
add_filter( 'woocommerce_shop_order_search_fields', 'my_shop_order_search_fields') );
public function my_shop_order_search_fields( $search_fields ) {
$orders = get_posts( array(
'post_type' => 'shop_order',
'post_status' => wc_get_order_statuses(), //get all available order statuses in an array
'posts_per_page' => 999999, // query all orders
'meta_query' => array(
array(
'key' => '_product_sku',
'compare' => 'NOT EXISTS'
)
) // only query orders without '_product_sku' postmeta
) );
foreach ($orders as $order) {
$order_id = $order->ID;
$wc_order = new WC_Order($order_id);
$items = $wc_order->get_items();
foreach($items as $item) {
$product_id = $item['product_id'];
$search_sku = get_post_meta($product_id, '_sku', true);
add_post_meta( $order_id, '_product_sku', $search_sku );
}
}
return array_merge($search_fields, array('_product_sku')); // make '_product_sku' one of the meta keys we are going to search for.
}
While a better solution might be calling add_post_meta when an order is created, extra efforts are needed to create _product_sku for existing orders, and you have to create the _product_sku for orders made while the code isn't activated. For simplicity sake, I'd just use the solution suggested above.
p.s. #Nikos 's solution does have one (debatable) advantage - if you change a product's SKU after orders are made, Nikos's solution will find those orders using the new SKU, while the solution above will not. That said, a product's SKU should NOT be changed anyway, and it's debatable whether searching new SKUs should show old orders.
You may also want to look into https://uk.wordpress.org/plugins/woo-search-order-by-sku/ which does not overloads post meta, but alters the where clause.
As stated in answer by Ming Yeung, any solution which uses the add_post_meta() function will increase your database size, which might not be ideal for larger stores. That sort of approach will also potentially make the searching of Woocommerce orders in admin quite slow.
An alternative solution is to make use of one of the existing "index" post meta records that Woocommerce uses, and append your data to those records (but only append once).
The following code does exactly that. It does not add SKU (as asked in question) but instead allows for Woocommerce order search to include Country name (as opposed to country code). However this code could be easily adapted to also include SKU if needed:
// when doing a search within Woocommerce orders screen, include the Country name as part of the search criteria
// this code has been optimised to do as little DB work as possible (looks for any index values which haven't been updated yet, and updates them)
// this code also makes use of existing post meta "index" records, instead of creating additional new post meta rows (which would fill up database unnecessarily)
add_filter('woocommerce_shop_order_search_fields', function($search_fields) {
global $wpdb;
$records_to_update = $wpdb->get_results("SELECT * FROM $wpdb->postmeta WHERE (meta_key LIKE '%_address_index%') AND (meta_value NOT LIKE '%[HAS_COUNTRY]%')");
foreach ($records_to_update as $record) {
$order = new WC_Order($record->post_id);
if ($record->meta_key == '_billing_address_index') {
$country_name = WC()->countries->countries[ $order->get_billing_country() ];
} else {
$country_name = WC()->countries->countries[ $order->get_shipping_country() ];
}
update_post_meta($record->post_id, $record->meta_key, $record->meta_value . ' ' . $country_name . ' [HAS_COUNTRY]');
}
return $search_fields;
});