The following custom Shortcode, will display a text imput field (with a submit button) where user can enter a coupon code to be applied.
Usage: [coupon_field]
or in Php code echo do_shortcode("[coupon_field]");
The code:
add_shortcode( 'coupon_field', 'display_coupon_field' );
function display_coupon_field() {
if( isset($_GET['coupon']) && isset($_GET['redeem-coupon']) ){
if( $coupon = esc_attr($_GET['coupon']) ) {
$applied = WC()->cart->apply_coupon($coupon);
} else {
$coupon = false;
}
$success = sprintf( __('Coupon "%s" Applied successfully.'), $coupon );
$error = __("This Coupon can't be applied");
$message = isset($applied) && $applied ? $success : $error;
}
$output="';
}
Code goes on function.php file of your active child theme (or theme). Tested and works.
It displays a success or an error message, once applying a coupon.