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