Hello
I would like to have some code to add to functions.php that removes the error message for
"Coupon code already applied"
I don't want to remove any other error message, only this one.
Thanks!
Bob answers:
not sure may be it works.
function my_woocommerce_add_error( $error ) {
if( 'Coupon code already applied!' == $error ) {
$error = '';
}
return $error;
}
add_filter( 'woocommerce_add_error', 'my_woocommerce_add_error' );
FirstEntertainment comments:
Hi Bob. Sorry, don't have time to test stuff. Please test it first and give me code that is working =)
Bob comments:
Output of my code and Dbranes code are same.
It shows empty <li>
Bob comments:
One easy was is to skip it printing at template file.
You can create template file in your theme.
path should be YOURTHEME/woocommerce/notices/error.php
generally content is
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
if ( ! $messages ) return;
?>
<ul class="woocommerce-error">
<?php foreach ( $messages as $message ) : ?>
<li><?php echo wp_kses_post( $message ); ?></li>
<?php endforeach; ?>
</ul>
you just need to add one if condition like this.
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
if ( ! $messages ) return;
?>
<ul class="woocommerce-error">
<?php foreach ( $messages as $message ) : ?>
<?php if( $message == "Coupon code already applied!" ) continue; ?>
<li><?php echo wp_kses_post( $message ); ?></li>
<?php endforeach; ?>
</ul>
Dbranes answers:
There's the <em>woocommerce_coupon_error</em> filter:
/**
* Modify the coupon errors:
*/
add_filter( 'woocommerce_coupon_error', 'wpq_coupon_error', 10, 2 );
function wpq_coupon_error( $err, $err_code ) {
return ( '103' == $err_code ) ? '' : $err;
}
where you can get the error codes from here:
// Coupon message codes
const E_WC_COUPON_INVALID_FILTERED = 100;
const E_WC_COUPON_INVALID_REMOVED = 101;
const E_WC_COUPON_NOT_YOURS_REMOVED = 102;
const E_WC_COUPON_ALREADY_APPLIED = 103;
const E_WC_COUPON_ALREADY_APPLIED_INDIV_USE_ONLY = 104;
const E_WC_COUPON_NOT_EXIST = 105;
const E_WC_COUPON_USAGE_LIMIT_REACHED = 106;
const E_WC_COUPON_EXPIRED = 107;
const E_WC_COUPON_MIN_SPEND_LIMIT_NOT_MET = 108;
const E_WC_COUPON_NOT_APPLICABLE = 109;
const E_WC_COUPON_NOT_VALID_SALE_ITEMS = 110;
const E_WC_COUPON_PLEASE_ENTER = 111;
const E_WC_COUPON_MAX_SPEND_LIMIT_MET = 112;
const WC_COUPON_SUCCESS = 200;
const WC_COUPON_REMOVED = 201;
Reigel Gallarde answers:
you should add a filter to has_discount...
// Check if applied
if ( $this->has_discount( $coupon_code ) ) {
$the_coupon->add_coupon_message( WC_Coupon::E_WC_COUPON_ALREADY_APPLIED );
return false;
}
as to avoid add_coupon_message...
which emptying error message will still display wc_add_notice with empty message... it's because of the second parameter of wc_add_notice... which is added by add_coupon_message...
this is the filter for has_discount...
add_filter( 'woocommerce_coupon_code', 'woocommerce_coupon_code_no_discount', 10, 1 );
function woocommerce_coupon_code_no_discount($coupon_code){
return ''; // return anything that is not a coupon code...
}
another way is to remove the error on WC()->session->get( 'wc_notices');
you check for the error, just unset it...