Categories

Monday, November 18, 2024
#919814419350 therichposts@gmail.com
WoocommerceWoocommerce Hooks

Display coupon usage count and limit in WooCommerce thank you page

Display coupon usage count and limit in WooCommerce thank you page

Hello guys how are you? Welcome back to my channel. Today in this post I am going to show you Display coupon usage count and limit in WooCommerce thank you page.

Live Demo

Guys if you are new in WordPress or in WooCommerce then please check the below links for some good tutorials:

  1. WooCommerce Hooks
  2. WordPress Tricks
  3. WordPress Hooks

Guys here is the working code snippet for Display coupon usage count and limit in WooCommerce thank you page and please use carefully:

1. Guys here is the code snippet and you need to add your theme’s functions.php file

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

This is it guys and if you will have any kind of query, suggestion or requirement then feel free to comment below.

Jassa

Developer’s King

Thanks

therichpost
the authortherichpost
Hello to all. Welcome to therichpost.com. Myself Ajay Malhotra and I am freelance full stack developer. I love coding. I know WordPress, Core php, Angularjs, Angular 19, MedusaJs, Next.js, Bootstrap 5, Nodejs, Laravel, Codeigniter, Shopify, Squarespace, jQuery, Google Map Api, Vuejs, Reactjs, Big commerce etc.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.