我在主题中使用woocommerce插件。我想要一个功能,我需要检查客户订单的总和。在此基础上,我将为他们提供优惠券折扣。优惠券部分很清楚。但我无法获得用户所有订单的总和。
我的计划是:如果用户购买的金额超过300美元,我们会向他提供优惠券。为此,我正在使用此动作。但是用户对订单总和的查询表单数据库有困难。
function so_27969258_track_orders_per_customer($order_id){
$order = new WC_Order( $order_id );
$myuser_id = (int)$order->user_id;
$user_info = get_userdata($myuser_id);
$items = $order->get_items();
foreach ($items as $item) {
}
return $order_id;
}
add_action( 'woocommerce_payment_complete', 'so_27969258_track_orders_per_customer' );
我也试图通过我在woocommerce / myaccount文件夹中的my-orders.php获得的用户ID来获取订单。我试图在function.php中运行此代码,但返回空数组。
$customer_orders = get_posts( apply_filters( 'woocommerce_my_account_my_orders_query', array(
'numberposts' => $order_count,
'meta_key' => '_customer_user',
'meta_value' => get_current_user_id(),
'post_type' => wc_get_order_types( 'view-orders' ),
'post_status' => array_keys( wc_get_order_statuses() )
) ) );
var_dump($customer_orders);
if ( $customer_orders ) :
foreach ( $customer_orders as $customer_order ) {
$order = wc_get_order( $customer_order );
$order->populate( $customer_order );
$item_count = $order->get_item_count();
echo $order->get_order_number();
echo wc_get_order_status_name( $order->get_status() );
echo sprintf( _n( '%s for %s item', '%s for %s items', $item_count, 'woocommerce' ), $order->get_formatted_order_total(), $item_count );
}
endif;
任何人都可以帮我解释这段代码在function.php中是如何工作的。我知道很多东西都丢失了。请建议。
答案 0 :(得分:3)
您可以使用以下函数获取用户已完成订单的总和
public function get_customer_total_order() {
$customer_orders = get_posts( array(
'numberposts' => - 1,
'meta_key' => '_customer_user',
'meta_value' => get_current_user_id(),
'post_type' => array( 'shop_order' ),
'post_status' => array( 'wc-completed' )
) );
$total = 0;
foreach ( $customer_orders as $customer_order ) {
$order = wc_get_order( $customer_order );
$total += $order->get_total();
}
return $total;
}
但是当wp完全加载时必须调用此函数,因此如果你在动作woocommerce_payment_complete
上使用此函数,它将起作用
希望有所帮助
答案 1 :(得分:0)
在最近的插件中,我们使用此功能列出前五名客户的订单数量,他们花费的总金额可能对您有用。
global $wpdb;
$top_customer_query = "";
$top_customer_query .= "SELECT SUM(wc_postmeta1.meta_value) AS 'Total'
,wc_postmeta2.meta_value AS 'BillingEmail'
,CONCAT(wc_postmeta3.meta_value, ' ',wc_postmeta5.meta_value) AS
BillingName
,Count(wc_postmeta2.meta_value) AS 'OrderCount' FROM ";
$top_customer_query .= "{$wpdb->prefix}posts as wc_posts
LEFT JOIN {$wpdb->prefix}postmeta as wc_postmeta1 ON
wc_postmeta1.post_id=wc_posts.ID
LEFT JOIN {$wpdb->prefix}postmeta as wc_postmeta2 ON
wc_postmeta2.post_id=wc_posts.ID
LEFT JOIN {$wpdb->prefix}postmeta as wc_postmeta3 ON
wc_postmeta3.post_id=wc_posts.ID
LEFT JOIN {$wpdb->prefix}postmeta as wc_postmeta5 ON
wc_postmeta5.post_id=wc_posts.ID";
$top_customer_query .= " WHERE wc_posts.post_type='shop_order'
AND wc_postmeta1.meta_key='_order_total'
AND wc_postmeta2.meta_key='_billing_email'
AND wc_postmeta3.meta_key='_billing_first_name'
AND wc_postmeta5.meta_key='_billing_last_name'";
$top_customer_query .= " AND wc_posts.post_status IN ('wc-completed')";
$top_customer_query .= " GROUP BY wc_postmeta2.meta_value";
$top_customer_query .= " Order By Total DESC";
$top_customer_query .= " LIMIT 0,5";
$top_5_customers = $wpdb->get_results($top_customer_query, OBJECT);
foreach ($top_5_customers as $customer) {
?>
<tr>
<td>
<?php echo $customer->BillingName; ?>
答案 2 :(得分:0)
WC API有一种获取订单的方法,您可以在其中添加various $args。这是我正在使用的功能:
function get_user_orders_total($user_id) {
// Use other args to filter more
$args = array(
'customer_id' => $user_id
);
// call WC API
$orders = wc_get_orders($args);
if (empty($orders) || !is_array($orders)) {
return false;
}
// One implementation of how to sum up all the totals
$total = array_reduce($orders, function ($carry, $order) {
$carry += (float)$order->get_total();
return $carry;
}, 0.0);
return $total;
}