Last active
May 12, 2022 13:15
-
-
Save Ostii/6a53aa436fc25e7b8be9a9bdd6e33c30 to your computer and use it in GitHub Desktop.
This file contains 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
function webroom_woocommerce_coupon_links(){ | |
// Bail if WooCommerce or sessions aren't available. | |
if (!function_exists('WC') || !WC()->session) { | |
return; | |
} | |
/** | |
* Filter the coupon code query variable name. | |
* | |
* @since 1.0.0 | |
* | |
* @param string $query_var Query variable name. | |
*/ | |
$query_var = apply_filters('woocommerce_coupon_links_query_var', 'coupon_code'); | |
// Bail if a coupon code isn't in the query string. | |
if (empty($_GET[$query_var])) { | |
return; | |
} | |
// Set a session cookie to persist the coupon in case the cart is empty. | |
WC()->session->set_customer_session_cookie(true); | |
// Apply the coupon to the cart if necessary. | |
if (!WC()->cart->has_discount($_GET[$query_var])) { | |
// WC_Cart::add_discount() sanitizes the coupon code. | |
WC()->cart->add_discount($_GET[$query_var]); | |
} | |
} | |
add_action('wp_loaded', 'webroom_woocommerce_coupon_links', 30); | |
add_action('woocommerce_add_to_cart', 'webroom_woocommerce_coupon_links'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment