Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(modal-checkout): custom coupon form #1629

Merged
merged 2 commits into from
Dec 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/modal-checkout/checkout.scss
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,13 @@
flex-grow: 1;
}
}
p.result {
margin: 0;
font-size: 14px;
&.error {
color: #cc1818;
}
}
}
#checkout_details { /* stylelint-disable-line */
display: flex;
Expand Down
67 changes: 58 additions & 9 deletions src/modal-checkout/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,20 +37,12 @@ import './checkout.scss';
return;
}

const $coupon = $( 'form.checkout_coupon' );
const $coupon = $( 'form.modal_checkout_coupon' );
const $checkout_continue = $( '#checkout_continue' );
const $customer_details = $( '#customer_details' );
const $after_customer_details = $( '#after_customer_details' );
const $place_order_button = $( '#place_order' );

/**
* Ensure coupon form is shown after removing a coupon.
*/
$( document.body ).on( 'removed_coupon_in_checkout', function () {
$coupon.show();
clearNotices();
} );

/**
* Handle styling update for selected payment method.
*/
Expand Down Expand Up @@ -173,6 +165,63 @@ import './checkout.scss';
container.dispatchEvent( readyEvent );
}

/**
* Handle coupon form submit.
*
* @param {Event} ev
*/
function handleCouponSubmit( ev ) {
ev.preventDefault();
if ( $coupon.is( '.processing' ) ) {
return false;
}
$coupon.addClass( 'processing' ).block( {
message: null,
overlayCSS: {
background: '#fff',
opacity: 0.6,
},
} );
const data = {
security: wc_checkout_params.apply_coupon_nonce,
coupon_code: $coupon.find( 'input[name="coupon_code"]' ).val(),
};
// Ajax request.
$.ajax( {
type: 'POST',
url: wc_checkout_params.wc_ajax_url.toString().replace( '%%endpoint%%', 'apply_coupon' ),
data,
dataType: 'html',
success: code => {
clearNotices();
$coupon.find( '.result' ).remove();
if ( code ) {
const isError = code.includes( 'error' );
$coupon.append(
'<p class="result ' + ( isError ? 'error' : '' ) + '">' + $( code ).text() + '</p>'
);
if ( isError ) {
$coupon.find( 'input[name="coupon_code"]' ).focus();
}
$( document.body ).trigger( 'applied_coupon_in_checkout', [ data.coupon_code ] );
$( document.body ).trigger( 'update_checkout', { update_shipping_method: false } );
}
},
complete: () => {
// Unblock form.
$coupon.removeClass( 'processing' ).unblock();
},
} );
}
if ( $coupon.length ) {
$coupon.on( 'submit', handleCouponSubmit );
$( document.body ).on( 'removed_coupon_in_checkout', () => {
clearNotices();
$coupon.find( '.result' ).remove();
$coupon.find( 'input[name="coupon_code"]' ).val( '' ).focus();
} );
}

/**
* Handle form 1st step submission.
*
Expand Down
2 changes: 1 addition & 1 deletion src/modal-checkout/templates/form-coupon.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
}

?>
<form class="checkout_coupon woocommerce-form-coupon" method="post">
<form class="modal_checkout_coupon woocommerce-form-coupon" method="post">
<h3><?php esc_html_e( 'Apply a coupon code', 'newspack-blocks' ); ?></h3>
<p>
<input type="text" name="coupon_code" class="input-text" placeholder="<?php esc_attr_e( 'Coupon code', 'newspack-blocks' ); ?>" id="coupon_code" value="" />
Expand Down