在WooCommerce中用短代码显示日期之间售出的商品数量 [英] Display number of items sold between dates with a shortcode in WooCommerce
问题描述
我们是一家二手公司,我们努力证明所售产品对环境的影响.
We are a secondhand company and we try hard to show the effect that the products sold have on the environment.
为此,我们要在首页上显示特定日期之间售出的产品数量,然后将其乘以一个常数因子以显示影响(例如说节省的水量).
For that, we want to show on the homepage the number of products sold between certain dates and multiply it by a constant factor to show the impact (for example, say water liters saved).
我们将举行一次大型的销售活动,并计划在首页上实时显示该活动,但是到目前为止,我一直无法找到一种方法来显示首页上两个日期之间的已售产品数量.
We'll have a big sales event and we plan to show that in real time on the homepage, but so far I've been unable to find a way to show the number of products sold between the dates on the homepage.
如何打印在某些日期之间售出的商品数量?
我知道后端有一个报告,但是我想通过自定义简码在前端显示它.
I know there's a report in the backend, but I want to show this on the frontend via custom shortcode.
推荐答案
您可以使用以下简短代码对 wp_wc_order_stats
数据库表进行非常轻巧的SQL查询,以获取日期之间的已售商品数量:
You can use the following shortcode that makes a very light SQL query on wp_wc_order_stats
database table to get the number of items sold between dates:
// Requires at least WooCommerce version 4
add_shortcode( 'num_items_sold', 'display_num_items_sold' );
function display_num_items_sold( $atts ) {
// Set the defaullt timezone (see: https://www.php.net/manual/en/timezones.php)
date_default_timezone_set( 'Europe/Paris' );
$now = date( 'Y-m-d H:i:s', strtotime("now") ); // get now formated datetime
// Shortcode attribute (or argument)
extract( shortcode_atts( array(
'from' => '', // Date from (is required)
'to' => $now, // Date to (optional: default is "now" value)
), $atts, 'num_items_sold' ) );
// Formating dates
$date_from = date('Y-m-d H:i:s', strtotime($from) );
$date_to = date('Y-m-d H:i:s', strtotime($to) );
if ( ! empty($date_from) ) {
global $wpdb;
// Custom very light SQL query
$num_items_sold = $wpdb->get_var( $wpdb->prepare("
SELECT SUM(num_items_sold)
FROM {$wpdb->prefix}wc_order_stats
WHERE date_created >= '%s'
AND date_created <= '%s'
", $date_from, $date_to ) );
return '<span class="num_items_sold">' . __("Items sold", "woocommerce") . ': ' . $num_items_sold . '</span>';
}
}
代码进入活动子主题(或活动主题)的functions.php文件中.经过测试并可以在WooCommerce 4+上使用.
Code goes in functions.php file of the active child theme (or active theme). Tested and works on WooCommerce 4+.
WooCommerce版本3.x.x改为使用以下内容(在woocommerce 4以下):
WooCommerce version 3.x.x use the following instead (below woocommerce 4):
add_shortcode( 'num_items_sold', 'display_num_items_sold' );
function display_num_items_sold( $atts ) {
// Set the defaullt timezone (see: https://www.php.net/manual/en/timezones.php)
date_default_timezone_set( 'Europe/Paris' );
$now = date( 'Y-m-d H:i:s', strtotime("now") ); // get now formated datetime
// Shortcode attribute (or argument)
extract( shortcode_atts( array(
'from' => '', // Date from (is required)
'to' => $now, // Date to (optional: default is "now" value)
), $atts, 'num_items_sold' ) );
// Formating dates
$date_from = date('Y-m-d H:i:s', strtotime($from) );
$date_to = date('Y-m-d H:i:s', strtotime($to) );
if ( ! empty($date_from) ) {
global $wpdb;
// Custom SQL query
$num_items_sold = $wpdb->get_var( $wpdb->prepare("
SELECT SUM(oim.meta_value)
FROM {$wpdb->prefix}woocommerce_order_itemmeta as oim
INNER JOIN {$wpdb->prefix}woocommerce_order_items as oi
ON oim.order_item_id = oi.order_item_id
INNER JOIN {$wpdb->prefix}posts as o
ON oi.order_id = o.ID
WHERE oim.meta_key = '_qty'
AND o.post_status IN ('wc-processing','wc-completed')
AND o.post_date >= '%s'
AND o.post_date <= '%s'
", $date_from, $date_to ) );
return '<span class="num_items_sold">' . __("Items sold", "woocommerce") . ': ' . $num_items_sold . '</span>';
}
}
代码进入活动子主题(或活动主题)的functions.php文件中.经过测试,可以正常工作.
Code goes in functions.php file of the active child theme (or active theme). Tested and works.
用法示例:
- 具有唯一的日期来自"(直到现在"):
[num_items_sold from =''"2019-05-02"]
- 带有来自"日期的日期以及日期至":
[num_items_sold from ="2020-05-02"to ="2020-08-17"]
这篇关于在WooCommerce中用短代码显示日期之间售出的商品数量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!