我只想在每个产品页面的弹出窗口中显示今天的产品销售数据。
例如“今天卖出10件”
我找到了此代码示例-WooCommerce Show Product Total Sales after Custom a Date in Frontend
但是我不确定如何使用它,将其添加到functions.php文件时会收到错误消息。
Uncaught ArgumentCountError: Too few arguments to function wh_get_total_sold_by_product_id(), 1 passed in /app/public/wp-includes/class-wp-hook.php on line 286
这是原始代码
add_action( 'woocommerce_after_add_to_cart_form', 'wh_get_total_sold_by_product_id', 18 );
//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));
}