Created
June 7, 2023 21:27
-
-
Save xardit/1df3bd302be3a91b77721c041402955c to your computer and use it in GitHub Desktop.
WP Woocommerce - Add "Cancel Order" button on Checkout page before place order, with action to Clear/Empty Cart and redirect to a referer page or wherever needed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
add_action( 'woocommerce_checkout_after_terms_and_conditions', 'custom_woocommerce_empty_cart_button' ); | |
function custom_woocommerce_empty_cart_button() { | |
echo '<a href="/?empty_cart=yes" class="rq-btn rq-btn-transparent" title="' . esc_attr( 'Cancel Order', 'woocommerce' ) . '">' . esc_html( 'Cancel Order', 'woocommerce' ) . '</a>'; | |
// echo '<a href="' . esc_url( '/cart' . add_query_arg( 'empty-cart', 'yes' ) ) . '" class="rq-btn rq-btn-transparent" title="' . esc_attr( 'Cancel Order', 'woocommerce' ) . '">' . esc_html( 'Cancel Order', 'woocommerce' ) . '</a>'; | |
} | |
add_action( 'wp_loaded', 'custom_woocommerce_empty_cart_action', 20 ); | |
function custom_woocommerce_empty_cart_action() { | |
if ( isset( $_GET['empty_cart'] ) && 'yes' === esc_html( $_GET['empty_cart'] ) ) { | |
WC()->cart->empty_cart(); | |
$referer = wp_get_referer() ? esc_url( remove_query_arg( 'empty_cart' ) ) : wc_get_cart_url(); | |
wp_safe_redirect( $referer ); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment