We are building a simple dashboard status plugin that will show the total "Active" subscriptions for a specific product.
I'm using the following function to grab the total active subscriptions, but the numbers aren't lining up due to switched subscriptions. How do I account for the switch subscriptions?
function get_active_subscriptions_count( $product_id) {
global $wpdb;
// return the active subscriptions for a defined product ID
return $wpdb->get_var("
SELECT COUNT(p.ID)
FROM {$wpdb->prefix}posts as p
LEFT JOIN {$wpdb->prefix}posts AS p2 ON p.post_parent = p2.ID
LEFT JOIN {$wpdb->prefix}postmeta AS pm ON p2.ID = pm.post_id
LEFT JOIN {$wpdb->prefix}woocommerce_order_items AS woi ON pm.post_id = woi.order_id
LEFT JOIN {$wpdb->prefix}woocommerce_order_itemmeta AS woim ON woi.order_item_id = woim.order_item_id
WHERE p.post_type LIKE 'shop_subscription'
AND p2.post_type LIKE 'shop_order' AND woi.order_item_type LIKE 'line_item'
AND pm.meta_key LIKE '_customer_user'
AND woim.meta_key = '_product_id'
AND woim.meta_value = '$product_id'
AND p.post_status LIKE 'wc-active'
");
}
What can I do to account for the switch subscriptions? What am I missing in the query?
Related
I want to limit each user to register only one order per day.
To do this, I need to check each user's order before registering whether this user has any 'wp-processing' order or not?
I wrote below MySQL query in PHP, but it doesn't work.
I printed value of '$sql_count_from_status' and try it in phpmyadmin, SQL tab to find the error.
Value of the variable was:
SELECT count(ID) FROM wp_posts p LEFT JOIN wp_postmeta m ON p.ID=m.post_id WHERE p.post_status LIKE 'wc-processing' AND p.post_type LIKE 'shop_order' AND m.meta_key LIKE '_customer_user' AND m.meta_value LIKE '100577';
and showed me zero in phpmyadmin as result. Now I know the query has problem, because I had 'processing order' for user 100577.
function get_orders_count_from_status( $status , $this_user_id){
global $wpdb;
// We add 'wc-' prefix when is missing from order staus
$status = 'wc-' . str_replace('wc-', '', $status);
$sql_count_from_status = "
SELECT count(ID) FROM {$wpdb->prefix}posts p
LEFT JOIN {$wpdb->prefix}postmeta m
ON p.ID=m.post_id
WHERE p.post_status LIKE '$status' AND p.post_type LIKE 'shop_order'
AND m.meta_key LIKE '_customer_user'
AND m.meta_value LIKE '".$this_user_id."';";
return $wpdb->get_var($sql_count_from_status);
}
Here's an example of a plausible query (although counting on a LEFT JOIN is a bit strange):
SELECT count(*)
FROM {$wpdb->prefix}posts p
LEFT
JOIN {$wpdb->prefix}postmeta m
ON m.post_id = p.ID
AND m.meta_key = '_customer_user'
AND m.meta_value = :this_user_id
WHERE p.post_status LIKE :status
AND p.post_type = 'shop_order'
I am working on freelancing site on Wordpress, fetching data from post type 'projects' and when user select the projects then project bidder will show just have to update the bid form the freelancer (bidders)
$query_update=($wpdb->prepare("
update wp_postmeta pm
join wp_posts p
on p.id = pm.post_id
join wp_users u
on u.id = p.post_author
set pm.meta_value = '$bid'
where pm.meta_key = 'bid_budget'
and p.post_title = '$project'
and u.user_login = '$user'
"));
if($query_update > 0){
echo "Successfully Updated";
}
else{
echo "Error ! Wrong Query";
echo 'Project '.$project.'<br>';
echo 'Bid '.$bid.'<br>';
echo 'User '.$user;
}
The data in the mysql backend is not changing. Is there something wrong with my query?
Thanks I got the solution I am using the wrong query - the right one is as follows:
$query_update=$wpdb->query($wpdb->prepare("update wp_postmeta pm inner join wp_posts p on p.id = pm.post_id inner join wp_users u on u.id=p.post_author set pm.meta_value = '$bid' where pm.meta_key = 'bid_budget' and p.post_title='$project' and u.user_login='$user'"));
so I have this SQL query:
SELECT
p.ID
FROM
`cdlr_posts` p,
cdlr_postmeta pm
WHERE
pm.post_id=p.ID AND
`post_type` = 'shop_order' AND
pm.meta_key = '_statusCDLR' AND
pm.meta_value <> 1
group by
p.ID
What I need is to show all the IDS if they match with those conditions, but I will also like to show the ones that do not contain the "_statusCDLR" meta_key I tried something like this with no luck:
WHERE
pm.post_id=p.ID AND
`post_type` = 'shop_order' AND
(pm.meta_key = '_statusCDLR' AND pm.meta_value <> 1 OR pm.meta_key <> '_statusCDLR')
group by
Any help will be appreciated to achieve what I need.
I understand that your requirement is to select the id of posts that :
either have a corresponding record in cdlr_post_meta with meta_key = 'statusCDLR' and meta_value <> 1
or do not have a record cdlr_post_meta with meta_key = 'statusCDLR'
A strategy to achieve this is to use a LEFT JOIN to search for a record in cdlr_post_meta with meta_key = 'statusCDLR', and then implement the rest of the logic in the WHERE clause (if there is no corresponding record, the columns of pm are all NULL).
SELECT p.ID
FROM cdlr_posts p
LEFT JOIN cdlr_postmeta pm
ON pm.post_id = p.ID AND pm.meta_key = '_statusCDLR'
WHERE
p.post_type = 'shop_order'
AND ( pm.post_id IS NULL OR pm.meta_value <> 1 )
GROUP BY p.ID
PS - General remarks regarding your sql :
When mixing ORs and ANDs, you need to surround the test expressions within parentheses to avoid running into prescedence issues (AND has higher precedence than OR).
you should use explicit JOINs instead of implicit ones.
I'm developing a complementary plugin for woocommerce.
I have a sql request that gets all the order and customer info, but i need to get the language from the order.
How can i detect the language was using a customer when he made an order? Is it registered somewhere?
In other CMS like prestashop it's stored as id_lang in orders and customer tables.
Without getting into which plugin you will chose and how it operates, here is how you would save some extra data to the order.
// save the extra field when checkout is processed
function kia_save_extra_checkout_fields( $order_id, $posted ){
$language = detect_language_with_your_plugin_of_choice() ? detect_language_with_your_plugin_of_choice() : "en";
update_post_meta( $order_id, '_order_language', $language );
}
add_action( 'woocommerce_checkout_update_order_meta', 'kia_save_extra_checkout_fields', 10, 2 );
And because I had an awful time the one time I tried to use WPML, maybe consider checking out Multilingual Press.
Finally solved, both solutions are OK.
With WPML plugin you can get the value in the table postmeta with the meta_key = wpml_language
Just added a left join in my SQL query:
SELECT O.ID as id_order, O.post_date_gmt as date, M.meta_value as email, M2.meta_value as firstname, M3.meta_value as lastname, M4.meta_value as iso_code
FROM ".$prefix."posts O
LEFT JOIN ".$prefix."postmeta M ON M.post_id = O.ID AND M.meta_key = '_billing_email'
LEFT JOIN ".$prefix."postmeta M2 ON M2.post_id = O.ID AND M2.meta_key = '_billing_first_name'
LEFT JOIN ".$prefix."postmeta M3 ON M3.post_id = O.ID AND M3.meta_key = '_billing_last_name'
LEFT JOIN ".$prefix."postmeta M4 ON M3.post_id = O.ID AND M4.meta_key = 'wpml_language'
WHERE O.post_type = 'shop_order' AND O.post_status = 'wc-completed'
I'm trying to calculate the Yield based on Wordpress meta_values and I'm using the MySQL statement below, which is not working. The SUM(returned.meta_value+staked.meta_value)-SUM(staked.meta_value))/SUM(staked.meta_value))*100) doesn't return any values.
Can someone help me with what's wrong with the expression below.
Thank you.
global $post;
$post_author = $post->post_author;
$post_status = 'publish';
$yield = $wpdb->get_var( $wpdb->prepare(
"
SELECT (ROUND((((SUM(returned.meta_value+staked.meta_value)-SUM(staked.meta_value))/SUM(staked.meta_value))*100),2),2)
FROM {$wpdb->posts} p
JOIN {$wpdb->users} u ON p.post_author = u.ID
LEFT JOIN {$wpdb->postmeta} staked
ON p.ID = staked.post_id AND staked.meta_key = 'staked'
LEFT JOIN {$wpdb->postmeta} returned
ON p.ID = returned.post_id AND returned.meta_key = 'returned'
WHERE p.post_author = %s AND p.post_status = %s
",
$post_author, $post_status
) );
Your query doesn't make sense. The select statement is using the aliases balance and bet. The from clause has the aliases pm and posts.
Do you mean this?
SELECT (ROUND((((SUM(pm.meta_value+pm.meta_value)-SUM(pm.meta_value))/SUM(pm.meta_value))*100),2),2)
FROM {$wpdb->postmeta} pm INNER JOIN
{$wpdb->posts} p
ON pm.post_id = p.ID
WHERE pm.meta_key = %s AND pm.meta_key1 = %s AND p.post_author = %s AND
Month(p.post_date) = MONTH(CURRENT_DATE) AND p.post_status = %s