I want to hide the coupon field in the cart.
To do so, I've wrapped it in a Bootstrap collapse element and added display:none
to the existing coupon field.
EDIT: After the comment from @gael (WooCommerce: Coupon doens't work in cart if I wrap it in collapse element), I figuered out, that the form works if I show it in other places of the template. But now it doesn't show any success or error messages anymore?!
Here's my code:
<?php
add_action( 'woocommerce_after_cart_table', 'display_coupon_form_below_proceed_checkout', 50 );
function display_coupon_form_below_proceed_checkout() {
?>
<small><a class="text-muted" data-toggle="collapse" href="#collapseCartCoupon" role="button" aria-expanded="false" aria-controls="collapseCartCoupon"><?php esc_attr_e( 'Coupon code', 'woocommerce' ); ?></a></small>
<form class="woocommerce-coupon-form" action="<?php echo esc_url( wc_get_cart_url() ); ?>" method="post">
<div class="collapse" id="collapseCartCoupon">
<?php if ( wc_coupons_enabled() ) { ?>
<div class="coupon under-proceed">
<input type="text" name="coupon_code" class="input-text" id="coupon_code" value="" placeholder="<?php esc_attr_e( 'Coupon code', 'woocommerce' ); ?>" style="width: 100%" />
<button type="submit" class="button" name="apply_coupon" value="<?php esc_attr_e( 'Apply coupon', 'woocommerce' ); ?>" style="width: 100%"><?php esc_attr_e( 'Apply coupon', 'woocommerce' ); ?></button>
</div>
<?php } ?>
</div>
</form>
<style>
.woocommerce table.shop_table .coupon {display: none !important;}
</style>
<?php
}
I would override the Cart page on WooCommerce to wrap the Coupon in a collapse element.
This template can be overridden by copying the file from wp-content/plugins/woocommerce/templates/cart/cart.php
it to wp-content/themes/yourtheme/woocommerce/cart/cart.php
.
Then from line 142 to 150 in WooCommerce v4, you can insert your snippet like so:
<?php if ( wc_coupons_enabled() ) { ?>
<small><a class="text-muted" data-toggle="collapse" href="#collapseCartCoupon" role="button" aria-expanded="false" aria-controls="collapseCartCoupon"><?php esc_attr_e( 'Coupon code', 'woocommerce' ); ?></a></small>
<div class="coupon collapse" id="collapseCartCoupon">
<label for="coupon_code"><?php esc_html_e( 'Coupon:', 'woocommerce' ); ?></label> <input type="text" name="coupon_code" class="input-text" id="coupon_code" value="" placeholder="<?php esc_attr_e( 'Coupon code', 'woocommerce' ); ?>" /> <button type="submit" class="button" name="apply_coupon" value="<?php esc_attr_e( 'Apply coupon', 'woocommerce' ); ?>"><?php esc_attr_e( 'Apply coupon', 'woocommerce' ); ?></button>
<?php do_action( 'woocommerce_cart_coupon' ); ?>
</div>
<?php } ?>
Since my theme is already using Bootstrap, the collapse
class already hides the whole div so I didn't have to add your inline style.
I did test to move it into a new <tr>
under the table and the coupon code is still working. I guess it only have to be inside the <form>...</form>
to be available.
Also note that warning from WooCommerce on top of the template file:
* HOWEVER, on occasion WooCommerce will need to update template files and you
* (the theme developer) will need to copy the new files to your theme to
* maintain compatibility. We try to do this as little as possible, but it does
* happen. When this occurs the version of the template file will be bumped and
* the readme will list any important changes.