Situation: We have a coupon code (Free $5 cash) that we want customers to use one time. Until he use it, show a special message on my account. After he used the coupon hide the special message. The example below is based on a recent free $5 cash gift we gave out at EventON.
Method: Use user_meta in wordpress to record if customer used the coupon code at checkout. Then use user_meta saved value conditionally to show the special message on my account page.
Execution: The below code can be added into functions.php file inside a theme or as a separate file that can be included into your theme or in a plugin.
Step 1: Record coupon code usage at checkout
The coupon code we have created in woocommerce that gives out customers a one time free $5 at checkout is ‘giftevo2020’. If a customer use that coupon at checkout we are going to record that using user_meta.
<?php class Custom_class{ public function __construct(){ add_action('woocommerce_checkout_order_processed', array($this, 'checkout_processed'), 10, 3); } public function checkout_processed($order_id, $posted_data, $order){ // get all coupons in cart $cc = $order->get_items('coupon'); // if no coupons used return if( !$cc ) return; // run through for each coupons used in cart foreach($cc as $cc1){ // if coupon code is free $5 coupon if($cc1->get_code() == 'giftevo2020'){ // save to user meta update_user_meta( $this->current_user->ID , 'evo_coupon_used_2020', 'giftevo2020'); } } } } new Custom_class();
Step 2: Conditionally display account content
Now we grab user_meta saved value ‘evo_coupon_used_2020’ and check if our $5 free cash coupon code ‘giftevo2020’ was used by this current logged in customer. If they have not used it then show a special message. The message here will tell them about this coupon code. Once the customer used the coupon code for one time, this special message will no longer show in my accounts page. I use woocommerce account hook “woocommerce_account_dashboard” to show this special message.
<?php public function dashboard(){ // GIFT Cash $hide_gift = true; // check if current user has coupon code in user meta $this->current_user = wp_get_current_user(); $coupon_used = get_user_meta( $this->current_user->ID, 'evo_coupon_used_2020'); // if this use has not used coupon code show special message if(!in_array('giftevo2020', $coupon_used) ): ?> <div class='b50' style=' background: linear-gradient(45deg, #ff5b9d, #ff9570); color: #fff;'> <h2>Our Gift to You</h2> <p style='line-height: 1.3;font-size: 36px;'>$5 Free Cash to Spend</p> <p>Code at checkout: <b>GIFTEVO2020</b></p> <p>Use this free cash to purchase any of our addons before Dec. 2nd, 2020</p> <p><a class='btn btn_grey' href='https://www.myeventon.com/addons/'>Find Addons</a></p> </div> <?php endif; }
View in My Account
Below is how the free $5 cash coupon code special message will appear in my account page.

Complete Code
<?php class Custom_class{ public function __construct(){ add_action('woocommerce_checkout_order_processed', array($this, 'checkout_processed'), 10, 3); add_action('woocommerce_account_dashboard',array($this,'dashboard') ); } public function checkout_processed($order_id, $posted_data, $order){ // get all coupons in cart $cc = $order->get_items('coupon'); // if no coupons used return if( !$cc ) return; // run through for each coupons used in cart foreach($cc as $cc1){ // if coupon code is free $5 coupon if($cc1->get_code() == 'giftevo2020'){ // save to user meta update_user_meta( $this->current_user->ID , 'evo_coupon_used_2020', 'giftevo2020'); } } } public function dashboard(){ // GIFT Cash $hide_gift = true; // check if current user has coupon code in user meta $this->current_user = wp_get_current_user(); $coupon_used = get_user_meta( $this->current_user->ID, 'evo_coupon_used_2020'); // if this use has not used coupon code show special message if(!in_array('giftevo2020', $coupon_used) ): ?> <div class='b50' style=' background: linear-gradient(45deg, #ff5b9d, #ff9570); color: #fff;'> <h2>Our Gift to You</h2> <p style='line-height: 1.3;font-size: 36px;'>$5 Free Cash to Spend</p> <p>Code at checkout: <b>GIFTEVO2020</b></p> <p>Use this free cash to purchase any of our addons before Dec. 2nd, 2020</p> <p><a class='btn btn_grey' href='https://www.myeventon.com/addons/'>Find Addons</a></p> </div> <?php endif; } } new Custom_class();
4 years ago
When all I can breath is wildfire smoke
Wildfire Smoke 2 weeks ago Oregon had a bad wildfire spread from multiple wildfires, burning about 200k-300k acres of forests. In my time in Oregon, this is the second bad…
5 years ago
Lessons from Nansen Summit
Nansen Summit is the summit of Mount Sylvalia that is close to where I live, with an elevation of just shy of 1000ft. Only after living here 2 years, I…
5 years ago
What if all the people realized they don't need stuff to be happy tomorrow
US economy and the global economy -for the best part- is run by consumers. And these consumers empower big corporations to exists. This whole so called economic system created by…
5 years ago
Valley of Lauterbrunnen
Lauterbrunnen valley in Switzerland is one of the best places I have been to where you can easily find the nature at its best display. This painting is from my…
5 years ago
Making of The Männlichen Painting
Switzerland is just breathtaking and every where you turn up in the great alps, is just another view worth painting. The painting of Männlichen is based on a photo i…
5 years ago
Welcome 2020 - The existence of this site
Hello everyone I am Ashan Jay. I am not an extraordinaire or a smart genius, I am just an average guy who falls just like everyone else but learn from…
5 years ago
Books I have read or listened in 2019
A complaint free world by Will Bowen The magic of thinking big by David Schwartz It works by RHJ 12 Rules for life by Jordan Paterson Radical Acceptance by Tara…