Skip to content

Instantly share code, notes, and snippets.

@rayflores
Last active August 29, 2015 14:16
Show Gist options
  • Save rayflores/8c211392076279beba8c to your computer and use it in GitHub Desktop.
Save rayflores/8c211392076279beba8c to your computer and use it in GitHub Desktop.
need for API
<?php
* 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 get_all_user_orders($user_id,$status='completed'){
if(!$user_id)
return false;
$orders=array();//order ids
$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
$orders=wp_list_pluck( $posts, 'ID' );
return $orders;
}
/**
* Get all Products Successfully Ordered by the user
*
* @global type $wpdb
* @param int $user_id
* @return bool|array false if no products otherwise array of product ids
*/
function get_all_products_ordered_by_user($user_id=false,$status='completed'){
$orders=get_all_user_orders($user_id,$status);
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 which was successfull
//we need to find the products in these order and make sure they are downloadable
// find all products in these order
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'));
if ($products) return "O";
return "N";
// return $products;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment