亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何輸出當前登錄的 WooCommerce 用戶優惠券使用情況?

如何輸出當前登錄的 WooCommerce 用戶優惠券使用情況?

PHP
守著星空守著你 2023-08-19 16:47:59
我想為當前登錄的用戶輸出他們使用特定優惠券代碼的次數。優惠券代碼“ example_coupon ”的最大“每次用戶使用限制r”為12。我想在前端向用戶顯示他們使用該優惠券的次數,例如“優惠券使用次數:3 ”我運行了下面的代碼,但是,它只顯示所有用戶的優惠券使用總量:function simple_function_1() {        $coupon_code = 'example_coupon';    global $woocommerce;    $coupon_data = new WC_Coupon($coupon_code);    echo ($coupon_data->usage_limit - $coupon_data->usage_count);// return number of remaining coupons}add_shortcode( 'own_shortcode1', 'simple_function_1' );希望這一切都有道理。
查看完整描述

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' );


查看完整回答
反對 回復 2023-08-19
  • 1 回答
  • 0 關注
  • 140 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號