I have created coupon links for Woocommerce with this:
function coupon_links()
{
if (!function_exists('WC') || !WC()->session) {
return;
}
$query_var = apply_filters('woocommerce_coupon_links_query_var', 'coupon');
if (empty($_GET[$query_var])) {
return;
}
WC()->session->set_customer_session_cookie(true);
if (!WC()->cart->has_discount($_GET[$query_var])) {
WC()->cart->add_discount($_GET[$query_var]);
}
}
add_action('wp_loaded', 'coupon_links', 30);
add_action('woocommerce_add_to_cart', 'coupon_links');
I need this in order to auto-apply a specific coupon when the user clicks on a link on the cart page. While it works, it is not ajax loaded. How can I make this link ajax so that the cart page does not refresh, but instead just ajax load like when you remove a coupon?