Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
WooCommerce - hide the coupon form on the cart or checkout page, but leave coupons enabled for use with plugins like Smart Coupons and URL Coupons
<?php
// hide coupon field on cart page
function hide_coupon_field_on_cart( $enabled ) {
if ( is_cart() ) {
$enabled = false;
}
return $enabled;
}
add_filter( 'woocommerce_coupons_enabled', 'hide_coupon_field_on_cart' );
// hide coupon field on checkout page
function hide_coupon_field_on_checkout( $enabled ) {
if ( is_checkout() ) {
$enabled = false;
}
return $enabled;
}
add_filter( 'woocommerce_coupons_enabled', 'hide_coupon_field_on_checkout' );
@urbanoantunes

This comment has been minimized.

Copy link

@urbanoantunes urbanoantunes commented Aug 22, 2015

where do I add this?

@sbellver

This comment has been minimized.

Copy link

@sbellver sbellver commented Sep 28, 2015

In your function.php (in theme folder)

@StefsterNYC

This comment has been minimized.

Copy link

@StefsterNYC StefsterNYC commented May 13, 2016

Anyone see a hook for hiding it from a specific user role? I have Pro MUA as a user role and I need to hide the coupon from them.

I added the following but it's not working

`function woo_get_user_role() {
global $current_user;
$user_roles = $current_user->roles;
$user_role = array_shift($user_roles);
return $user_role;
}

function hide_coupon_field_on_cart( $enabled ) {
if(woo_get_user_role() =='pro_mua_customer' && is_cart() || is_checkout() ) {
$enabled = false;
}
return $enabled;
}
add_filter( 'woocommerce_coupons_enabled', 'hide_coupon_field_on_cart' );`

@photoncreative

This comment has been minimized.

Copy link

@photoncreative photoncreative commented Jan 19, 2017

Works a treat! Just make sure to remove the first line <?php from this code snippet if it's already in your functions.php file. You want to paste this code snipper just before the last line of the functions file which should be ?>

@neilbannet

This comment has been minimized.

Copy link

@neilbannet neilbannet commented Aug 16, 2017

Thanks ;)

@cristianryu

This comment has been minimized.

Copy link

@cristianryu cristianryu commented Aug 29, 2017

I have a problem. I've placed this code and everything seems to work fine.
But when I add a product with a discount coupon through URL Coupons to the Cart, the "Remove" link stops working (The one that removes the coupon from the product). In the checkout page the "Remove" link works fine.
What can happen? Does anyone have any help, please?

@indefinitelee

This comment has been minimized.

Copy link

@indefinitelee indefinitelee commented Oct 19, 2017

just want to ask to be sure: It would be best to create a child theme for woocommerce for the cart and/or checkout pages and then put this code in the functions.php of those themes, correct?

@jchrislemmer

This comment has been minimized.

Copy link

@jchrislemmer jchrislemmer commented Oct 25, 2017

Ah, very nice piece of code. Thanks! 👍

@Shubhras

This comment has been minimized.

Copy link

@Shubhras Shubhras commented Dec 12, 2017

Very Nice code

@naliseth

This comment has been minimized.

Copy link

@naliseth naliseth commented Jan 8, 2018

Hello, i have a question. i configured a rule on my woocomerce->coupon (for example apply a coupon discount when min spend is 27$). Now when I want to pay the bill, always appear: do you have any coupon? please enter the code....... Then i only want that this message appear only when the bill is >27$. How can i do it?
Thanks

@craigcooperxyz

This comment has been minimized.

Copy link

@craigcooperxyz craigcooperxyz commented Jan 25, 2018

thanks

@tigredanky

This comment has been minimized.

Copy link

@tigredanky tigredanky commented May 12, 2018

thank you. works great on checkout!

@jjbbrr

This comment has been minimized.

Copy link

@jjbbrr jjbbrr commented May 29, 2018

Thanks - this still works with Woocommerce 3.4 and Wordpress 4.9.6 :)

@TGurgen

This comment has been minimized.

Copy link

@TGurgen TGurgen commented Nov 3, 2018

What if my checkout and cart on the same page, and i want hide it only on checkout? what code i can use?

@KoolPal

This comment has been minimized.

Copy link

@KoolPal KoolPal commented May 15, 2019

@TGurgen, use
function hide_coupon_field_on_cart( $enabled ) { if ( is_cart() ) { $enabled = false; } return $enabled; } add_filter( 'woocommerce_coupons_enabled', 'hide_coupon_field_on_cart' );

And
add_action( 'woocommerce_before_checkout_form', 'remove_checkout_coupon_form', 9 ); function remove_checkout_coupon_form(){ remove_action( 'woocommerce_before_checkout_form', 'woocommerce_checkout_coupon_form', 10 ); }

@zspringer

This comment has been minimized.

Copy link

@zspringer zspringer commented Jan 30, 2020

Works great as of Jan. 2020!

@Mohammad-Afridi

This comment has been minimized.

Copy link

@Mohammad-Afridi Mohammad-Afridi commented Jun 26, 2020

I have woocommerce site running woocommerce memberships and I am trying to disable to coupon entry box in both the cart and checkout for members only. Non members should still see the coupon entry box and be able to use coupons, but people with a membership should not see it at all.I want to do this with a code snippet. Anyone can help me ?

@jpensotes

This comment has been minimized.

Copy link

@jpensotes jpensotes commented Dec 18, 2020

Works perfectly. Kudos

@oitent

This comment has been minimized.

Copy link

@oitent oitent commented Jun 9, 2021

Say I'd like to hide it only when I already have a coupon applied. Can that be done editing this code?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment