• Resolved cupcakes99

    (@cupcakes99)


    In a theme template, how can we check if the current user has a coupon or store credit available to them? I can’t find any developer docs.

Viewing 2 replies - 1 through 2 (of 2 total)
  • Plugin Support karlaviado

    (@karlaviado)

    hi @cupcakes99

    To check if there is a store credit available for the current user, you may check out this snippet

    function get_user_store_credits($user_id) {
    // Replace 'store_credits' with the actual meta key used by your store credit system
    $credits = get_user_meta($user_id, 'acfw_store_credit_balance', true);
    return $credits ? $credits : 0;
    }

    // Example usage:
    $user_id = get_current_user_id(); // or any specific user ID
    echo 'Store Credits: ' . get_user_store_credits($user_id);

    For you inquiry in checking coupons is connected to the user, this one is a bit tricky. In WooCommerce, the coupon is not directly connected to the users, but rather in products/cart. You may only check if the coupon is connected to the users if they have already used the coupon from previous orders.

    You may still want to try it out using this code:

    function is_coupon_used_by_user($coupon_code, $user_id) {
    // Get all orders of the user
    $orders = wc_get_orders(array(
    'customer' => $user_id,
    'status' => array('wc-completed', 'wc-processing') // Modify statuses as needed
    ));

    foreach ($orders as $order) {
    // Check if the coupon is used in the order
    $used_coupons = $order->get_used_coupons();
    if (in_array($coupon_code, $used_coupons)) {
    return true;
    }
    }

    return false;
    }

    // Example usage:
    $coupon_code = 'COUPON_CODE'; // Replace with your coupon code
    $user_id = get_current_user_id(); // Replace with specific user ID
    if (is_coupon_used_by_user($coupon_code, $user_id)) {
    echo 'The coupon is used to this user.';
    } else {
    echo 'The coupon is not used to this user.';
    }

    I hope this helps.

    Cheers!

    Plugin Support Fauzan Azizie

    (@fauzanade)

    Duplicated answer above

    • This reply was modified 3 months ago by Fauzan Azizie. Reason: Duplicated answer above
Viewing 2 replies - 1 through 2 (of 2 total)
  • You must be logged in to reply to this topic.