1 回答
TA贡献1784条经验 获得超2个赞
将显示以下简码
特定优惠券已使用多少次
每个用户的使用限制
基于当前登录用户的用户 ID。
// Function that runs when shortcode is called
function users_coupon_usage_shortcode() {
// Only for logged-in users
if ( ! is_user_logged_in() ) return;
global $wpdb;
// Coupon code
$coupon_code = 'test';
// Retrieving the coupon ID
$coupon_post_obj = get_page_by_title( $coupon_code, OBJECT, 'shop_coupon' );
$coupon_id = $coupon_post_obj->ID;
// Get the WC_Coupon Object
$coupon_data = new WC_Coupon( $coupon_id );
// Get current user id
$user_id = get_current_user_id();
// Usage limit per user
$usage_limit_per_user = $coupon_data->get_usage_limit_per_user();
// Get an array of used by count based on post_id, meta_key and meta_value (user_id)
$used_by_count = $wpdb->get_var("SELECT COUNT(*) FROM $wpdb->postmeta WHERE post_id = '$coupon_id' AND meta_key = '_used_by' AND meta_value = '$user_id'");
// Output
$output = sprintf( __( 'The coupon %s has already been used %s times out of %s', 'woocommerce' ), '"<strong>' . $coupon_code . '</strong>"', $used_by_count, $usage_limit_per_user );
// Output needs to be return
return $output;
}
// Register shortcode
add_shortcode( 'users_coupon_usage', 'users_coupon_usage_shortcode' );
- 1 回答
- 0 关注
- 111 浏览
添加回答
举报