admin管理员组文章数量:1300023
I am developing a function that gives me the results of sales on a product in a certain period of time, in order to correctly fill in the data to send to Google Merchant. So I created a mysql query that works from phpmyadmin and I transformed it into php. But while in mysql it works perfectly in php it always gives me a result equal to an empty string.
I need to convert this mysql to php
SELECT COUNT(product_id)
FROM wp_wc_order_product_lookup
WHERE product_id = 4098995
AND date_created < '2025-02-11'
AND date_created > '2017-02-11'
limit 50
this is my php
$sql = $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(product_id) FROM $wpdb->wp_wc_order_product_lookup WHERE product_id = 4098995 AND date_created < '2025-02-11' AND date_created > '2017-02-11' limit 50"),$product_id,$date_from,$date_to );
but I don't know why it doesn't give me the results, while if I use phpmyadmin I have 28 as a count
this is the complete function
global $product;
global $wpdb;
$sku = $sku.$sex;
$product_id = $wpdb->get_var( $wpdb->prepare( "SELECT post_id FROM $wpdb->postmeta WHERE meta_key='_sku' AND meta_value='%s' LIMIT 1", $sku ) );
$date_to = date("Y-m-d", strtotime('-96 months'));
$date_from = date("Y-m-d");
$product_id = 4098995;
$tot = get_post_meta($product_id,'total_sales',true);
echo "vendite totali ". $tot . " id prodotto ". $product_id. " dalla data ". $date_from . " fino alla data ".$date_to;
$sql = $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(product_id) FROM $wpdb->wp_wc_order_product_lookup WHERE product_id = 4098995 AND date_created < '2025-02-11' AND date_created > '2017-02-11' limit 50"),$product_id,$date_from,$date_to );
echo " TEST SQL " . $sql;
I am developing a function that gives me the results of sales on a product in a certain period of time, in order to correctly fill in the data to send to Google Merchant. So I created a mysql query that works from phpmyadmin and I transformed it into php. But while in mysql it works perfectly in php it always gives me a result equal to an empty string.
I need to convert this mysql to php
SELECT COUNT(product_id)
FROM wp_wc_order_product_lookup
WHERE product_id = 4098995
AND date_created < '2025-02-11'
AND date_created > '2017-02-11'
limit 50
this is my php
$sql = $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(product_id) FROM $wpdb->wp_wc_order_product_lookup WHERE product_id = 4098995 AND date_created < '2025-02-11' AND date_created > '2017-02-11' limit 50"),$product_id,$date_from,$date_to );
but I don't know why it doesn't give me the results, while if I use phpmyadmin I have 28 as a count
this is the complete function
global $product;
global $wpdb;
$sku = $sku.$sex;
$product_id = $wpdb->get_var( $wpdb->prepare( "SELECT post_id FROM $wpdb->postmeta WHERE meta_key='_sku' AND meta_value='%s' LIMIT 1", $sku ) );
$date_to = date("Y-m-d", strtotime('-96 months'));
$date_from = date("Y-m-d");
$product_id = 4098995;
$tot = get_post_meta($product_id,'total_sales',true);
echo "vendite totali ". $tot . " id prodotto ". $product_id. " dalla data ". $date_from . " fino alla data ".$date_to;
$sql = $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(product_id) FROM $wpdb->wp_wc_order_product_lookup WHERE product_id = 4098995 AND date_created < '2025-02-11' AND date_created > '2017-02-11' limit 50"),$product_id,$date_from,$date_to );
echo " TEST SQL " . $sql;
Share
Improve this question
edited Feb 11 at 15:24
LoicTheAztec
254k24 gold badges396 silver badges443 bronze badges
asked Feb 11 at 14:43
Vito SantimoneVito Santimone
558 bronze badges
5
|
1 Answer
Reset to default 0There are some mistakes in your code attempt, to get the sales count for a product from a specific date:
- You need to replace "wp_" prefix from the table string name with
{$wpdb->prefix}
, - The WPDB
prepare()
method, requires placeholders, for each variable, - With WPDB
prepare()
method, each comma separated variable is inside, not outside, - The "date to" is not needed in your query,
- The
limit
argument is not needed as you are trying to get the count.
See WPDB() WordPress Class Documentation.
Try the following instead (with a "from" date):
global $wpdb;
$product_id = 4098995;
$count = $wpdb->get_var( $wpdb->prepare( "
SELECT COUNT(product_id)
FROM {$wpdb->prefix}wc_order_product_lookup
WHERE product_id = %d
AND date_created > '%s';
", $product_id, date("Y-m-d", strtotime('-8 years') ) ) );
It should work.
Using a date range
With a date range you can use the BETWEEN
operator instead like:
global $wpdb;
$product_id = 4098995;
$from_date = date("Y-m-d", strtotime('-8 years') );
$to_date = date("Y-m-d", strtotime('-1 years') );
$count = $wpdb->get_var( $wpdb->prepare( "
SELECT COUNT(product_id)
FROM {$wpdb->prefix}wc_order_product_lookup
WHERE product_id = %d
AND date_created BETWEEN '%s' AND '%s';
", $product_id, $from_date, $to_date ) );
本文标签: phpGet the sales count of a WooCommerce product for a period of timeStack Overflow
版权声明:本文标题:php - Get the sales count of a WooCommerce product for a period of time - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741656127a2390770.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
{$wpdb->prefix}wc_order_product_lookup
instead of$wpdb->wp_wc_order_product_lookup
ensuring that you declared beforeglobal $wpdb;
. – LoicTheAztec Commented Feb 11 at 14:47