Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WooCommerce: Move Coupon Field on Cart Page

I am trying to change the position of the coupon field on the cart page. I am vaguely familiar with actions and hooks, so I know I need to remove the woocommerce_cart_coupon action first and hook it to a different action, as in this example:

remove_action( 'woocommerce_checkout_order_review', 'woocommerce_order_review', 10 );
add_action( 'woocommerce_checkout_before_customer_details', 'woocommerce_order_review', 10 );

My problem is, I don't know to from where to unhook this action. I cannot find a list of cart hooks and their hooked actions (like the content-single-product.php file) + order.

The coupon field should be placed after the "Proceed to Checkout" button. Any ideas?

Thanks

like image 775
Dot123 Avatar asked Nov 25 '25 14:11

Dot123


2 Answers

The hook woocommerce_cart_coupon doesn't hook the coupon php code itself and you will not be able to use it as in your example.

The related coupon php code (on cart page) is located on cart/cart.php template at line 132 to 150 and isn't hooked within a separate template. So You will have to edit cart/cart.php template, and this is not going to be so easy.

Here is the official related documentation for Overriding Templates via a Theme (Templates structure)

like image 75
LoicTheAztec Avatar answered Nov 28 '25 02:11

LoicTheAztec


Wrap your coupon PHP in a form tag with the correct form action.

<?php if ( wc_coupons_enabled() ) { ?>
 <div class="coupon">
  <form action="http://example.com/cart/" method="post">
   <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>
   </form>
  <?php do_action( 'woocommerce_cart_coupon' ); ?>
 </div>
<?php } ?>
like image 24
JTK Avatar answered Nov 28 '25 04:11

JTK



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!