Hi there,
Here is what’s worked for me, with an explanation of what I think went wrong in the initial snippet you had. ??
function apply_coupon_fieldback() {
if ( ! empty( WC()->cart->applied_coupons ) ) { ?>
<div class="woocommerce-form-coupon-toggle">
<?php wc_print_notice( apply_filters( 'woocommerce_checkout_coupon_message', __( 'Have a coupon?', 'woocommerce' ) . ' <a href="#" class="showcoupon">' . __( 'Click here to enter your code', 'woocommerce' ) . '</a>' ), 'notice' ); ?>
</div>
<form class="checkout_coupon woocommerce-form-coupon" method="post" style="display:none">
<p><?php esc_html_e( 'If you have a coupon code, please apply it below.', 'woocommerce' ); ?></p>
<p class="form-row form-row-first">
<input type="text" name="coupon_code" class="input-text" placeholder="<?php esc_attr_e( 'Coupon code', 'woocommerce' ); ?>" id="coupon_code" value="" />
</p>
<p class="form-row form-row-last">
<button type="submit" class="button" name="apply_coupon" value="<?php esc_attr_e( 'Apply coupon', 'woocommerce' ); ?>"><?php esc_html_e( 'Apply coupon', 'woocommerce' ); ?></button>
</p>
<div class="clear"></div>
</form>
<?php
}
}
add_action ( 'woocommerce_before_checkout_form', 'apply_coupon_fieldback', 9);
Originally when I pasted your first snippet into my editor I needed to replace what looked like curly quotes with straight quotes. Maybe the editor you are using automatically does that if it is more suited to word processing? See here for more explanations on that.
I also just did a little re-arranging and updating to the code used. In particular, using Storefront it worked better, in my opinion, to place this above the checkout form so I used woocommerce_before_checkout_form
.
I also got your notice to appear, but it didn’t include a link that would open the form nor the actual form code. I’ve copied that over into the new snippet above. Let me know if that is helpful!
Here is a quick video so you can see what I see.