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.
Related
Im new to WP Query and ACF custom fields. I want to write a code that will show first 3 results from the calculation of the total_score custom field. I have managed to short by the total score but I want to show the title and permanlink of the first 3 posts so the visitor will click and go to the post. Any help will be much appreciated. My code so far :
$args = array(
'posts_per_page' => -1,
'post_title' => true,);
$all_posts = array();
$the_query = new WP_Query( $args );
if ( $the_query->have_posts() ):
while ( $the_query->have_posts() ): $the_query->the_post();
// Get all fields
$fields = get_fields();
// Push each $fields array into the $all_posts array
array_push($all_posts, $fields);
endwhile;
// Restore original Post Data
wp_reset_postdata();
// Print the result here and do what you choose
print_r($all_posts);
endif;
if(isset($_POST['place']))
{ // start the loop
$q1=$_POST["place"];
//CORECT CODE !!!!
foreach($all_posts as &$value) {
if ($value['question1']==$q1){
$value['total_score']=$q1;
}
} } //end question 1
// question 2
if(isset($_POST['home']))
{ // start the loop
$q2=$_POST["home"];
foreach($all_posts as &$value) {
if ($value['question2']==$q2){
$value['total_score']=$value['total_score']+$q2;
}
//echo $value['total_score']."<br>"; }
//echo "Q2"."<br>";
//print_r($all_posts);
} //end question 2
// question 3
if(isset($_POST['hours']))
{ // start the loop
$q3=$_POST["hours"];
//CORECT CODE !!!!
foreach($all_posts as &$value) {
if ($value['question2']==$q3){
$value['total_score']=$value['total_score']+$q3;
}
}
//echo "Q2"."<br>";
} //end question 3
// shorting by total_score
function sortByOrder($a, $b) {
return $b['total_score'] - $a['total_score'];
}
usort($all_posts, 'sortByOrder');
//print_r($all_posts);
foreach($all_posts as &$value) {
echo $value['total_score']."<br>";
}
Please replace your code with only this code. It's give you only 3 post and your total_score ASC or DESC
and Replace data in as per comment
Let me know if any Query
<?php
$args = array(
'post_type' => 'portfolio', // your post type name
'orderby' => 'meta_value_num', // if total_score is string then use it - meta_value
'meta_key' => 'total_score', // your meta key name ( total_score custom field )
'posts_per_page' => 3,
'order' => 'ASC' // ASC or DESC
);
$loop = new WP_Query( $args );
if ( $loop->have_posts() ) :
while ( $loop->have_posts() ) : $loop->the_post();
echo ''.get_the_title().'';
endwhile;
endif;
wp_reset_postdata();
You could look at using your $args variable to both limit and filter the results of WP_Query. Setting your 'posts_per_page' => 3. This means only 3 posts will be returned in the query.
However, as you are fetching all of the posts and using usort to sort them, you can swap this out to use this resource to help you query using a custom field. This will reduce the amount of work is needed to compile the posts you require.
You can then use the final result to loop over the 3 posts and output your title & permalink.
I have a custom Wordpress widget which allows the user to filter products via attributes (custom taxonomies).
It all happens via an Ajax call, but I'm having trouble keeping the pagination up to date based on the filtered results.
For example:
If the page loads 30 products, 10 to a page = 3 pages of results.
The user then filters by an attribute which reduces that 30 products to 20. I need the pagination to change to just 2 pages of results.
Here's a sample of the WP_Query that replaces the default page content. You can see the woocommerce_pagination() which doesn't appear to work in this environment.
// Args
$args = array(
'post_type' => 'product',
'posts_per_page' => 10,
'orderby' => 'name',
'order' => 'ASC',
'tax_query' => $tax_query
);
$query = new WP_Query( $args );
if( $query->have_posts() ) :
woocommerce_product_loop_start();
while( $query->have_posts() ): $query->the_post();
wc_get_template_part( 'content', 'product' );
endwhile;
woocommerce_product_loop_end();
// TODO - get pagination working
woocommerce_pagination();
wp_reset_postdata();
else :
echo '<p>No products found</p>';
endif;
WooCommerce pagination works based on global $wp_query variable. But you are using your own $query variable. That's why it is obvious why it is not working.
You have 2 ways to go:
First is using query_posts instead of WP_QUERY class.
The second way is a small hack, where you can cheat $wp_query pagination argument.
Here it is:
global $wp_query;
$wp_query->max_num_pages=$query->max_num_pages;
// TODO - get pagination working
woocommerce_pagination();
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 have used ACF. I have added the 'taxonomy' select dropdown field to the post edit page. From the dropdown I select which category this post should be featured in, but my code is displaying the same post as featured across all categories.
Below is the code in the category.php file. I need it to display the most recent post which has been given a 'Feature In Category', and to therefore be featured in the category I have defined.
My current loop in category.php
<?php
$category = get_field('feature_in_category');
// args
$args = array(
'numberposts' => -1,
'posts_per_page' => 1,
'category__in' => $category,
'orderby'=> 'modified'
);
// get results
$the_query = new WP_Query( $args );
// The Loop
?>
<?php if( $the_query->have_posts() ): ?>
<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<div class="small-6 columns">
<div class="img-box-shadow">
<a href="<?php the_permalink(); ?>">
<?php echo the_post_thumbnail(); ?>
</a>
</div>
</div>
<div class="small-6 columns">
<h3><?php echo the_title(); ?></h3>
<p><?php echo the_excerpt(); ?></p>
</div>
<?php endwhile; ?>
<?php else : echo '<p style="color:#fff;">no posts</p>'; ?>
<?php endif; ?>
<?php wp_reset_query(); // Restore global post data stomped by the_post(). ?>
Pastebin: http://pastebin.com/NR3UanAd
I'm not very familiar with ACF and haven't yet used it. I had a look at some documentation etc, and this is what I've found:
ACF TAXONOMY FIELD
The ACF taxonomy field will return NULL in your application as the ID being passed to get_field is a category ID, which is a non valid post ID. Yes, you can pass a category ID to get_field, but then there must be a custom field assigned to that particular category in order to return the value of that custom field.
Look at the ACF docs
$field = get_field($field_name, $post_id, $format_value);
◦$post_id: Specific post ID where your value was entered. Defaults to current post ID (not required). This can also be options / taxonomies / users / etc
ACCESS STORED DATA BY ACF
As I stated before, I'm not familiar with ACF but it seems that ACF field data is stored in the same way as data is stored by the default custom fields. So you will have a matching pair of key=>value sets. This data can be accessed and retrieved by a meta_query
THE IDEA
The ACF Taxonomy dropdown saves a selected value which translated into a category. Again, I'm not sure whether it saves the name, slug or ID, but from what I can pick up, it saves the category ID, so I will make my assumptions on that
So, the first thing to do here is, get the currently viewed category's ID. This can can be retrieved by get_queried_object_id()
Now that you have the current category ID, you will need to match that to a value for the specific meta_key=feature_in_category and return the posts that has this specififc key=>value pair attached to it
THE CODE
Your code should look something like this with the assumption ACF data is stored in the same way as data is stored from a custom field (This code requires PHP5.4+, for earlier versions, change [] to array())
$cat_id = get_queried_object_id(); // Assumption that category ID is saved by ACF
$args = [
'posts_per_page' => 1,
'orderby' => 'modified',
'meta_key' => 'feature_in_category',
'meta_value_num' => $cat_id, // Assumption that category ID is saved by ACF
];
$q = new WP_Query( $args );
if( $q->have_posts() ) {
while( $q->have_posts() ) {
$q->the_post();
// YOUR TEMPLATE TAGS AND MARKUP
}
wp_reset_postdata();
}
If the category ID is not saved by the ACF field, and it saves the name or slug, you can change the code as follows
$category = get_queried_object()->name; // For category name
or
$category = get_queried_object()->slug; // For category slug
Then in your query arguments, change
'meta_value_num' => $cat_id, // Assumption that category ID is saved by ACF
to
'meta_value' => $category,
EDIT
In addition and posted as a comment, the query aruments as used by the OP, working.
$cat_id = get_queried_object_id();
$args = [
'posts_per_page' => 1,
'orderby' => 'modified',
'meta_query' => [
[
'key' => 'feature_in_category',
'value' => $cat_id,
'compare' => 'IN'
]
]
];
After reviewing the documentation here http://codex.wordpress.org/Class_Reference/WP_Query it appears that your code is doing all the right stuff. I believe your answer lies in the actual query string that is being used to produce your results. You should be able to see that query string in the properties of the query object by doing:
<?php
$category = get_field('feature_in_category');
// args
$args = array(
'numberposts' => -1,
'posts_per_page' => 1,
'category__in' => $category,
'orderby'=> 'modified'
);
// get results
$the_query = new WP_Query( $args );
echo '<pre>'; print_r($the_query); die();
I suggest running the query string once you see it directly on the database using something like phpMyAdmin or Navicat to connect directly to the database. You should be able to adjust your query there to produce your desired results and then adjust your arguments accordingly. It may also be that there is nothing wrong with your query and that the issue lies with the category functionality instead. This debugging method should reveal that as well if that is the case.
UPDATE:
I think you just need to use 'cat' and not 'category__in'. Also, make sure you use what you set for Field Name inside of the call to get_field. This feedback is based on this example: http://www.the-perfect-life.org/html-5-css-3-php-wordpress-jquery-javascript-photoshop-illustrator-tutorial/how-to-create-custom/wordpress-plugin-theme-codex-code-function/advanced-custom-fields-get-field-taxonomy-query-related-posts-by-tag-and-category/
$categoryValue = get_field('feature_in_category');
$args=array(
'cat' => $categoryValue,
'posts_per_page'=>1 // Number of related posts to display
);
$my_query = new wp_query( $args );
while( $my_query->have_posts() ) {
$my_query->the_post();
?>
<div>
<a href="<? the_permalink()?>">
<?php the_title(); ?>
</a>
</div>
wp_reset_query();
I am creating a custom theme for woocommerce and I need to be able to create a mini product display. I am having problems finding documentation on the woocommerce api. I have a comma delimited list of product IDs that I need to iterate through and display a custom mini product display for each in sequence.
$key_values = get_post_custom_values('rel_products_ids');
//get comma delimited list from product
$rel_product_ids = explode(",", trim($key_values, ","));
// create array of just the product ids
foreach ( $rel_product_ids as $pid ) {
//sequentially get each id and do something with it
$loop = new WP_Query( array( 'post__in' => $pid ) );
// also tried ...
//$loop = new WP_Query( array( 'ID' => $pid ) );
while ( $loop->have_posts() ) : $loop->the_post(); $_product = &new WC_Product( $loop->post->ID );
//do stuff here I have stripped the html in favor of getting to the meat of the issue
woocommerce_show_product_sale_flash( $post, $_product );
if (has_post_thumbnail( $loop->post->ID )) echo get_the_post_thumbnail($loop->post->ID, 'shop_single');
get_permalink( $loop->post->ID );
the_title();
$_product->get_price_html();
endwhile;
}
Any help would be appreciated.
Thank you,
Tim
Use this method:
$_product = wc_get_product( $id );
Official API-docs: wc_get_product
Another easy way is to use the WC_Product_Factory class and then call function get_product(ID)
http://docs.woothemes.com/wc-apidocs/source-class-WC_Product_Factory.html#16-63
sample:
// assuming the list of product IDs is are stored in an array called IDs;
$_pf = new WC_Product_Factory();
foreach ($IDs as $id) {
$_product = $_pf->get_product($id);
// from here $_product will be a fully functional WC Product object,
// you can use all functions as listed in their api
}
You can then use all the function calls as listed in their api:
http://docs.woothemes.com/wc-apidocs/class-WC_Product.html
Alright, I deserve to be throttled. definitely an RTM but not for WooCommerce, for Wordpress.
Solution found due to a JOLT cola (all hail JOLT cola).
TASK:
Field named 'related_product_ids' added to a custom post type. So when that post is displayed mini product displays can be displayed with it.
PROBLEM:
Was having a problem getting the multiple ids returned via WP_Query.
SOLUTION:
$related_id_list = get_post_custom_values('related_product_ids');
// Get comma delimited list from current post
$related_product_ids = explode(",", trim($related_id_list[0],','));
// Return an array of the IDs ensure no empty array elements from extra commas
$related_product_post_ids = array( 'post_type' => 'product',
'post__in' => $related_product_ids,
'meta_query'=> array(
array( 'key' => '_visibility',
'value' => array('catalog', 'visible'),'compare' => 'IN'
)
)
);
// Query to get all product posts matching given IDs provided it is a published post
$loop = new WP_Query( $related_posts );
// Execute query
while ( $loop->have_posts() ) : $loop->the_post(); $_product = get_product( $loop->post->ID );
// Do stuff here to display your products
endwhile;
Thank you for anyone who may have spent some time on this.
Tim
global $woocommerce;
var_dump($woocommerce->customer->get_country());
foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
$product = new WC_product($cart_item['product_id']);
var_dump($product);
}