Created
June 23, 2022 11:10
-
-
Save kimcoleman/ab336488b0d19ce889e927edf649271f to your computer and use it in GitHub Desktop.
Apply a WooCommerce discount code to cart via URL parameter.
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 | |
/** | |
* Apply a WooCommerce discount code to cart via URL parameter. | |
* This code is adapted from: https://www.webroomtech.com/apply-coupon-via-url-in-woocommerce/ | |
*/ | |
function my_woocommerce_apply_cart_coupon_in_url() { | |
// Return early if WooCommerce or sessions aren't available. | |
if ( ! function_exists( 'WC' ) || ! WC()->session ) { | |
return; | |
} | |
// Return if there is no coupon in the URL, otherwise set the variable. | |
if ( empty( $_REQUEST['coupon'] ) ) { | |
return; | |
} else { | |
$coupon_code = esc_attr( $_REQUEST['coupon'] ); | |
} | |
// Set a session cookie to remember the coupon if they continue shopping. | |
WC()->session->set_customer_session_cookie(true); | |
// Apply the coupon to the cart if necessary. | |
if ( ! WC()->cart->has_discount( $coupon_code ) ) { | |
// WC_Cart::add_discount() sanitizes the coupon code. | |
WC()->cart->add_discount( $coupon_code ); | |
} | |
} | |
add_action('wp_loaded', 'my_woocommerce_apply_cart_coupon_in_url', 30); | |
add_action('woocommerce_add_to_cart', 'my_woocommerce_apply_cart_coupon_in_url'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment