Skip to content

WooCommerce: Apply Coupon COde via URL Parameter

Use like this: https://examplestore.com/?coupon_code=5dollarOff

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');

via

Dieser Beitrag hat 0 Kommentare

Schreibe einen Kommentar

Deine E-Mail wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert

An den Anfang scrollen