3

I want to display the remaining number of the coupon on the WooCommerce thankyou page

For example: You used the test coupon 3 times and there are 10 more left.

This is my code:

add_action('woocommerce_after_cart_table', 'coupon_count');
function coupon_count() {
    global $woocommerce;

    if ( ! empty( $woocommerce->cart->applied_coupons ) ) {
         $my_coupon = $woocommerce->cart->get_coupons() ;
         foreach($my_coupon as $coupon){

            if ( $post = get_post( $coupon->id ) ) {

                        $counter = $coupon->get_usage_count();
                        echo "<span class='name-coupon'><b>Total usage for coupon </b><b>'</b><b>".$coupon->code."</b><b>'</b><b>: </b></span>";
                        echo "<span class='coupon-counter'>".($counter)."</span>";

            }
        }
    }
}

But I have two problems:

1.With this code, only the number of times used is displayed and the remaining number is not displayed.

2.Replacing woocommerce_after_cart_table with woocommerce_thankyou on the thankyou page does not execute the code.

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
Sinapars
  • 33
  • 4

2 Answers2

3
  • Instead of using the cart object, the order object is used on the thankyou page. So we're going to use that object instead

  • You can use get_usage_limit_per_user() to get coupon usage limit per customer (for a single customer)

  • OR use get_usage_limit() to get coupon usage limit.

So you get:

function action_woocommerce_thankyou( $order_id ) {
    // Get $order object
    $order = wc_get_order( $order_id );

    foreach( $order->get_coupon_codes() as $coupon_code ) {
        // Get the WC_Coupon object
        $coupon = new WC_Coupon( $coupon_code );

        // Get usage count
        $count = $coupon->get_usage_count();
        
        // Get coupon usage limit per customer
        $limit = $coupon->get_usage_limit_per_user();
        
        // OR use this instead, to get coupon usage limit.
        // $limit = $coupon->get_usage_limit();
        
        // NOT empty
        if ( ! empty ( $count ) && ! empty ( $limit ) ) {
            // Calculate remaining
            $remaining = $limit - $count;
        
            // Output
            echo sprintf( '<span class="coupon-class">You used the <strong>%s</strong> coupon <strong>%d</strong> times and there are <strong>%d</strong> more left</span>', $coupon_code, $count, $remaining );
        }
    }
}
add_action( 'woocommerce_thankyou', 'action_woocommerce_thankyou', 10, 1 );
7uc1f3r
  • 28,449
  • 17
  • 32
  • 50
  • Can I display a list of all users who used this discount code in the same function? – Sinapars Jan 24 '21 at 09:19
  • Yes, use [get_used_by()](https://woocommerce.github.io/code-reference/classes/WC-Coupon.html#method_get_used_by) - Get records of all users who have used the current coupon. – 7uc1f3r Jan 24 '21 at 10:32
2
add_action('woocommerce_after_cart_table', 'coupon_count');
function coupon_count() {
    global $woocommerce;

    if ( ! empty( $woocommerce->cart->applied_coupons ) ) {
         $my_coupon = $woocommerce->cart->get_coupons() ;
         foreach($my_coupon as $coupon){

            if ( $post = get_post( $coupon->id ) ) {

                        $counter = $coupon->get_usage_count();
                        $remaining = $coupon->get_usage_limit()-$counter;
                        echo "<span class='name-coupon'><b>Total usage for coupon </b><b>'</b><b>".$coupon->code."</b><b>'</b><b>: </b></span>";
                        echo "<span class='coupon-counter'>".($counter)."</span>";
                        echo "<span class='coupon-counter'> there are ".($remaining)." more left</span>";

            }
        }
    }
}
mujuonly
  • 11,370
  • 5
  • 45
  • 75