please help to complete this. I need to download order report of woocommerce in csv format, for that i made the following query:
header('Content-Type: text/csv; charset=utf-8');
header('Content-Disposition: attachment; filename=data.csv');
// create a file pointer connected to the output stream
$output = fopen('php://output', 'w');
fputcsv($output, array('ID', 'Date', 'Status','Name'));
$rows = mysql_query('SELECT ID,post_date,post_status,post_name FROM wp_posts WHERE post_date LIKE "%2016-03-30%"');
while ($row = mysql_fetch_assoc($rows)) fputcsv($output, $row);
And this is just a trail and here i only fetch data from post table.
But i need to connect to postmeta and other tables, so that i can get all information about order.
By searching on the internet i get the following code, but i don't know how to integrate with this with my code.
See the query for to get all order details:
select
p.ID as order_id,
p.post_date,
max( CASE WHEN pm.meta_key = '_billing_email' and p.ID = pm.post_id THEN pm.meta_value END ) as billing_email,
max( CASE WHEN pm.meta_key = '_billing_first_name' and p.ID = pm.post_id THEN pm.meta_value END ) as _billing_first_name,
max( CASE WHEN pm.meta_key = '_billing_last_name' and p.ID = pm.post_id THEN pm.meta_value END ) as _billing_last_name,
max( CASE WHEN pm.meta_key = '_billing_address_1' and p.ID = pm.post_id THEN pm.meta_value END ) as _billing_address_1,
max( CASE WHEN pm.meta_key = '_billing_address_2' and p.ID = pm.post_id THEN pm.meta_value END ) as _billing_address_2,
max( CASE WHEN pm.meta_key = '_billing_city' and p.ID = pm.post_id THEN pm.meta_value END ) as _billing_city,
max( CASE WHEN pm.meta_key = '_billing_state' and p.ID = pm.post_id THEN pm.meta_value END ) as _billing_state,
max( CASE WHEN pm.meta_key = '_billing_postcode' and p.ID = pm.post_id THEN pm.meta_value END ) as _billing_postcode,
max( CASE WHEN pm.meta_key = '_shipping_first_name' and p.ID = pm.post_id THEN pm.meta_value END ) as _shipping_first_name,
max( CASE WHEN pm.meta_key = '_shipping_last_name' and p.ID = pm.post_id THEN pm.meta_value END ) as _shipping_last_name,
max( CASE WHEN pm.meta_key = '_shipping_address_1' and p.ID = pm.post_id THEN pm.meta_value END ) as _shipping_address_1,
max( CASE WHEN pm.meta_key = '_shipping_address_2' and p.ID = pm.post_id THEN pm.meta_value END ) as _shipping_address_2,
max( CASE WHEN pm.meta_key = '_shipping_city' and p.ID = pm.post_id THEN pm.meta_value END ) as _shipping_city,
max( CASE WHEN pm.meta_key = '_shipping_state' and p.ID = pm.post_id THEN pm.meta_value END ) as _shipping_state,
max( CASE WHEN pm.meta_key = '_shipping_postcode' and p.ID = pm.post_id THEN pm.meta_value END ) as _shipping_postcode,
max( CASE WHEN pm.meta_key = '_order_total' and p.ID = pm.post_id THEN pm.meta_value END ) as order_total,
max( CASE WHEN pm.meta_key = '_order_tax' and p.ID = pm.post_id THEN pm.meta_value END ) as order_tax,
max( CASE WHEN pm.meta_key = '_paid_date' and p.ID = pm.post_id THEN pm.meta_value END ) as paid_date,
( select group_concat( order_item_name separator '|' ) from wp_woocommerce_order_items where order_id = p.ID ) as order_items
from
wp_posts as p,
wp_postmeta as pm
where
post_type = 'shop_order' and
p.ID = pm.post_id and
post_date BETWEEN '2015-01-01' AND '2015-07-08'
and post_status = 'wc-completed'
group by
p.ID
Please help to complete this, or is there any good method rather than this?
I don't want to use plugins.
Currently i have a plugin, but that is working slow, that's why i making this page.
Please help to solve this issue .
From my own blog post, this is the format for the SQL to extract information from an EAV style table layout.
$reportQuery = "
SELECT
A.ID as order_id
, B.meta_value as b_first_name
, C.meta_value as b_last_name
, D.meta_value as b_address_1
, E.meta_value as b_address_2
, F.meta_value as b_country
, G.meta_value as b_state
, H.meta_value as b_city
, I.meta_value as b_postcode
, J.meta_value as b_user_id
, K.user_email as b_email
FROM wp_posts as A
LEFT JOIN wp_postmeta B
ON A.id = B.post_id AND B.meta_key = '_billing_first_name'
LEFT JOIN wp_postmeta C
ON A.id = C.post_id AND C.meta_key = '_billing_last_name'
LEFT JOIN wp_postmeta D
ON A.id = D.post_id AND D.meta_key = '_billing_address_1'
LEFT JOIN wp_postmeta E
ON A.id = E.post_id AND E.meta_key = '_billing_address_2'
LEFT JOIN wp_postmeta F
ON A.id = F.post_id AND F.meta_key = '_billing_country'
LEFT JOIN wp_postmeta G
ON A.id = G.post_id AND G.meta_key = '_billing_state'
LEFT JOIN wp_postmeta H
ON A.id = H.post_id AND H.meta_key = '_billing_city'
LEFT JOIN wp_postmeta I
ON A.id = I.post_id AND I.meta_key = '_billing_postcode'
LEFT JOIN wp_postmeta J
ON A.id = J.post_id AND J.meta_key = '_customer_user'
LEFT JOIN wp_users K
ON J.meta_value = K.ID
WHERE A.post_type = 'shop_order'
AND A.post_status = 'wc-completed';
AND A.post_date_gmt >= DATE_SUB(NOW(), INTERVAL 1 DAY)
";
header('Content-Type: text/csv; charset=utf-8');
header('Content-Disposition: attachment; filename=woocommerce-'.date('Y-m-d').'.csv');
// create a file pointer connected to the output stream
$output = fopen('php://output', 'w');
$rows = mysql_query($reportQuery);
while ($row = mysql_fetch_assoc($rows)) fputcsv($output, $row);
fclose($output);
Related
The following using wpdb->prepare returns empty results while the code below not using wpdb->prepare returns the correct results. What is wrong?
global $wpdb;
$query = $wpdb->prepare("select subid, firstname, lastname from wpks_members where member_id = %d,".$mid);
$row= $wpdb->get_row($query);
$query = $wpdb->prepare("select t.subid, t.testid, t.test_date, t.puzzle_score,t.clock_score,t.match_score,t.oddone_score,(t.puzzle_score+t.clock_score+t.match_score+t.oddone_score) as tot,
if((t.puzzle_score+t.clock_score+t.match_score+t.oddone_score) < 90, 'Yes', 'No') as refer, u.ufname, u.ulname
from wpks_results t
join wpks_hasi_users u on t.userid = u.userid
where t.member_id = %d,".$mid);
$rows = $wpdb->get_results($query);
//this below works
$row= $wpdb->get_row("select subid, firstname, lastname from wpks_members where member_id = ".$mid);
$query = $wpdb->prepare();
$rows = $wpdb->get_results("select t.subid, t.testid, t.test_date, t.puzzle_score,t.clock_score,t.match_score,t.oddone_score,(t.puzzle_score+t.clock_score+t.match_score+t.oddone_score) as tot,
if((t.puzzle_score+t.clock_score+t.match_score+t.oddone_score) < 90, 'Yes', 'No') as refer, u.ufname, u.ulname
from wpks_results t
join wpks_hasi_users u on t.userid = u.userid
where t.member_id = ".$mid);
It should be:
$query = $wpdb->prepare("select subid, firstname, lastname from wpks_members where member_id = %d", $mid);
I want to display min and max price of subcategories product on parent categories pages.
function woocommerce_template_loop_subcategory_info( $category ) {
$term = get_queried_object();
global $wpdb;
# Get ALL related products prices related to a specific product category
$results = $wpdb->get_col( "
SELECT pm.meta_value
FROM {$wpdb->prefix}term_relationships as tr
INNER JOIN {$wpdb->prefix}term_taxonomy as tt ON tr.term_taxonomy_id = tt.term_taxonomy_id
INNER JOIN {$wpdb->prefix}terms as t ON tr.term_taxonomy_id = t.term_id
INNER JOIN {$wpdb->prefix}postmeta as pm ON tr.object_id = pm.post_id
WHERE tt.taxonomy LIKE 'product_cat'
AND t.term_id = {$term->term_id}
AND pm.meta_key = '_price'
");
// Sorting prices numerically
sort($results, SORT_NUMERIC);
// Get the min and max prices
$min = current($results);
$max = end($results);
// Format the price range after the title
$price_range = sprintf( __( ' <small>(from %1$s to %2$s)</small>', 'woocommerce' ), wc_price( $min ), wc_price( $max ) );
return $term_title . $price_range;
}
this is how to get the related product min and max price but i want to get subcategories price.
I need to get all items in a custom taxonomy in custom post type
here is my try:
function gat_all_terms($taxonomy){
$terms = get_terms( $taxonomy, 'orderby=count&hide_empty=0' );
$count = count($terms);
$out ='';
if ( $count > 0 ){
foreach ( $terms as $term ) {
$out .= "<li class='item'> <a href='#'>" . $term->name . "</a></li>";
}
}
return $out;
}
But i cant detect for a custom post type.
If you're trying to get the terms of a custom post type you can check out the answer here:
https://wordpress.stackexchange.com/questions/14331/get-terms-by-taxonomy-and-post-type
static public function get_terms_by_post_type( $taxonomies, $post_types ) {
global $wpdb;
$query = $wpdb->prepare(
"SELECT t.*, COUNT(*) from $wpdb->terms AS t
INNER JOIN $wpdb->term_taxonomy AS tt ON t.term_id = tt.term_id
INNER JOIN $wpdb->term_relationships AS r ON r.term_taxonomy_id = tt.term_taxonomy_id
INNER JOIN $wpdb->posts AS p ON p.ID = r.object_id
WHERE p.post_type IN('%s') AND tt.taxonomy IN('%s')
GROUP BY t.term_id",
join( "', '", $post_types ),
join( "', '", $taxonomies )
);
$results = $wpdb->get_results( $query );
return $results;
}
Which you could convert to something simpler like:
function get_terms_by_post_type( $taxonomy, $post_type ) {
global $wpdb;
$query = $wpdb->prepare(
"SELECT t.*, COUNT(*) from $wpdb->terms AS t
INNER JOIN $wpdb->term_taxonomy AS tt ON t.term_id = tt.term_id
INNER JOIN $wpdb->term_relationships AS r ON r.term_taxonomy_id = tt.term_taxonomy_id
INNER JOIN $wpdb->posts AS p ON p.ID = r.object_id
WHERE p.post_type IN('%s') AND tt.taxonomy IN('%s')
GROUP BY t.term_id",
$post_type,
$taxonomy
);
$results = $wpdb->get_results( $query );
return $results;
}
get_terms_by_post_type( 'taxonomy', 'type' );
i have posts with two advanced custom fields "name" and "surname"
and i need to query the post to have it in the right sequece.
name / surnname / title
like this:
Doe, Joen, Title
my query looks like this
$args = array(
'numberposts' => -1,
'post_type' => array ('events'),
'meta_key' => 'name',
'orderby' => 'meta_value',
'order' => 'ASC',
'meta_query'=> array(
array(
'key' => 'date',
'compare' => '<',
'value' => $today,
'type' => 'DATE',
)),
);
How can I order the posts in the right way?
i tried something like this:
'orderby' => 'meta_value surname',
but i think this dont work with custom fields
thanks!!!
++
thanks alot Mukesh!
this works (i hope so…)
SELECT SQL_CALC_FOUND_ROWS * FROM wpdev_posts
INNER JOIN wpdev_postmeta ON ( wpdev_posts.ID = post_id )
INNER JOIN wpdev_postmeta AS mt1 ON ( wpdev_posts.ID = mt1.post_id )
INNER JOIN wpdev_postmeta AS mt2 ON ( wpdev_posts.ID = mt2.post_id )
WHERE 1=1 AND ( wpdev_postmeta.meta_key = 'referent_name' ) AND ( mt1.meta_key = 'referent_vorname' )
AND ( ( mt2.meta_key = 'datum' AND CAST(mt2.meta_value AS DATE) < 20190527 ) )
AND wpdev_posts.post_type = 'veranstaltungen'
AND (wpdev_posts.post_status = 'publish'
OR wpdev_posts.post_status = 'pending' OR wpdev_posts.post_status = 'confirmed'
OR wpdev_posts.post_status = 'paid' OR wpdev_posts.post_status = 'complete'
OR wpdev_posts.post_status = 'private')
GROUP BY wpdev_posts.ID
ORDER BY wpdev_postmeta.meta_value ASC, mt1.meta_value ASC, post_title ASC
Try below code it will help to you.
<?php
global $wpdb;
$query = "SELECT SQL_CALC_FOUND_ROWS * FROM $wpdb->posts
INNER JOIN $wpdb->postmeta ON ( $wpdb->posts.ID = $wpdb->postmeta.post_id )
INNER JOIN $wpdb->postmeta AS mt1 ON ( wp_posts.ID = mt12.post_id )
INNER JOIN $wpdb->postmeta AS mt2 ON ( wp_posts.ID = mt2.post_id )
WHERE 1=1 AND ( $wpdb->postmeta.meta_key = 'name' ) AND ( mt1.meta_key = 'surname' )
AND ( ( mt2.meta_key = 'date' AND CAST(mt2.meta_value AS DATE) < $today ) ) )
AND $wpdb->posts.post_type = 'events' AND ($wpdb->posts.post_status = 'publish'
OR $wpdb->posts.post_status = 'pending' OR $wpdb->posts.post_status = 'confirmed'
OR $wpdb->posts.post_status = 'paid' OR $wpdb->posts.post_status = 'complete'
OR $wpdb->posts.post_status = 'private')
GROUP BY wp_posts.ID
ORDER BY wp_postmeta.meta_value ASC, mt1.meta_value ASC ";
$posts = $wpdb->get_results($query, object);
echo '<pre>';print_r($posts);
?>
I need to rewrite pagerquery, i have tried out several options adding tags, extend(PagerDefault) but nothing worked for me:
Please help.
My version 6 code is:
$sql = 'SELECT DISTINCT(n.nid), n.sticky, n.title, n.created FROM {node} n '.$sort_join.' WHERE n.uid = %d AND n.type = "case" AND n.status = 1 ORDER BY '. $order;
$sql_count = 'SELECT COUNT(DISTINCT(n.nid)) FROM {node} n WHERE n.uid = %d AND n.type = "case" AND n.status = 1';
$args = array('uid' => $user->uid);
$sql = db_rewrite_sql($sql);
$sql_count = db_rewrite_sql($sql_count);
if ($pager) {
$result = pager_query($sql, variable_get('default_nodes_main', 10), 0, $sql_count, $args);
dsm($result);
}
else {
$result = db_query_range($sql, $args, 0, variable_get('feed_default_items', 10));
}
$num_rows = FALSE;
while ($node = db_fetch_object($result)) {
$output .= node_view(node_load($node->nid), 1);
$num_rows = TRUE;
}
Without knowing the name of your join table this isn't complete, but should get you started:
$query = db_select('node', 'n')
->fields('n', array('nid', 'sticky', 'title', 'created'))
->condition('n.uid', $user->uid)
->condition('n.type', 'case')
->condition('n.status', 1)
->extend('PagerDefault')
->limit(variable_get('default_nodes_main', 10))
->addTag('node_access')
->orderBy('col_name');
$query->join('table_to_join', 'table_alias', 'table_alias.nid = n.nid');
foreach ($query->execute() as $row) {
// Do what you need to
}
if ($vidw == 'my_cases' or $vidw == 'my') { // including private
switch ($sort_by) {
case 'rated':
$order = 'n.sticky DESC, vc.value DESC';
$sort_join = 'LEFT OUTER JOIN {votingapi_cache} vc ON n.nid = vc.content_id AND vc.content_type = "node" AND vc.function = "average"';
break;
case 'discussed':
$order = 'n.sticky DESC, nc.comment_count DESC';
$sort_join = 'LEFT OUTER JOIN {node_comment_statistics} nc ON n.nid = nc.nid';
break;
case 'viewed':
$order = 'n.sticky DESC, nc.totalcount DESC';
$sort_join = 'LEFT OUTER JOIN {node_counter} nc ON n.nid = nc.nid';
break;
case 'recent':
default:
$order = 'n.sticky DESC, n.created DESC';
$sort_join = '';
break;
}
// from taxonomy_select_nodes
$sql = 'SELECT DISTINCT(n.nid), n.sticky, n.title, n.created FROM {node} n '.$sort_join.' WHERE n.uid = %d AND n.type = "case" AND n.status = 1 ORDER BY '. $order;
$sql_count = 'SELECT COUNT(DISTINCT(n.nid)) FROM {node} n WHERE n.uid = %d AND n.type = "case" AND n.status = 1';
$args = array('uid' => $user->uid);
$sql = db_rewrite_sql($sql);
$sql_count = db_rewrite_sql($sql_count);
if ($pager) {
$result = pager_query($sql, variable_get('default_nodes_main', 10), 0, $sql_count, $args);
}
else {
$result = db_query_range($sql, $args, 0, variable_get('feed_default_items', 10));
}
// $output .= taxonomy_render_nodes($result);
$num_rows = FALSE;
while ($node = db_fetch_object($result)) {
$output .= node_view(node_load($node->nid), 1);
$num_rows = TRUE;dsm($output);
}
if ($num_rows) {
$output .= theme('pager', NULL, variable_get('default_nodes_main', 10), 0);
}
else {
$output .= '<p>'. t('There are currently no visible cases in this category.') .'</p>';
}
}
In above drupal 6 code, i have updated the query which gives me result but node_view() and theme() does not work.