gpt4 book ai didi

php - WooCommerce 在前端自定义日期后显示产品总销售额

转载 作者:行者123 更新时间:2023-11-29 04:35:17 24 4
gpt4 key购买 nike

我想显示自定义日期后的产品总销售额:

我有此代码,但我想将其自定义为仅显示自定义日期之后的总销售额,例如 05/26/2016

function total_sales()
{
global $product;
$units_sold = get_post_meta(206, 'total_sales', true);
echo '<p>' . sprintf(__('Units Sold: %s', 'woocommerce'), $units_sold) . '</p>';
}

最佳答案

total_sales holds the total sales count of all time, So you need to write custom query to get sold count between specific date.

//woocommerce get total sale count of a product after a specific data
function wh_get_total_sold_by_product_id($date_from, $product_id)
{
global $wpdb;
$date_to = date('Y-m-d');

$sql = "
SELECT COUNT(*) AS sale_count
FROM {$wpdb->prefix}woocommerce_order_items AS order_items
INNER JOIN {$wpdb->prefix}woocommerce_order_itemmeta AS order_meta ON order_items.order_item_id = order_meta.order_item_id
INNER JOIN {$wpdb->posts} AS posts ON order_meta.meta_value = posts.ID
WHERE order_items.order_item_type = 'line_item'
AND order_meta.meta_key = '_product_id'
AND order_meta.meta_value = %d
AND order_items.order_id IN (
SELECT posts.ID AS post_id
FROM {$wpdb->posts} AS posts
WHERE posts.post_type = 'shop_order'
AND posts.post_status IN ('wc-completed','wc-processing')
AND DATE(posts.post_date) BETWEEN %s AND %s
)
GROUP BY order_meta.meta_value";

return $wpdb->get_var($wpdb->prepare($sql, $product_id, $date_from, $date_to));
}

代码进入 functions.php您活跃的子主题(或主题)的文件。或者也可以在任何插件 php 文件中。

用法

//$count = wh_get_total_sold_by_product_id($my_custom_date, $product_id);
$count = wh_get_total_sold_by_product_id('2016-05-26', 56);

请注意:所有日期均采用 Y-m-d 格式。

代码已经过测试并且可以工作。

希望这对您有所帮助!

关于php - WooCommerce 在前端自定义日期后显示产品总销售额,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44183001/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com