例如,如果客户以前的购物记录超过50k,那么他可以获得50%的折扣。
这是我的代码,用于检索客户的当前订单价格,但我想检索总订单价格:
add_action( 'woocommerce_before_cart', 'apply_matched_coupons' );
function apply_matched_coupons() {
global $woocommerce;
$coupon_code = '10percent'; // your coupon code here
if ( $woocommerce->cart->has_discount( $coupon_code ) ) return;
if ( $woocommerce->cart->cart_contents_total >= 500 ) {
$woocommerce->cart->add_discount( $coupon_code );
$woocommerce->show_messages();
}
}
答案 0 :(得分:1)
您可以使用wc_get_customer_total_spent()
专用的Woocommerce功能,但此功能会将所有订单付费状态(其中"处理""已完成")
要获得所有客户购买的总和(仅用于"已完成"订单状态),您可以使用基于wc_get_customer_total_spent()
similar source code的简单且轻量级的SQL来完成这一操作:
global $wpdb;
$user_id = get_current_user_id(); // Current user ID
$user_purchases_total_sum = $wpdb->get_var( "
SELECT SUM(pm.meta_value) FROM {$wpdb->prefix}postmeta as pm
INNER JOIN {$wpdb->prefix}posts as p ON pm.post_id = p.ID
INNER JOIN {$wpdb->prefix}postmeta as pm2 ON pm.post_id = pm2.post_id
WHERE p.post_status LIKE 'wc-completed' AND p.post_type LIKE 'shop_order'
AND pm.meta_key LIKE '_order_total' AND pm2.meta_key LIKE '_customer_user'
AND pm2.meta_value LIKE $user_id
" );
经过测试和工作。
答案 1 :(得分:1)
$ user_id = get_current_user_id();
$ string = wc_get_customer_total_spent($ user_id);
echo $ string;