Skip to content

Instantly share code, notes, and snippets.

@sarangs07
Created July 17, 2020 05:34
Show Gist options
  • Save sarangs07/83156f7f076b51b668025f1f0414d44c to your computer and use it in GitHub Desktop.
Save sarangs07/83156f7f076b51b668025f1f0414d44c to your computer and use it in GitHub Desktop.
Apply the coupon on the Checkout page via URL.
/*
* How to add the custome code.
*
* Please follow below instructions:
* 1. Copy the below code.
* 2. Open your child theme's functions.php file.
* 3. Paste the copied code at the very bottom of it & save the file OR upload it on your server/hosting.
*/
add_action( 'wp', 'wc_add_the_coupon_from_url' );
function wc_add_the_coupon_from_url() {
$discount_coupon = '';
if( isset( $_GET['token'] ) ){
$discount_coupon = sanitize_text_field( wp_unslash( $_GET['token'] ) );
}
if ( is_array( $discount_coupon ) && ! empty( $discount_coupon ) ) {
$discount_coupon = reset( $discount_coupon );
}
if ( ! empty( $discount_coupon ) ) {
$show_coupon_msg = apply_filters( 'mycustom_filter_action', true );
if ( ! $show_coupon_msg ) {
add_filter( 'woocommerce_coupon_message', '__return_empty_string' );
}
WC()->cart->add_discount( $discount_coupon );
if ( ! $show_coupon_msg ) {
remove_filter( 'woocommerce_coupon_message', '__return_empty_string' );
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment