Skip to content

Instantly share code, notes, and snippets.

@Ercogx
Last active April 5, 2022 15:53
Show Gist options
  • Save Ercogx/8cb6fb8d35b3390150b10e6893b9970b to your computer and use it in GitHub Desktop.
Save Ercogx/8cb6fb8d35b3390150b10e6893b9970b to your computer and use it in GitHub Desktop.

Revisions

  1. Ercogx revised this gist Apr 5, 2022. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion functions.php
    Original file line number Diff line number Diff line change
    @@ -7,7 +7,7 @@ function woocommerce_checkout_coupon_form_custom() {
    <div class="add_gift_card_form coupon-fictitious-form">
    <h4>Have a coupon?</h4>
    <div id="wc_gc_cart_redeem_form">
    <input placeholder="Enter your code" type="text" name="coupon_code" id="wc_gc_cart_code" autocomplete="off">
    <input placeholder="Coupon code" type="text" name="coupon_code" id="wc_gc_cart_code" autocomplete="off">
    <button type="button" name="apply_coupon" id="wc_gc_cart_redeem_send">Apply</button>
    </div>
    </div>
  2. Ercogx revised this gist Apr 5, 2022. No changes.
  3. Ercogx created this gist Apr 5, 2022.
    37 changes: 37 additions & 0 deletions functions.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,37 @@
    <?php

    // Add a custom coupon field before checkout payment section
    add_action( 'woocommerce_checkout_order_review', 'woocommerce_checkout_coupon_form_custom' );
    function woocommerce_checkout_coupon_form_custom() {
    ?>
    <div class="add_gift_card_form coupon-fictitious-form">
    <h4>Have a coupon?</h4>
    <div id="wc_gc_cart_redeem_form">
    <input placeholder="Enter your code…" type="text" name="coupon_code" id="wc_gc_cart_code" autocomplete="off">
    <button type="button" name="apply_coupon" id="wc_gc_cart_redeem_send">Apply</button>
    </div>
    </div>
    <?php
    }

    // jQuery code
    add_action( 'wp_footer', 'custom_checkout_jquery_script' );
    function custom_checkout_jquery_script() {
    if ( is_checkout() && ! is_wc_endpoint_url() ) :
    ?>
    <script type="text/javascript">
    jQuery( function($){
    // Copy the inputed coupon code to WooCommerce hidden default coupon field
    $('.coupon-fictitious-form input[name="coupon_code"]').on( 'input change', function(){
    $('form.checkout_coupon input[name="coupon_code"]').val($(this).val());
    });

    // On button click, submit WooCommerce hidden default coupon form
    $('.coupon-fictitious-form button[name="apply_coupon"]').on( 'click', function(){
    $('form.checkout_coupon').submit();
    });
    });
    </script>
    <?php
    endif;
    }
    3 changes: 3 additions & 0 deletions style.css
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,3 @@
    .thb-checkout-coupon{
    display: none;
    }