Ahmad Naser Turnkey Ecosystem Ahmad Naser
May 2, 2020

Force Coupon Code In Woocommerce form-pay.php

Published in the Ahmad Naser ecosystem. Estimated reading time: 5 minutes.

<?php
defined( 'ABSPATH' ) || exit;
$link = $_SERVER['REQUEST_URI'];
?>
<script>
	(function ($) {
$( document ).on( 'click', 'form.checkout_coupon.woocommerce-form-coupon > p.form-row.form-row-last > button', function(e) {
	console.log('ccc');
	coupon_val = $('#coupon_code').val();
	var currentLocation = "<?php echo $link;?>";
	currentLocation = currentLocation + "&coupon_code="+coupon_val;
	window.location.href = currentLocation;
e.preventDefault();
});
})(jQuery);
</script>
<?php
$totals = $order->get_order_item_totals(); // phpcs:ignore WordPress.WP.GlobalVariablesOverride.OverrideProhibited
$order_id = $order->get_order_number();
$order = wc_get_order( $order_id );

$cpon = $_GET['coupon_code'];
if(isset($cpon)){
$coupon_code = $cpon;
if(!empty($coupon_code)){

//$woocommerce->cart->calculate_totals();
$coupon = new WC_Coupon( $coupon_code);




if( true ) {
$order->apply_coupon( $coupon );
$order->calculate_totals();
?>
<script>
  if(!window.location.hash) {
        window.location = window.location + '#loaded';
        window.location.reload();
    }
</script>
<?php
}
}
}
  
  
  
  
  
// Calculate totals



?>
<div class="woocommerce-form-coupon-toggle">
	<?php wc_print_notice( apply_filters( 'woocommerce_checkout_coupon_message', esc_html__( 'Have a coupon?', 'woocommerce' ) . ' <a href="#" class="showcoupon">' . esc_html__( 'Click here to enter your code', 'woocommerce' ) . '</a>' ), 'notice' ); ?>
</div>

<form class="checkout_coupon woocommerce-form-coupon" method="post" style="display:block;">

	<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>
<form id="order_review" method="post">
  <table class="shop_table">
    <thead>
      <tr>
        <th class="product-name"><?php esc_html_e( 'Product', 'woocommerce' ); ?></th>
        <th class="product-quantity"><?php esc_html_e( 'Qty', 'woocommerce' ); ?></th>
        <th class="product-total"><?php esc_html_e( 'Totals', 'woocommerce' ); ?></th>
      </tr>
    </thead>
    <tbody>
      <?php if ( count( $order->get_items() ) > 0 ) : ?>
        <?php foreach ( $order->get_items() as $item_id => $item ) : ?>
          <?php
          if ( ! apply_filters( 'woocommerce_order_item_visible', true, $item ) ) {
            continue;
          }
          ?>
          <tr class="<?php echo esc_attr( apply_filters( 'woocommerce_order_item_class', 'order_item', $item, $order ) ); ?>">
            <td class="product-name">
              <?php
                echo apply_filters( 'woocommerce_order_item_name', esc_html( $item->get_name() ), $item, false ); // @codingStandardsIgnoreLine
                do_action( 'woocommerce_order_item_meta_start', $item_id, $item, $order, false );
                wc_display_item_meta( $item );
                do_action( 'woocommerce_order_item_meta_end', $item_id, $item, $order, false );
              ?>
            </td>
            <td class="product-quantity"><?php echo apply_filters( 'woocommerce_order_item_quantity_html', ' <strong class="product-quantity">' . sprintf( '&times; %s', esc_html( $item->get_quantity() ) ) . '</strong>', $item ); ?></td><?php // @codingStandardsIgnoreLine ?>
            <td class="product-subtotal"><?php echo $order->get_formatted_line_subtotal( $item ); ?></td><?php // @codingStandardsIgnoreLine ?>
          </tr>
        <?php endforeach; ?>
      <?php endif; ?>
    </tbody>
    <tfoot>
      <?php if ( $totals ) : ?>
        <?php foreach ( $totals as $total ) : ?>
          <tr>
            <th scope="row" colspan="2"><?php echo $total['label']; ?></th><?php // @codingStandardsIgnoreLine ?>
            <td class="product-total"><?php echo $total['value']; ?></td><?php // @codingStandardsIgnoreLine ?>
          </tr>
        <?php endforeach; ?>
      <?php endif; ?>
    </tfoot>
  </table>
  
  
  
  
  
  
  <div id="payment">
    <?php if ( $order->needs_payment() ) : ?>
      <ul class="wc_payment_methods payment_methods methods">
        <?php
        if ( ! empty( $available_gateways ) ) {
          foreach ( $available_gateways as $gateway ) {
            wc_get_template( 'checkout/payment-method.php', array( 'gateway' => $gateway ) );
          }
        } else {
          echo '<li class="woocommerce-notice woocommerce-notice--info woocommerce-info">' . apply_filters( 'woocommerce_no_available_payment_methods_message', __( 'Sorry, it seems that there are no available payment methods for your location. Please contact us if you require assistance or wish to make alternate arrangements.', 'woocommerce' ) ) . '</li>'; // @codingStandardsIgnoreLine
        }
        ?>
      </ul>
    <?php endif; ?>
    <div class="form-row">
      <input type="hidden" name="woocommerce_pay" value="1" />
      <?php wc_get_template( 'checkout/terms.php' ); ?>
      <?php do_action( 'woocommerce_pay_order_before_submit' ); ?>
      <?php echo apply_filters( 'woocommerce_pay_order_button_html', '<button type="submit" class="button alt" id="place_order" value="' . esc_attr( $order_button_text ) . '" data-value="' . esc_attr( $order_button_text ) . '">' . esc_html( $order_button_text ) . '</button>' ); // @codingStandardsIgnoreLine ?>
      <?php do_action( 'woocommerce_pay_order_after_submit' ); ?>
      <?php wp_nonce_field( 'woocommerce-pay', 'woocommerce-pay-nonce' ); ?>
    </div>
  </div>
</form>


 

Leave a Reply

Your email address will not be published. Required fields are marked *